|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先谈谈我对java的一些认识。我选择java,是因为他语法简单,功能强大,从web,到桌面,到嵌入式,无所不能。但当我进一步了解了java后,感叹,java原来也有许多缺点。成绩Java中的Big/Little成绩
1.办理Endian成绩:一个总结
Java二进制文件中的一切工具都以big-endian情势存在,高字节优先,这偶然被称为收集按次。这是一个好的动静,意味着假如你只利用Java。一切文件在一切平台(Mac,PC,Solaris等)上按一样的体例举行处置。能够自在地互换二进制数据,以电子情势在Internet上,或在软盘上,而无需思索endian成绩。存在的成绩是当你与那些不是利用Java编写的程序互换数据文件时,会存在一些成绩。由于这些程序利用的是little-endian按次,一般是在PC上利用的C言语。有些平台外部利用big-endian字节按次(Mac,IBM390);有些平台利用little-endian字节按次(Intel)。Java对用户坦白了endian成绩。
在二进制文件中,在域之间没有支解符,文件是二进制情势的,不成读的ASCII。假如你想读的数据不是尺度格局,一般由非Java程序筹办的。能够由四种选择:
1).重写供应输出文件的输入程序。它能够间接输入big-endian字撙节DataOutputStream大概字符DataOutputSream格局。
2).写一个自力的翻译程序,读和分列字节。能够用任何言语编写。
3).以字节情势读数据,偏重新布置它们(onthefly)。
4).最复杂的体例是,利用我编写的LEDataInputStream,LEDataOutputStream和LERandomAccessFile摹拟DataInputStream,DataOutputStreamandRandomAccessFile,它们利用的是little-endian字撙节。YoucanreadaboutLEDataStream.Youcandownloadthecodeandsourcefree.YoucangethelpfromtheFileI/OAmanuensistoshowyouhowtousetheclasses.Justtellityouhavelittle-endianbinarydata.
2.你大概乃至不会有任何成绩。
从C来的很多Java老手大概会以为必要思索它们所依附的平台外部所利用的是big仍是little成绩。在Java中这不是一个成绩。进一步,不借助于当地类,你没法晓得它们是怎样存储的。JavahasnostructI/Oandnounionsoranyoftheotherendian-sensitivelanguageconstructs.
仅在与遗留的C/C++使用程序通信时必要思索endian成绩。以下代码在bigorlittleendian呆板上都将发生一样的了局:
//take16-bitshortapartintotwo8-bitbytes.
shortx=0xabcd;
bytehigh=(byte)(x>>>8);
bytelow=(byte)x;/*castimplies&0xff*/
System.out.println("x="+x+"high="+high+"low="+low);
3.读Little-EndianBinaryFiles
Themostcommonproblemisdealingwithfilesstoredinlittle-endianformat.
Ihadtoimplementroutinesparalleltothoseinjava.io.DataInputStreamwhichreadsrawbinary,inmyLEDataInputStreamandLEDataOutputStreamclasses.Dontconfusethiswiththeio.DataInputhuman-readablecharacter-basedfile-interchangeformat.
Ifyouwantedtodoityourself,withouttheoverheadofthefullLEDataInputStreamandLEDataOutputStreamclasses,hereisthebasictechnique:
Presumingyourintegersarein2scomplementlittle-endianformat,shortsareprettyeasytohandle:
--------------------------------------------------------------------------------
shortreadShortLittleEndian()
{
//2bytes
intlow=readByte()&0xff;
inthigh=readByte()&0xff;
return(short)(high<<8|low);
}
Orifyouwanttogetcleverandpuzzleyourreaders,youcanavoidonemasksincethehighbitswilllaterbeshavedoffbyconversionbacktoshort.
shortreadShortLittleEndian()
{
//2bytes
intlow=readByte()&0xff;
inthigh=readByte();
//avoidmaskinghere
return(short)(high<<8|low);
}
--------------------------------------------------------------------------------
Longsarealittlemorecomplicated:
--------------------------------------------------------------------------------
longreadLongLittleEndian()
{
//8bytes
longaccum=0;
for(intshiftBy=0;shiftBy<64;shiftBy+=8)
{
//mustcasttolongorshiftdonemodulo32
accum|=(long)(readByte()&0xff)<<shiftBy;
}
returnaccum;
}
--------------------------------------------------------------------------------
Inasimilarwaywehandlecharandint.
--------------------------------------------------------------------------------
charreadCharLittleEndian()
{
//2bytes
intlow=readByte()&0xff;
inthigh=readByte();
return(char)(high<<8|low);
}
--------------------------------------------------------------------------------
intreadIntLittleEndian()
{
//4bytes
intaccum=0;
for(intshiftBy=0;shiftBy<32;shiftBy+=8)
{
accum|=(readByte()&0xff)<<shiftBy;
}
returnaccum;
}
--------------------------------------------------------------------------------
Floatingpointisalittletrickier.PresumingyourdataisinIEEElittle-endianformat,youneedsomethinglikethis:
--------------------------------------------------------------------------------
doublereadDoubleLittleEndian()
{
longaccum=0;
for(intshiftBy=0;shiftBy<64;shiftBy+=8)
{
//mustcasttolongorshiftdonemodulo32
accum|=((long)(readByte()&0xff))<<shiftBy;
}
returnDouble.longBitsToDouble(accum);
}
--------------------------------------------------------------------------------
floatreadFloatLittleEndian()
{
intaccum=0;
for(intshiftBy=0;shiftBy<32;shiftBy+=8)
{
accum|=(readByte()&0xff)<<shiftBy;
}
returnFloat.intBitsToFloat(accum);
}
--------------------------------------------------------------------------------
YoudontneedareadByteLittleEndiansincethecodewouldbeidenticaltoreadByte,thoughyoumightcreateonejustforconsistency:
--------------------------------------------------------------------------------
bytereadByteLittleEndian()
{
//1byte
returnreadByte();
}
--------------------------------------------------------------------------------
4.History
InGulliverstravelstheLilliputianslikedtobreaktheireggsonthesmallendandtheBlefuscudiansonthebigend.Theyfoughtwarsoverthis.Thereisacomputeranalogy.Shouldnumbersbestoredmostorleastsignificantbytefirst?Thisissometimesreferredtoasbytesex.
Thoseinthebig-endiancamp(mostsignificantbytestoredfirst)includetheJavaVMvirtualcomputer,theJavabinaryfileformat,theIBM360andfollow-onmainframessuchasthe390,andtheMotorola68Kandmostmainframes.ThePowerPCisendian-agnostic.
Blefuscudians(big-endians)assertthisisthewayGodintendedintegerstobestored,mostimportantpartfirst.Atanassemblerlevelfieldsofmixedpositiveintegersandtextcanbesortedasifitwereonebigtextfieldkey.Realprogrammersreadhexdumps,andbig-endianisaloteasiertocomprehend.
Inthelittle-endiancamp(leastsignificantbytefirst)aretheIntel8080,8086,80286,PentiumandfollowonsandtheAMD6502popularisedbytheApple][.
Lilliputians(little-endians)assertthatputtingtheloworderpartfirstismorenaturalbecausewhenyoudoarithmeticmanually,youstartattheleastsignificantpartandworktowardthemostsignificantpart.Thisorderingmakeswritingmulti-precisionarithmeticeasiersinceyouworkupnotdown.Itmadeimplementing8-bitmicroprocessorseasier.Attheassemblerlevel(notinJava)italsoletsyoucheatandpassaddressesofa32-bitpositiveintstoaroutineexpectingonlya16-bitparameterandstillhaveitwork.Realprogrammersreadhexdumps,andlittle-endianismoreofastimulatingchallenge.
Ifamachineiswordaddressable,withnofineraddressingsupported,theconceptofendiannessmeansnothingsincewordsarefetchedfromRAMinparallel,bothendsfirst.
5.WhatSexIsYourCPU?
ByteSexEndiannessofCPUs
CPU
EndiannessNotes
AMD6502,Duron,Athlon,Thunderird
little
6502wasusedintheApple][,theDuron,AthlonandThunderbirdinWindows95/08/ME/NT/2000/XP
Apple][6502
little
AppleMac68000
big
UsesMotorola68000
ApplePowerPC
big
CPUisbisexualbutstaysbigintheMacOS.
Burroughs1700,1800,1900
?
bitaddressable.Useddifferentinterpreterfirmwareinstructionsetsforeachlanguage.
Burroughs7800
?
Algolmachine
CDCLGP-30
word-addressableonly,hencenoendianness
31 |
|