2010-10-19

网络字节序和主机字节序

Little endian:将低序字节存储在起始地址,主要是x86。
Big endian:将高序字节存储在起始地址,对于大端直接按照顺序读就可以了,网络序和PPC使用大端。
但要注意的是Big Endian 和 Little Endian并不单单由CPU或OS决定,而是两者共同决定的。
看例子,对于0x01020304

内存

Little endian

Big endian

01

04

 

02

03

 

03

02

04

01


2010-04-23

Java获取系统信息最优化代码

import java.util.Map.Entry;

 

public class Env {

    public static void main(String[] args) {

       for (Entry<Object, Object> e : System.getProperties().entrySet()) {

           System.out.printf("%s = %s\n", e.getKey(), e.getValue());

       }

    }

}


2010-04-22

readyState状态详解

    大多数javascript初学者都只知道XMLHttpRequest请求时,readyState == 4代表请求的数据已经完全可用了,但是对0,1,2,3,4等值代表的具体含义,不一定完全了解了,同时,国内关于javascript方面的书籍、文章,鱼龙混杂,互相冲突,那究竟不同的readyState代表的具体含义是什么呢?查阅了最权威的w3c官方文档,终于找到了结果:
    文档The XMLHttpRequest Object,对readyState的描述如下:
    readyState of type unsigned short, readonly
The state of the object. The attribute MUSt be one of the following values:
    0 Uninitialized
The initial value.
    1 Open
The open() method has been successfully called.
    2 Sent
The UA successfully completed the request, but no data has yet been received.
    3 Receiving
Immediately before receiving the message body (if any). All HTTP headers have been received.
    4 Loaded
The data transfer has been completed.
    但是,这并不是最终的答案,我们注意到,这份文档标记为W3C Working Draft 05 April 2006,在该文档标题下方,标记有Latest version:XMLHttpRequest,标记日期为W3C Working Draft 19 November 2009,也就是说,前面的描述不是最新的,那最新的文档怎么描述的呢?两者是否相同呢?答案是否定的,内容如下:
    The XMLHttpRequest object can be in several states. The readyState attribute, on getting, must return the current state, which must be one of the following values:

    UNSENT (numeric value 0)

The object has been constructed.

    OPENED (numeric value 1)

The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method.

    HEADERS_RECEIVED (numeric value 2)

All HTTP headers have been received. Several response members of the object are now available.

    LOADING (numeric value 3)

The response entity body is being received.

    DONE (numeric value 4)

The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).

    The OPENED state has an associated send() flag that indicates whether the send() method has been invoked. It can be either true or false and has an initial value of false.

    The DONE state has an associated error flag that indicates some type of network error or abortion. It can be either true or false and has an initial value of false.

    略加分析,我们可以发现,这两份文档对readyState == 0,1,4的描述基本相同,但对于2和3的描述是完全不同的旧文档中,readyState == 2时,只是已经完成了send()操作,并没有收到任何数据,readyState == 3只是收到了HTTP headers,这样,浏览器大多数时间应该是停在3上,等待数据体和最后的数据处理了,并且readyState == 3时,是不能够处理数据的,新文档中,去除了Sent状态,2状态时所作的事情基本等同于旧文档的2和3状态,即调用了send(),并且只收到了HTTP headers,同时强化了数据接收的状态描述,将数据接收分为了接收数据内容(readyState == [2, 3))和内容处理(readyState == [3, 4))(主要包括null处理,根据Content-Type读取和处理MIME类型,字符集转换和错误处理等工作),也就是说,如果您很清楚返回值的情况的话,那么当readyState == 3时,服务器返回的数据已经完全传输完毕,您就可以自己开始处理数据了。


2010-04-07

linux下查看cpu信息

Linux下可以在/proc/cpuinfo中看到每个cpu地详细信息。

当然对于双核地cpu,在cpuinfo中会看到两个cpu。常常会让人误以为是两个单核地cpu。

其实应该通过Physical Processor ID来区分单核和双核。而Physical Processor ID可以从cpuinfo或者dmesg中找到

例如
root@debian:~:0# dmesg | grep CPU | grep ID
CPU0: Initial APIC ID: 0, Physical Processor ID: 0
CPU1: Initial APIC ID: 1, Physical Processor ID: 0
CPU2: Initial APIC ID: 6, Physical Processor ID: 3
CPU3: Initial APIC ID: 7, Physical Processor ID: 3

可以看到,这台机器有两个双核地CPU,ID分别是0和3。

Mime Types By File Extension


ExtensionType/sub-type
 application/octet-stream
323text/h323
acxapplication/internet-property-stream
aiapplication/postscript
aifaudio/x-aiff
aifcaudio/x-aiff
aiffaudio/x-aiff
asfvideo/x-ms-asf
asrvideo/x-ms-asf
asxvideo/x-ms-asf
auaudio/basic
avivideo/x-msvideo
axsapplication/olescript
bastext/plain
bcpioapplication/x-bcpio
binapplication/octet-stream
bmpimage/bmp
ctext/plain
catapplication/vnd.ms-pkiseccat
cdfapplication/x-cdf
cerapplication/x-x509-ca-cert
classapplication/octet-stream
clpapplication/x-msclip
cmximage/x-cmx
codimage/cis-cod
cpioapplication/x-cpio
crdapplication/x-mscardfile
crlapplication/pkix-crl
crtapplication/x-x509-ca-cert
cshapplication/x-csh
csstext/css
dcrapplication/x-director
derapplication/x-x509-ca-cert
dirapplication/x-director
dllapplication/x-msdownload
dmsapplication/octet-stream
docapplication/msword
dotapplication/msword
dviapplication/x-dvi
dxrapplication/x-director
epsapplication/postscript
etxtext/x-setext
evyapplication/envoy
exeapplication/octet-stream
fifapplication/fractals
flrx-world/x-vrml
gifimage/gif
gtarapplication/x-gtar
gzapplication/x-gzip
htext/plain
hdfapplication/x-hdf
hlpapplication/winhlp
hqxapplication/mac-binhex40
htaapplication/hta
htctext/x-component
htmtext/html
htmltext/html
htttext/webviewhtml
icoimage/x-icon
iefimage/ief
iiiapplication/x-iphone
insapplication/x-internet-signup
ispapplication/x-internet-signup
jfifimage/pipeg
jpeimage/jpeg
jpegimage/jpeg
jpgimage/jpeg
jsapplication/x-javascript
latexapplication/x-latex
lhaapplication/octet-stream
lsfvideo/x-la-asf
lsxvideo/x-la-asf
lzhapplication/octet-stream
m13application/x-msmediaview
m14application/x-msmediaview
m3uaudio/x-mpegurl
manapplication/x-troff-man
mdbapplication/x-msaccess
meapplication/x-troff-me
mhtmessage/rfc822
mhtmlmessage/rfc822
midaudio/mid
mnyapplication/x-msmoney
movvideo/quicktime
movievideo/x-sgi-movie
mp2video/mpeg
mp3audio/mpeg
mpavideo/mpeg
mpevideo/mpeg
mpegvideo/mpeg
mpgvideo/mpeg
mppapplication/vnd.ms-project
mpv2video/mpeg
msapplication/x-troff-ms
mvbapplication/x-msmediaview
nwsmessage/rfc822
odaapplication/oda
p10application/pkcs10
p12application/x-pkcs12
p7bapplication/x-pkcs7-certificates
p7capplication/x-pkcs7-mime
p7mapplication/x-pkcs7-mime
p7rapplication/x-pkcs7-certreqresp
p7sapplication/x-pkcs7-signature
pbmimage/x-portable-bitmap
pdfapplication/pdf
pfxapplication/x-pkcs12
pgmimage/x-portable-graymap
pkoapplication/ynd.ms-pkipko
pmaapplication/x-perfmon
pmcapplication/x-perfmon
pmlapplication/x-perfmon
pmrapplication/x-perfmon
pmwapplication/x-perfmon
pnmimage/x-portable-anymap
pot,application/vnd.ms-powerpoint
ppmimage/x-portable-pixmap
ppsapplication/vnd.ms-powerpoint
pptapplication/vnd.ms-powerpoint
prfapplication/pics-rules
psapplication/postscript
pubapplication/x-mspublisher
qtvideo/quicktime
raaudio/x-pn-realaudio
ramaudio/x-pn-realaudio
rasimage/x-cmu-raster
rgbimage/x-rgb
rmiaudio/mid
roffapplication/x-troff
rtfapplication/rtf
rtxtext/richtext
scdapplication/x-msschedule
scttext/scriptlet
setpayapplication/set-payment-initiation
setregapplication/set-registration-initiation
shapplication/x-sh
sharapplication/x-shar
sitapplication/x-stuffit
sndaudio/basic
spcapplication/x-pkcs7-certificates
splapplication/futuresplash
srcapplication/x-wais-source
sstapplication/vnd.ms-pkicertstore
stlapplication/vnd.ms-pkistl
stmtext/html
svgimage/svg+xml
sv4cpioapplication/x-sv4cpio
sv4crcapplication/x-sv4crc
swfapplication/x-shockwave-flash
tapplication/x-troff
tarapplication/x-tar
tclapplication/x-tcl
texapplication/x-tex
texiapplication/x-texinfo
texinfoapplication/x-texinfo
tgzapplication/x-compressed
tifimage/tiff
tiffimage/tiff
trapplication/x-troff
trmapplication/x-msterminal
tsvtext/tab-separated-values
txttext/plain
ulstext/iuls
ustarapplication/x-ustar
vcftext/x-vcard
vrmlx-world/x-vrml
wavaudio/x-wav
wcmapplication/vnd.ms-works
wdbapplication/vnd.ms-works
wksapplication/vnd.ms-works
wmfapplication/x-msmetafile
wpsapplication/vnd.ms-works
wriapplication/x-mswrite
wrlx-world/x-vrml
wrzx-world/x-vrml
xafx-world/x-vrml
xbmimage/x-xbitmap
xlaapplication/vnd.ms-excel
xlcapplication/vnd.ms-excel
xlmapplication/vnd.ms-excel
xlsapplication/vnd.ms-excel
xltapplication/vnd.ms-excel
xlwapplication/vnd.ms-excel
xofx-world/x-vrml
xpmimage/x-xpixmap
xwdimage/x-xwindowdump
zapplication/x-compress
zipapplication/zip