«á¤@­¶
«e¤@­¶
¦^¥Ø¿ý
¦^­º­¶
²Ä¤G¤Q³¹¡@¶}µoDelphiª«¥ó¦¡¼Æ¾ÚºÞ²z¥\¯à¡]¤@¡^

¡@¡@­±¦Vª«¥ó§Þ³N¬O¤E¤Q¦~¥Nªº¥D¬y§Þ³N¡A¦UÃþÀ³¥Î³nÅé¦pªG¥H­±¦Vª«¥óªº¤èªkºc³y¨Ã¥Bº¯³z­±¦Vª«¥óªº­·®æ±N¨Ï³nÅé¨ã¦³§ó°ªªº«~½è¡C¦b­±¦Vª«¥óµ{¦¡³]­p¤¤¡Aª«¥ó¦¡¼Æ¾ÚºÞ²z¦û¦³«Ü­«­nªº¦a¦ì¡C¦bDelphi¤¤¡A¹ïª«¥ó¦¡¼Æ¾ÚºÞ²zªº¤ä´©¤è¦¡¬O¨ä¤@¤j¯S¦â¡C

Delphi¬O¤@­Ó­±¦Vª«¥óªº¥iµø¤Æ³]­p»P­±¦Vª«¥óªº»y¨¥¬Ûµ²¦Xªº¾ã¦X¶}µoÀô¹Ò¡CDelphiªº®Ö¤ß¬O³¡¥ó¡C³¡¥ó¬Oª«¥óªº¤@ºØ¡CDelphiÀ³¥Îµ{¦¡§¹¥þ¬O¥Ñ³¡¥ó¨Óºc³yªº¡A¦]¦¹¶}µo°ª©Ê¯àªºDelphiÀ³¥Îµ{¦¡¥²µM·|¯A¤Îª«¥ó¦¡¼Æ¾ÚºÞ²z§Þ³N¡C

ª«¥ó¦¡¼Æ¾ÚºÞ²z¥]¬A¨â¤è­±ªº¤º®e¡G

¡´ ¥Îª«¥ó¨ÓºÞ²z¼Æ¾Ú

¡´ ¹ï¦UÃþ¼Æ¾Úª«¥ó¡]¥]¬Aª«¥ó©M³¡¥ó¡^ªººÞ²z

¡@

Delphi¦b³o¨â¤è­±³£°µªº¬Û·í¥X¦â¡C¦bDelphiªº¦­´Áª©¥»Turbo Pascal ¤¤´N´¿¦³¬y(Stream)¡B¸s(Collection)©M¸ê·½(Resource)µ¥±Mªù¥Î©óª«¥ó¦¡¼Æ¾ÚºÞ²zªºÃþ¡C¦bDelphi¤¤¡A³o¨Ç¥\¯à±o¨ì¤F¤j¤jªº¥[±j¡CDelphi±Nª«¥ó¦¡¼Æ¾ÚºÞ²zÃþÂkµ²¬°Streamª«¥ó(Stream)©MFilerª«¥ó(Filer)¡A¨Ã±N¥¦­ÌÀ³¥Î©ó¥iµø³¡¥óÃþ®w¡]VCL¡^ªº¤è¤è­±­±¡C¥¦­Ì¤£¶È´£¨Ñ¤F¦b°O¾ÐÅé¡B¥~¦s©MWindows¸ê·½¤¤ºÞ²zª«¥óªº¥\¯à¡AÁÙ´£¨Ñ¤F¦b¸ê®Æ®wBLOBÄæ¦ì¤¤ª«¥óªº¥\¯à¡C

¡@¡@¦b¥»³¹¤¤±N¤¶²ÐStreamª«¥ó©MFilerª«¥óªº¹ê²{­ì²z¡BÀ³¥Î¤èªk¥H¤Î¦b¶W´CÅé¨t²Î¤¤ªºÀ³¥Î¡C³o¹ï©ó¹B¥ÎDelphi ¶}µo¶i¶¥À³¥Î¬O«Ü­«­nªº¡C

¡@

20.1 ¬y¦¡ª«¥óªº¹ê²{­ì²z©MÀ³¥Î

¡@

¡@¡@Streamª«¥ó¡A¤SºÙ¬y¦¡ª«¥ó¡A¬OTStream¡BTHandleStream¡BTFileStream¡BTMemoryStream¡BTResourceStream©MTBlobStreamµ¥ªº²ÎºÙ¡C¥¦­Ì¤À§O¥Nªí¤F¦b¦UºØ´C¤¶¤W¦sÀx¼Æ¾Úªº¯à¤O¡A¥¦­Ì±N¦UºØ¼Æ¾Ú«¬¦¡(¥]¬Aª«¥ó©M³¡¥ó) ¦b°O¾ÐÅé¡B¥~¦s©M¸ê®Æ®wÄæ¦ì¤¤ªººÞ²z¾Þ§@©â¶H¬°ª«¥ó¤èªk¡A¨Ã¥B¥R¤À§Q¥Î¤F­±¦Vª«¥ó§Þ³NªºÀuÂI¡AÀ³¥Îµ{¦¡¥i¥H¬Û·í®e©ö¦a¦b¦UºØStreamª«¥ó¤¤«þ¨©¼Æ¾Ú¡C

¡@¡@¤U­±¤¶²Ð¦UºØª«¥óªº¼Æ¾Ú©M¤èªk¤Î¨Ï¥Î¤èªk¡C

¡@

20.1.1 TStreamª«¥ó

¡@

¡@¡@TStreamª«¥ó¬O¯à¦b¦UºØ´C¤¶¤¤¦sÀx¤G¶i¨î¼Æ¾Úªºª«¥óªº©â¶Hª«¥ó¡C±qTStream ª«¥óÄ~©Óªºª«¥ó¥Î©ó¦b°O¾ÐÅé¡BWindows¸ê·½¤å¥ó¡BºÏ¤ùÀÉ©M¸ê®Æ®wÄæ¦ìµ¥´C¤¶¤¤¦sÀx¼Æ¾Ú¡C

¡@¡@TStream¤¤©w¸q¤F¨â­ÓÄÝ©Ê¡GSize©MPosition¡C¥¦­Ì¤À§O¥H¦ì¤¸¸s²Õ¬°³æ¦ìªí¥Üªº¬yªº¤j¤p©M¥Ø«e«ü¼Ð¦ì¸m¡CTStream¤¤©w¸qªº¤èªk¥Î©ó¦b¦UºØ¬y¤¤Åª¡B¼g©M¬Û¤¬«þ¨©¤G¶i¨î¼Æ¾Ú¡C¦]¬°©Ò¦³ªºStreamª«¥ó³£¬O±qTStream¤¤Ä~©Ó¨Óªº¡A©Ò¥H¦bTStream¤¤©w¸qªºÄæ¦ì©M¤èªk³£¯à³QStreamª«¥ó©I¥s©M¦s¨ú¡C¦¹¥~¡A¤S¥Ñ©ó­±¦Vª«¥ó§Þ³Nªº°ÊºAÁp½s¥\¯à¡ATStream¬°¦UºØ¬yªºÀ³¥Î´£¨Ñ¤F²Î¤@ªº±µ¤f¡A²¤Æ¤F¬yªº¨Ï¥Î¡F¤£¦PStreamª«¥ó¬O©â¶H¤F¹ï¤£¦P¦sÀx´C¤¶ªº¼Æ¾Ú¤Wªº¾Þ§@¡A¦]¦¹¡ATStreamªº»Ý¤èªk¬°¦b¤£¦P´C¤¶¶¡ªº¼Æ¾Ú«þ¨©´£¨Ñ¤F³Ì²±¶ªº¤â¬q¡C

¡@

20.1.1.1 TStreamªºÄÝ©Ê©M¤èªk

¡@

¡@¡@1. PositionÄÝ©Ê¡@

Án©ú¡Gproperty Position: Longint;

¡@¡@PositionÄÝ©Ê«ü©ú¬y¤¤Åª¼gªº¥Ø«e°¾²¾¶q¡C

¡@¡@2. SizeÄÝ©Ê

¡@¡@Án©ú¡Gproperty Size: Longint;

SizeÄÝ©Ê«ü©ú¤F¥H¦ì¤¸¸s²Õ¬°³æ¦ìªº¬yªºªº¤j¤p¡A¥¦¬O°ßŪªº¡C

¡@¡@3. CopyFrom¤èªk

¡@¡@Án©ú¡Gfunction CopyFrom(Source: TStream; Count: Longint): Longint;

CopyFrom±qSource©Ò«ü©wªº¬y¤¤«þ¨©Count­Ó¦ì¤¸¸s²Õ¨ì¥Ø«e¬y¤¤¡A ¨Ã±N«ü¼Ð±q¥Ø«e¦ì¸m²¾°ÊCount­Ó¦ì¤¸¸s²Õ¼Æ¡A¨ç¼Æªð¦^­È¬O¹ê»Ú«þ¨©ªº¦ì¤¸¸s²Õ¼Æ¡C

¡@¡@4. Read¤èªk

¡@¡@Án©ú¡Gfunction Read(var Buffer; Count: Longint): Longint; virtual; abstract;

Read¤èªk±q¥Ø«e¬y¤¤ªº¥Ø«e¦ì¸m°_±NCount­Ó¦ì¤¸¸s²Õªº¤º®e´_¨î¨ìBuffer¤¤¡A¨Ã§â¥Ø«e«ü¼Ð¦V«á²¾°ÊCount­Ó¦ì¤¸¸s²Õ¼Æ¡A¨ç¼Æªð¦^­È¬O¹ê»ÚŪªº¦ì¤¸¸s²Õ¼Æ¡C¦pªGªð¦^­È¤p©óCount¡A³o·N¨ýµÛŪ¾Þ§@¦bŪº¡©Ò»Ý¦ì¤¸¸s²Õ¼Æ«e«ü¼Ð¤w¸g¨ì¹F¤F¬yªº§À³¡¡C

¡@¡@Read¤èªk¬O©â¶H¤èªk¡C¨C­Ó«áÄ~Streamª«¥ó³£­n®Ú¾Ú¦Û¤v¯S¦³ªº¦³Ãö¯S©w¦sÀx´C¤¶ªºÅª¾Þ§@Âл\¸Ó¤èªk¡C¦Ó¥B¬yªº©Ò¦³¨ä¥¦ªºÅª¼Æ¾Úªº¤èªk¡]¦p¡GReadBuffer¡AReadComponentµ¥¡^¦b§¹¦¨¹ê»ÚªºÅª¾Þ§@®É³£©I¥s¤FRead¤èªk¡C­±¦Vª«¥óªº°ÊºAÁp½sªºÀuÂI´NÅé²{¦b³o¨à¡C¦]¬°«áÄ~Streamª«¥ó¥u»ÝÂл\Read¤èªk¡A¦Ó¨ä¥¦Åª¾Þ§@(¦pReadBuffer¡BReadComponentµ¥)³£¤£»Ý­n­«·s©w¸q¡A¦Ó¥BTStreamÁÙ´£¨Ñ¤F²Î¤@ªº±µ¤f¡C

¡@¡@5. ReadBuffer¤èªk

¡@¡@Án©ú¡Gprocedure ReadBuffer(var Buffer; Count: Longint);

¡@¡@ReadBuffer¤èªk±q¬y¤¤±NCount­Ó¦ì¤¸¸s²Õ´_¨î¨ìBuffer ¤¤¡A ¨Ã±N¬yªº¥Ø«e«ü¼Ð¦V«á²¾°ÊCount­Ó¦ì¤¸¸s²Õ¡C¦pŪ¾Þ§@¶W¹L¬yªº§À³¡¡AReadBuffer¤èªk¤Þ°_EReadError²§±`¨Æ¥ó¡C

¡@¡@6. ReadComponent¤èªk

¡@ Án©ú¡Gfunction ReadComponent(Instance: TComponent): TComponent;

ReadComponent¤èªk±q¥Ø«e¬y¤¤Åª¨ú¥ÑInstance©Ò«ü©wªº³¡¥ó¡A¨ç¼Æªð¦^©ÒŪªº³¡¥ó¡CReadComponent¦bŪInstance¤Î¨ä¾Ö¦³ªº©Ò¦³ª«¥ó®É«Ø¥ß¤F¤@­ÓReaderª«¥ó¨Ã©I¥s¥¦ªºReadRootComponent¤èªk¡C

¡@¡@¦pªGInstance¬°nil¡AReadComponentªº¤èªk°ò©ó¬y¤¤´y­zªº³¡¥ó«¬¦¡¸ê°T«Ø¥ß³¡¥ó¡A¨Ãªð¦^·s«Ø¥ßªº³¡¥ó¡C

¡@¡@7. ReadComponentRes¤èªk

¡@¡@Án©ú¡Gfunction ReadComponentRes(Instance: TComponent): TComponent;

ReadComponentRes¤èªk±q¬y¤¤Åª¨úInstance«ü©wªº³¡¥ó¡A¦ý¬O¬yªº¥Ø«e¦ì¸m¥²¶·¬O¥ÑWriteComponentRes¤èªk©Ò¼g¤Jªº³¡¥óªº¦ì¸m¡C

¡@¡@ReadComponentRes ­º¥ý©I¥sReadResHeader¤èªk±q¬y¤¤Åª¨ú¸ê·½ÀY¡AµM«á©I¥sReadComponent¤èªkŪ¨úInstance¡C¦pªG¬yªº¥Ø«e¦ì¸m¤£¥]§t¤@­Ó¸ê·½ÀY¡CReadResHeader±N¤Þµo¤@­ÓEInvalidImage²§±`¨Æ¥ó¡C¦bClasses®w³æ¤¸¤¤¤]¥]§t¤@­Ó¦W¬°ReadComponentResªº¨ç¼Æ¡A¸Ó¨ç¼Æ°õ¦æ¬Û¦Pªº¾Þ§@¡A¥u¤£¹L¥¦°ò©óÀ³¥Îµ{¦¡¥]§tªº¸ê·½«Ø¥ß¦Û¤vªº¬y¡C

¡@¡@8. ReadResHeader¤èªk

¡@¡@Án©ú¡Gprocedure ReadResHeader;

ReadResHeader¤èªk±q¬yªº¥Ø«e¦ì¸mŪ¨úWindows¸ê·½¤å¥óÀY¡A¨Ã±N¬yªº¥Ø«e¦ì¸m«ü¼Ð²¾¨ì¸Ó¤å¥óÀYªº§À³¡¡C¦pªG¬y¤£¥]§t¤@­Ó¦³®Äªº¸ê·½¤å¥óÀY¡AReadResHeader±N¤Þµo¤@­ÓEInvalidImage²§±`¨Æ¥ó¡C

¡@¡@¬yªºReadComponentRes¤èªk¦b±q¸ê·½¤å¥ó¤¤Åª¨ú³¡¥ó¤§«e¡A·|¦Û°Ê©I¥sReadResHeader¤èªk¡A¦]¦¹¡A³q±`µ{¦¡­û³q±`¤£»Ý­n¦Û¤v©I¥s¥¦¡C

¡@¡@9. Seek¤èªk

¡@¡@Án©ú¡Gfunction Seek(Offset: Longint; Origin: Word): Longint; virtual; abstract;

Seek¤èªk±N¬yªº¥Ø«e«ü¼Ð²¾°ÊOffset­Ó¦ì¤¸¸s²Õ¡A¦ì¤¸¸s²Õ²¾°Êªº°_ÂI¥ÑOrigin«ü©w¡C¦pªGOffset¬O­t¼Æ¡ASeek¤èªk±N±q©Ò´y­zªº°_ÂI©¹¬yªºÀY³¡²¾°Ê¡C¤Uªí¤¤¦C¥X¤FOriginªº¤£¦P¨ú­È©M¥¦­Ìªº§t¸q¡G

¡@

ªí20.1 ¨ç¼ÆSeekªº°Ñ¼Æªº¨ú­È

¡@¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

¡@¡@±`¼Æ¡@¡@¡@¡@¡@¡@¡@­È¡@¡@ ¡@¡@¡@Seekªº°_ÂI Offsetªº¨ú­È

¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X

¡@SoFromBeginning 0 ¡@¬yªº¶}ÀY ¥¿ ¼Æ

¡@SoFromCurrent 1 ¬yªº¥Ø«e¦ì¸m ¥¿¼Æ©Î­t¼Æ

¡@SoFromEnd 2 ¬yªºµ²§À ­t ¼Æ

¡@¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

¡@

¡@ 10. Write¤èªk

¡@¡@¦bDelphiª«¥ó¦¡ºÞ²zªºª«¥ó¤¤¦³¨âÃþª«¥óªº¤èªk³£¦³ºÙ¬°Writeªº¡GStreamª«¥ó©MFilerª«¥ó¡CStreamª«¥óªºWrite¤èªk±N¼Æ¾Ú¼g¶i¬y¤¤¡CFilerª«¥ó³q¹L¬ÛÃöªº¬y¶Ç»¼¼Æ¾Ú¡A¦b«á¤å¤¤·|¤¶²Ð³oÃþ¤èªk¡C

¡@¡@Streamª«¥óªºWrite¤èªkÁn©ú¦p¤U¡G

¡@

function Write(const Buffer; Count: Longint): Longint; virtual; abstract;

¡@

Write¤èªk±NBuffer¤¤ªºCount­Ó¦ì¤¸¸s²Õ¼g¤J¬y¤¤¡A¨Ã±N¥Ø«e¦ì¸m«ü¼Ð¦V¬yªº§À³¡²¾°ÊCount­Ó¦ì¤¸¸s²Õ¡A¨ç¼Æªð¦^¼g¤Jªº¦ì¤¸¸s²Õ¼Æ¡C

¡@ TStreamªºWrite¤èªk¬O©â¶Hªº¡A¨C­ÓÄ~©ÓªºStreamª«¥ó³£­n³q¹LÂл\¸Ó¤èªk¨Ó´£¨Ñ¦V¯S©w¦sÀx´C¤¶(°O¾ÐÅé¡BºÏ¤ùÀɵ¥)¼g¼Æ¾Úªº¯S©w¤èªk¡C¬yªº¨ä¥¦©Ò¦³¼g¼Æ¾Úªº¤èªk(¦pWriteBuffer¡BWriteComponent)³£©I¥sWrite¾á·í¹ê»Úªº¼g¾Þ§@¡C

¡@¡@11. WriteBuffer¤èªk

¡@¡@Án©ú¡Gprocedure WriteBuffer(const Buffer; Count: Longint);

¡@¡@WriteBufferªº¥\¯à»PWrite¬Û¦ü¡CWriteBuffer¤èªk©I¥sWrite¨Ó°õ¦æ¹ê»Úªº¼g¾Þ§@¡A¦pªG¬y¨S¯à¼g©Ò¦³¦ì¤¸¸s²Õ¡AWriteBuffer·|IJµo¤@­ÓEWriteError²§±`¨Æ¥ó¡C

¡@¡@12. WriteComponent¤èªk

¡@¡@¦bStreamª«¥ó©MFilerª«¥ó³£¦³³QºÙ¬°WriteComponentªº¤èªk¡CStreamª«¥óªºWriteComponent¤èªk±NInstance©Ò«ü©wªº³¡¥ó©M¥¦©Ò¥]§tªº©Ò¦³³¡¥ó³£¼g¤J¬y¤¤¡FWriterª«¥óªºWriteComponent±N«ü©w³¡¥óªºÄݩʭȼg¤JWriterª«¥óªº¬y¤¤¡C

¡@¡@Streamª«¥óªºWriteComponent¤èªkÁn©ú¬O³o¼Ëªº¡G

procedure WriteComponent(Instance: Tcomponent);

¡@

¡@¡@WriteComponent«Ø¥ß¤@­ÓWriterª«¥ó¡A¨Ã©I¥sWriterªºWriteRootComponent¤èªk±NInstance¤Î¨ä¾Ö¦³ªºª«¥ó¼g¤J¬y¡C

¡@¡@13. WriteComponentRes¤èªk

¡@¡@Án©ú¡GWriteComponentRes(const ResName: String; Instance: TComponent);

¡@¡@WriteComponentRes¤èªk­º¥ý©¹¬y¤¤¼g¤J¼Ð·ÇWindows ¸ê·½¤å¥óÀY¡AµM«á±NInstance«ü©wªº³¡¥ó¼g¤J¬y¤¤¡C­nŪ¥ÑWriteComponentRes¼g¤Jªº³¡¥ó¡A¥²¶·©I¥sReadComponentRes¤èªk¡C

¡@¡@WriteComponentRes¨Ï¥ÎResName¶Ç¤Jªº¦r¤¸¦ê§@¬°¸ê·½¤å¥óÀYªº¸ê·½¦W¡AµM«á©I¥sWriteComponent¤èªk±NInstance©M¥¦¾Ö¦³ªº³¡¥ó¼g¤J¬y¡C

¡@¡@14. WriteDescendant¤èªk

¡@¡@Án©ú¡Gprocedure WriteDescendant(Instance Ancestor: TComponent);

¡@¡@Streamª«¥óªºWriteDescendant¤èªk«Ø¥ß¤@­ÓWriterª«¥ó¡AµM«á½Õ¤J¸Óª«¥óªºWriteDescendant¤èªk±NInstance³¡¥ó¼g¤J¬y¤¤¡CInstance¥i¥H¬O±qAncestor³¡¥óÄ~©Óªºµ¡Åé¡A¤]¥i¥H¬O¦b±q¯ª¥ýµ¡Å餤Ä~©Óªºµ¡Å餤¬ÛÀ³©ó¯ª¥ýµ¡Å餤Ancestor³¡¥óªº³¡¥ó¡C

¡@¡@15. WriteDescendantRes¤èªk

¡@¡@Án©ú¡Gprocedure WriteDescendantRes(const ResName: String;

Instance, Ancestor: TComponent);

¡@¡@WriteDescendantRes¤èªk±NWindows¸ê·½¤å¥óÀY¼g¤J¬y¡A¨Ã¨Ï¥ÎResName§@¥Î¸ê·½¦W¡AµM«á©I¥sWriteDescendant¤èªk¡A±NInstance¼g¤J¬y¡C

¡@

20.1.1.2 TStreamªº¹ê²{­ì²z

¡@

¡@¡@TStreamª«¥ó¬OStreamª«¥óªº°ò¦Ãþ¡A³o¬OStreamª«¥óªº°ò¦¡C¬°¤F¯à¦b¤£¦P´C¤¶¤Wªº¦sÀx¼Æ¾Úª«¥ó¡A«áÄ~ªºStreamª«¥ó¥D­n¬O¦bRead©MWrite¤èªk¤W°µ¤F§ï¶i¡A¡C¦]¦¹¡A¤F¸ÑTStream¬O´x´¤Streamª«¥óºÞ²zªº®Ö¤ß¡CBorland¤½¥qÁöµM´£¨Ñ¤FStreamª«¥óªº±µ¤f±Ô­z¤åÀÉ¡A¦ý¹ï©ó¨ä¹ê²{©MÀ³¥Î¤èªk«o¨S¦³´£¤Î¡Aµ§ªÌ¬O±qBorland Delphi 2.0 Client/Server Suite ´£¨Ñªº·½¥N½X©M³¡¤À¨Ò¤lµ{¦¡¤¤´x´¤¤F¬y¦¡ª«¥ó§Þ³N¡C

¡@¡@¤U­±´N±qTStreamªºÄÝ©Ê©M¤èªkªº¹ê²{¶}©l¡C

¡@¡@1. TStreamÄݩʪº¹ê²{

¡@¡@«e­±¤¶²Ð¹L¡ATStream¨ã¦³Position©MSize¨â­ÓÄÝ©Ê¡A§@¬°©â¶H¼Æ¾Ú«¬¦¡¡A¥¦©â¶H¤F¦b¦UºØ¦sÀx´C¤¶¤¤Åª¼g¼Æ¾Ú©Ò»Ý­n¸g±`¦s¨úªºÄæ¦ì¡C¨º»ò¥¦­Ì¬O«ç¼Ë¹ê²{ªº©O¡H

¡@¡@¦b¦Û©w¸q³¡¥ó½s¼g³o¤@³¹¤¤¤¶²Ð¹L³¡¥óÄÝ©Ê©w¸q¤¤ªºÅª¼g±±¨î¡CPosition©MSize¤]§@¤FŪ¼g±±¨î¡C©w¸q¦p¤U¡G

¡@

property Position: Longint read GetPosition write SetPosition;

property Size: Longint read GetSize;

¡@

¡@¡@¥Ñ¤W¥iª¾¡APosition¬O¥iŪ¼gÄÝ©Ê¡A¦ÓSize¬O°ßŪªº¡C

¡@¡@PositionÄݩʪº¹ê²{´NÅé²{¦bGetPosition©MSetPosition¡C·í¦bµ{¦¡°õ¦æ¹Lµ{¤¤¡A¥ô¦óŪ¨úPositionªº­È©Mµ¹Position½á­Èªº¾Þ§@³£·|¦Û°ÊIJµo¨p¦³¤èªkGetPosition©MSetPosition¡C¨â­Ó¤èªkªºÁn©ú¦p¤U¡G

¡@

function TStream.GetPosition: Longint;

begin

Result := Seek(0, 1);

end;

¡@

procedure TStream.SetPosition(Pos: Longint);

begin

Seek(Pos, 0);

end;

¡@

¦b³]©w¦ì¸m®É¡ADelphi½sĶ¾÷¨î·|¦Û°Ê±NPosition¶Ç¬°Pos¡C

¡@¡@«e­±¤¶²Ð¹LSeekªº¨Ï¥Î¤èªk¡A²Ä¤@°Ñ¼Æ¬O²¾°Ê°¾²¾¶q¡A²Ä¤G­Ó°Ñ¼Æ¬O²¾°Êªº°_ÂI¡Aªð¦^­È¬O²¾°Ê«áªº«ü¼Ð¦ì¸m¡C

¡@¡@SizeÄݩʪº¹ê²{¥u¦³Åª±±¨î¡A§¹¥þ«Ì½ª¤F¼g¾Þ§@¡CŪ±±¨î¤èªkGetSize¹ê²{¦p¤U¡G

¡@

function TStream.GetSize: Longint;

var

Pos: Longint;

begin

Pos := Seek(0, 1);

Result := Seek(0, 2);

Seek(Pos, 0);

end;

¡@

2. TStream¤èªkªº¹ê²{

¡@¡@Æ« CopyFrom¤èªk

¡@¡@CopyFrom¬OStreamª«¥ó¤¤«Ü¦³¥Îªº¤èªk¡A¥¦¥Î©ó¦b¤£¦P¦sÀx´C¤¶¤¤«þ¨©¼Æ¾Ú¡C¨Ò¦p¡A°O¾ÐÅé»P¥~³¡¤å¥ó¤§¶¡¡B°O¾ÐÅé»P¸ê®Æ®wÄæ¦ì¤§¶¡µ¥¡C¥¦Â²¤Æ¤F³\¦h°O¾ÐÅé¤À°t¡B¤å¥ó¥´¶}©MŪ¼gµ¥ªº²Ó¸`¡A±N©Ò¦³«þ¨©¾Þ§@³£²Î¤@¨ìStreamª«¥ó¤W¡C

¡@¡@«e­±´¿¤¶²Ð¡GCopyFrom¤èªk±aSource©MCount¨â­Ó°Ñ¼Æ¨Ãªð¦^ªø¾ã«¬¡C¸Ó¤èªk±NCount­Ó¦ì¤¸¸s²Õªº¤º®e±qSource«þ¨©¨ì¥Ø«e¬y¤¤¡A¦pªGCount­È¬°0«h«þ¨©©Ò¦³¼Æ¾Ú¡C

¡@

function TStream.CopyFrom(Source: TStream; Count: Longint): Longint;

const

MaxBufSize = $F000;

var

BufSize, N: Integer;

Buffer: PChar;

begin

if Count = 0 then

begin

Source.Position := 0;

CouNG="ZH-CN">¸ê·½¤å¥ó¤¤ªº³¡¥ó®É©I¥s¡A³q±`µ{¦¡­û¤£»Ý¦Û¤v©I¥s¡C¦pªGŪ¨úªº¤£¬O¸ê·½¤å¥óReadResHeader¡A±NIJµo²§±`¨Æ¥ó¡C

¡@

procedure TStream.ReadResHeader;

var

ReadCount: Longint;

Header: array[0..79] of Char;

begin

FillChar(Header, SizeOf(Header), 0);

ReadCount := Read(Header, SizeOf(Header) - 1);

if (Byte((@Header[0])^) = $FF) and (Word((@Header[1])^) = 10) then

Seek(StrLen(Header + 3) + 10 - ReadCount, 1)

else

raise EInvalidImage.CreateRes(SInvalidImage);

end;

¡@

¡@¡@ReadComponentRes¦bWindows¸ê·½¤å¥ó¤¤Åª¨ú³¡¥ó¡A¬°¤F§PÂ_¬O§_¬O¸ê·½¤å¥ó¡A¥¦­º¥ý©I¥sReadResHeader¤èªk¡AµM«á©I¥sReadComponent¤èªkŪ¨úInstance«ü©wªº³¡¥ó¡C¤U­±¬O¥¦ªº¹ê²{¡G

¡@

function TStream.ReadComponentRes(Instance: TComponent): TComponent;

begin

ReadResHeader;

Result := ReadComponent(Instance);

end;

¡@

¡@»PReadComponentRes¬ÛÀ³ªº¼g¤èªk¬OWriteComponentRes¡ADelphi ©I¥s³o¨â­Ó¤èªkŪ¼gµ¡Åé¤å¥ó(DFM¤å¥ó)¡A¦b«á­±®Ñ¤¤·|Á|¥Î³o¨â­Ó¤èªkŪ¨úDFM¤å¥óªº¨Ò¤l¡C

¡@¡@Æ® WriteComponent©MWriteDescendant¤èªk

¡@¡@Streamª«¥óªºWriteDescendant¤èªk¦b¹ê²{¹Lµ{¤¤¡A«Ø¥ß¤FTWriterª«¥ó¡AµM«á§Q¥ÎTWriterªºWriteDescendant¤èªk±NInstance¼g¤J¬y¡C¦ÓWriteComponent¤èªk¥u¬O²³æ¦a©I¥sWriteDescendant¤èªk±NInstance¼g¤J¬y¡C¥¦­Ìªº¹ê²{¦p¤U¡G

¡@¡@

procedure TStream.WriteComponent(Instance: TComponent);

begin

WriteDescendent(Instance, nil);

end;

¡@

procedure TStream.WriteDescendent(Instance, Ancestor: TComponent);

var

Writer: TWriter;

begin

Writer := TWriter.Create(Self, 4096);

try

Writer.WriteDescendent(Instance, Ancestor);

finally

Writer.Free;

end;

end;

¡@

¡@¡@Ư WriteDescendantRes©MWriteComponentRes¤èªk

¡@¡@WriteDescendantRes¤èªk¥Î©ó±N³¡¥ó¼g¤JWindows¸ê·½¤å¥ó¡F¦ÓWriteComponentRes ¤èªk¥u¬O²³æ¦a©I¥sWriteDescendantRes¤èªk¡A¥¦­Ìªº¹ê²{¦p¤U¡G

¡@

procedure TStream.WriteComponentRes(const ResName: string; Instance:

TComponent);

begin

WriteDescendentRes(ResName, Instance, nil);

end;

¡@

procedure TStream.WriteDescendentRes(const ResName: string; Instance,

Ancestor: TComponent);

var

HeaderSize: Integer;

Origin, ImageSize: Longint;

Header: array[0..79] of Char;

begin

Byte((@Header[0])^) := $FF;

Word((@Header[1])^) := 10;

HeaderSize := StrLen(StrUpper(StrPLCopy(@Header[3], ResName, 63))) + 10;

Word((@Header[HeaderSize - 6])^) := $1030;

Longint((@Header[HeaderSize - 4])^) := 0;

WriteBuffer(Header, HeaderSize);

Origin := Position;

WriteDescendent(Instance, Ancestor);

ImageSize := Position - Origin;

Position := Origin - 4;

WriteBuffer(ImageSize, SizeOf(Longint));

Position := Origin + ImageSize;

end;

¡@

¡@¡@WriteCompnentRes¬O»PReadComponentRes¬ÛÀ³ªºª«¥ó¼g¤èªk¡A³o¨â­Ó¤èªk¬Û¤¬°t¦X¥iŪ¨úDelphiªºDFM¤å¥ó¡A±q¦Ó§Q¥ÎDelphi¨t²Îªº¥\¯à¡C

¡@

20.1.2 THandleStreamª«¥ó

¡@

¡@¡@THandleStreamª«¥óªº¦æ¬°¯S§O¶HFileStreamª«¥ó¡A©Ò¤£¦Pªº¬O¥¦³q¹L¤w«Ø¥ßªº¤å¥ó¥y¬`¦Ó¤£¬OÀÉ®×¼Ðñ¨Ó¦sÀx¬y¤¤ªº¼Æ¾Ú¡C

¡@¡@THandleStreamª«¥ó©w¸q¤FHandleÄÝ©Ê¡A¸ÓÄÝ©Ê´£¨Ñ¤F¹ï¤å¥ó¥y¬`ªº°ßŪ¦s¨ú¡A¨Ã¥BHandleÄÝ©Ê¥i¥H§@¬°DelphiªºRTL¤å¥óºÞ²z¨ç¼Æªº°Ñ¼Æ¡A§Q¥Î¤å¥óÃþ¨ç¼Æ¨ÓŪ¼g¼Æ¾Ú¡CTHandleStreamÂл\¤Fºc³y¨ç¼ÆCreate¡A¸Ó¨ç¼Æ±a¦³Handle °Ñ¼Æ¡A¸Ó°Ñ¼Æ«ü©w»PTHandleStreamª«¥ó¬ÛÃöªº¤å¥ó¥y¬`¡C

¡@

20.1.2.1 THandleStreamªºÄݩʪº¤èªk¡G

¡@

¡@¡@1. HandleÄÝ©Ê

¡@¡@Án©ú¡Gproperty Handle: Integer;

HandleÄÝ©Ê´£¨Ñ¤F¹ï¤å¥ó¥y¬`ªº°ßŪ¦s¨ú¡A¸Ó¥y¬`¥ÑTHandleStreamªººc³y¤èªkCreate¶Ç¤J¡C¦]¦¹°£¤F¥ÎTHandleStream´£¨Ñªº¤èªk¥~¡A¤]¥i¥H¥Î¤å¥óºÞ²z¨ç¼Æ¹ï¥y¬`¶i¦æ¾Þ§@¡C¹ê»Ú¤W¡ATHandleStreamªº¤èªk¦b¹ê²{¤W¤]¬O¹B¥Î¤å¥óºÞ²z¨ç¼Æ¶i¦æ¹ê»ÚªºÅª¼g¾Þ§@¡C

¡@¡@2. Create¤èªk

¡@¡@Án©ú¡Gconstructor Create(AHandle: Integer);

¡@¡@Create¤èªk¨Ï¥Î¶Ç¤JªºHandle°Ñ¼Æ«Ø¥ß¤@­Ó»P¯S©w¤å¥ó¥y¬`¬ÛÁpªºTHandleStreamª«¥ó¡A¨Ã¥B±NAHandle½áµ¹¬yªºHandleÄÝ©Ê¡C

¡@

¡@¡@3. Read¡BWrite©MSeek¤èªk

¡@¡@³o¤T­Ó¤èªk¬OTStreamªºµê¤èªk¡A¥u¬O¦bTHandleStream ¤¤Âл\¤F³o¤T­Ó¤èªk¡A¥H¹ê²{¯S©w´C¤¶¡X¡X¤å¥óªº¼Æ¾Ú¦s¨ú¡C«á­±·|¸Ô²Ó¤¶²Ð³o¤T­Ó¤èªkªº¹ê²{¡C

¡@

20.1.2.2 THandleStreamªº¹ê²{­ì²z

¡@

¡@¡@THandleStream¬O±qTStreamÄ~©Ó¨Óªº¡A¦]¦¹¥i¥H¦@¥ÎTStream¤¤ªºÄÝ©Ê©M¤j¦h¼Æ¤èªk¡CTHandleStream¦b¹ê²{¤W¥D­n¬O¼W¥[¤F¤@­ÓÄÝ©ÊHandle©MÂл\¤FCreate¡BRead¡BWrite©MSeek¥|­Ó¤èªk¡C

¡@¡@1. Äݩʪº¹ê²{

¡@¡@HandleÄݩʪº¹ê²{¥¿¦pDelphi¤j¦h¼ÆÄݩʪº¹ê²{¨º¼Ë¡A¥ý¦bª«¥ó©w¸qªºprivate³¡¤ÀÁn©ú¤@­Ó¦s©ñ¼Æ¾ÚªºÅܼÆFHandle¡AµM«á¦b©w¸qªºpublic³¡¤ÀÁn©úÄÝ©ÊHandle¡A¨ä¤¤ÄÝ©Ê©w¸qªºÅª¼g±±¨î³¡¤À¥[¤W°ßŪ±±¨î¡AŪ±±¨î¥u¬Oª½±µÅª¨úFHandleÅܼƪº­È¡A¨ä¹ê²{¦p¤U¡G

¡@

THandleStream = class(TStream)

private

FHandle: Integer;

public

¡K

property Handle: Integer read FHandle;

end;

¡@

2. ¤èªkªº¹ê²{

¡@¡@THandleStreamªºCreate¤èªk¡A¥HAHandle§@¬°°Ñ¼Æ¡A¦b¤èªkùØ­±¥u¬O²³æªº±NAHandleªº­È½áµ¹FHandle¡A¨ä¹ê²{¦p¤U¡G

¡@

constructor THandleStream.Create(AHandle: Integer);

begin

FHandle := AHandle;

end;

¡@

¡@¡@¬°¹ê²{°w¹ï¤å¥óªº¼Æ¾Úª«¥ó¦sÀx¡ATHandleStreamªºRead¡BWrite©MSeek¤èªkÂл\¤FTStream¤¤ªº¬ÛÀ³¤èªk¡C¥¦­Ìªº¹ê²{³£©I¥s¤FWindowsªº¤å¥óºÞ²z¨ç¼Æ¡C

¡@¡@Read¤èªk©I¥sFileRead¨ç¼Æ¹ê²{¤å¥óŪ¾Þ§@¡A¨ä¹ê²{¦p¤U¡G

¡@

function THandleStream.Read(var Buffer; Count: Longint): Longint;

begin

Result := FileRead(FHandle, Buffer, Count);

if Result = -1 then Result := 0;

end;

¡@

¡@¡@Write¤èªk©I¥sFileWrite¨ç¼Æ¹ê²{¤å¥ó¼g¾Þ§@¡A¨ä¹ê²{¦p¤U¡G

¡@

function THandleStream.Write(const Buffer; Count: Longint): Longint;

begin

Result := FileWrite(FHandle, Buffer, Count);

if Result = -1 then Result := 0;

end;

¡@

¡@¡@Seek¤èªk©I¥sFileSeek¨ç¼Æ¹ê²{¤å¥ó«ü¼Ðªº²¾°Ê¡A¨ä¹ê²{¦p¤U¡G

¡@

function THandleStream.Seek(Offset: Longint; Origin: Word): Longint;

begin

Result := FileSeek(FHandle, Offset, Origin);

end;

¡@

20.1.3 TFileStreamª«¥ó

¡@

¡@¡@TFileStreamª«¥ó¬O¦bºÏ¤ùÀɤW¦sÀx¼Æ¾ÚªºStreamª«¥ó¡CTFileStream¬O±qTHandleStreamÄ~©Ó¤U¨Óªº¡A¥¦©MTHandleStream¤@¼Ë³£¬O¹ê²{¤å¥óªº¦s¨ú¾Þ§@¡C¤£¦P¤§³B¦b©óTHandleStream¥Î¥y¬`¦s¨ú¤å¥ó¡A¦ÓTFileStream¥ÎÀÉ®×¼Ðñ¦s¨ú¤å¥ó¡C¹ê»Ú¤WTFileStream¬OTHandleStream¤Wªº¤@¼h¥]¸Ë¡A¨ä¤º®Ö¬OTHandleStreamªºÄÝ©Ê©M¤èªk¡C

¡@¡@TFileStream¤¤¨S¦³¼W¥[·sªºÄÝ©Ê©M¤èªk¡C¥¦¥u¬OÂл\¤Fªººc³y¤èªkCreate©MªRºc¤èªkDestory¡C¦bCreate¤èªk¤¤±a¨â­Ó°Ñ¼ÆFileName©MMode¡CFileName´y­z­n«Ø¥ß©Î¥´¶}ªºÀÉ®×¼Ðñ¡A¦ÓMode´y­z¤å¥ó¼Ò¦¡¦pfmCreate¡BfmOpenRead©MfmOpenWriteµ¥¡CCreate¤èªk­º¥ý¨Ï¥ÎFileCreate©ÎFileOpen¨ç¼Æ«Ø¥ß©Î¥´¶}¦W¬°FileNameªº¤å¥ó¡A¦A±N±o¨ìªº¤å¥ó¥y¬`½áµ¹FHandle¡CTFileStreamªº¤å¥óŪ¼g¾Þ§@³£¬O¥Ñ±qTHandleStreamÄ~©ÓªºRead

var

Stream: TStream;

begin

Stream := TFileStream.Create(FileName, fmCreate);

try

SaveToStream(Stream);

finally

Stream.Free;

end;

end;

¡@

¡@¡@¦bDelphi ªº³\¦hª«¥óªºSaveToStream ©MSaveToFile¡BLoadFromStream©MLoadFromFile¤èªkªº¹ê²{³£¦³Ãþ¦üªº±_ª¬µ²ºc¡C

¡@

20.1.5 TMemoryStreamª«¥ó

¡@

¡@ TMemoryStreamª«¥ó¬O¤@­ÓºÞ²z°ÊºA°O¾ÐÅ餤ªº¼Æ¾ÚªºStreamª«¥ó¡A¥¦¬O±qTCustomMemoryStream¤¤Ä~©Ó¤U¨Óªº¡A°£¤F±qTCustomMemoryStream¤¤Ä~©ÓªºÄÝ©Ê©M¤èªk¥~¡A¥¦ÁÙ¼W¥[©MÂл\¤F¤@¨Ç¥Î©ó±qºÏ¤ùÀÉ©M¨ä¥¦ª`¥xŪ¼Æ¾Úªº¤èªk¡C¥¦ÁÙ´£¨Ñ¤F¼g¤J¡B®ø°£°O¾ÐÅ餺®eªº°ÊºA°O¾ÐÅéºÞ²z¤èªk¡C¤U­±¤¶²Ð¥¦ªº³o¨ÇÄÝ©Ê©M¤èªk¡C

¡@

20.1.5.1 TMemoryStreamªºÄÝ©Ê©M¤èªk

¡@

¡@¡@1. CapacityÄÝ©Ê

¡@¡@Án©ú¡Gproperty Copacity: Longint;

CapacityÄݩʨM©w¤F¤À°tµ¹°O¾ÐÅé¬yªº°O¾ÐÅé¦Àªº¤j¤p¡C³o»PSizeÄݩʦ³¨Ç¤£¦P¡CSizeÄݩʬO´y­z¬y¤¤¼Æ¾Úªº¤j¤p¡C¦bµ{¦¡¤¤¥i¥H±NCapacity ªº­È³]©wªº¤ñ¼Æ¾Ú©Ò»Ý³Ì¤j°O¾ÐÅé¤j¤@¨Ç¡A³o¼Ë¥i¥HÁקKÀWÁc¦a­«·s¤À°t¡C

¡@¡@2. Realloc¤èªk

¡@¡@Án©ú¡Gfunction Realloc(var NewCapacity: Longint): Pointer; virtual;

Realloc¤èªk¡A¥H8K¬°³æ¦ì¤À°t°ÊºA°O¾ÐÅé¡A°O¾ÐÅ骺¤j¤p¥ÑNewCapacity«ü©w¡A¨ç¼Æªð¦^«ü¦V©Ò¤À°t°O¾ÐÅ骺«ü¼Ð¡C

¡@¡@3. SetSize¤èªk

¡@¡@SetSize¤èªk®ø°£°O¾ÐÅé¬y¤¤¥]§tªº¼Æ¾Ú¡A¨Ã±N°O¾ÐÅé¬y¤¤°O¾ÐÅé¦Àªº¤j¤p³]¬°Size¦ì¤¸¸s²Õ¡C¦pªGSize¬°¹s¡A¬OSetSize¤èªk±NÄÀ©ñ¤w¦³ªº°O¾ÐÅé¦À¡A¨Ã±NMemoryÄݩʸm¬°nil¡F§_«h¡ASetSize¤èªk±N°O¾ÐÅé¦À¤j¤p½Õ¾ã¬°Size¡C

4. Clear¤èªk

¡@¡@Án©ú¡Gprocedure Clear;

Clear¤èªkÄÀ©ñ°O¾ÐÅ餤ªº°O¾ÐÅé¦À¡A¨Ã±NMemoryÄݩʸm¬°nil¡C¦b©I¥sClear¤èªk«á¡ASize©MPositionÄݩʳ£¬°0¡C

¡@¡@5. LoadFromStream¤èªk

¡@¡@Án©ú¡Gprocedure LoadFromStream(Stream: TStream);

LoadFromStream¤èªk±NStream«ü©wªº¬y¤¤ªº¥þ³¡¤º®e´_¨î¨ìMemoryStream¤¤¡A´_¨î¹Lµ{±N¨ú¥N¤w¦³¤º®e¡A¨ÏMemoryStream¦¨¬°Streamªº¤@¥÷«þ¨©¡C

¡@¡@6. LoadFromFile¤èªk

¡@¡@Án©ú¡Gprocedure LoadFromFile(count FileName: String);

LoadFromFile¤èªk±NFileName«ü©w¤å¥óªº©Ò¦³¤º®e´_¨î¨ìMemoryStream¤¤¡A¨Ã¨ú¥N¤w¦³¤º®e¡C©I¥sLoadFromFile¤èªk«á¡AMemoryStream±N¦¨¬°¤å¥ó¤º®e¦b°O¾ÐÅ餤ªº§¹¾ã«þ¨©¡C

¡@

20.1.5.2 TMemoryStreamª«¥óªº¹ê²{­ì²z

¡@

¡@¡@TMemoryStream±qTCustomMemoryStreamª«¥óª½±µÄ~©Ó¡A¦]¦¹¥i¥H¨É¥ÎTCustomMemoryStreamªºÄÝ©Ê©M¤èªk¡C«e­±Á¿¹L¡ATCustomMemoryStream¬O¥Î©ó°O¾ÐÅ餤¼Æ¾Ú¾Þ§@ªº©â¶Hª«¥ó¡A¥¦¬°MemoryStreamª«¥óªº¹ê²{´£¨Ñ¤F®Ø¬[¡A®Ø¬[¤¤ªº¤º®eÁÙ­n¥Ñ¨ãÅéMemoryStreamª«¥ó¥h¶ñ¥R¡CTMemoryStreamª«¥ó´N¬O«ö°ÊºA°O¾ÐÅéºÞ²zªº»Ý­n¶ñ¥R®Ø¬[¤¤ªº¨ãÅ餺®e¡C¤U­±¤¶²ÐTMemoryStreamª«¥óªº¹ê²{¡C

¡@¡@1. TMemoryStreamÄݩʪº¹ê²{

¡@¡@TMemoryStream¦b¨äprotected³¡¤À¼W¥[¤F¤@­ÓCapacityÄÝ©Ê¡A¸ÓÄݩʨM©w¤FMemoryStream©Ò¦û°ÊºA°O¾ÐÅ骺¤j¤p¡CTMemoryStream­º¥ý¦bprivate³¡¤ÀÁn©ú¤FFCapacityÅܼƧ@¬°¦sÀxCapacityÄݩʭȪº¼Æ¾ÚÄæ¦ì¡AµM«á¦bprotected³¡¤ÀÁn©ú¤F¸ÓÄÝ©Ê¡C¦bÄÝ©ÊÁn©úªºÅª±±¨î³¡¤À²³æŪ¨úFCapacityªº­È¡A¦b¼g±±¨î³B©I¥s¤F¤èªkSetCapacity¡C¸Ó¤èªk°£¤Fµ¹FCapacity½á­È¥~ÁÙ°õ¦æ¤F­×§ïCapacityÄÝ©Ê©Ò¥²»Ý¾Þ§@¦pª¬ºA§ïÅܵ¥¡C

¡@¡@¤U­±¬OÄݩʪº¹ê²{¡G

¡@

TMemoryStream = class(TCustomMemoryStream)

private

FCapacity: Longint;

procedure SetCapacity(NewCapacity: Longint);

protected

¡K

property Capacity: Longint read FCapacity write SetCapacity;

public

¡K

end;

¡@

¡@¡@¼g±±¨î¤èªkSetCapacityªº¹ê²{¬O³o¼Ëªº¡G

¡@

procedure TMemoryStream.SetCapacity(NewCapacity: Longint);

begin

SetPointer(Realloc(NewCapacity), FSize);

FCapacity := NewCapacity;

end;

¡@

¡@¡@¦bSetCapacity ¤èªk¥ý¬O©I¥sRealloc­«·s¤À°t°O¾ÐÅé¡AµM«á¥ÎNewCapacityªº­Èµ¹FCapacity½á­È¡CRealloc¤èªk¶i¦æ¬Y¨Çª«¥óª¬ºAªº§ïÅÜ¡C

¡@¡@2. TMemoryStreamª«¥ó¤èªkªº¹ê²{

¡@¡@Æ« Realloc¤èªk

¡@¡@Realloc¤èªk¬OTMemoryStream°ÊºA°O¾ÐÅé¤À°tªº®Ö¤ß¡A¥¦ªºSetSize¡BSetCapacityµ¥¤èªk³Ì²×³£¬O©I¥sRealloc¶i¦æ°O¾ÐÅ骺¤À°t©Mªì©l¤Æ¤u§@ªº¡C¥¦ªº¹ê²{¦p¤U¡G

¡@

const

MemoryDelta = $2000;

¡@

function TMemoryStream.Realloc(var NewCapacity: Longint): Pointer;

begin

if NewCapacity > 0 then

NewCapacity := (NewCapacity + (MemoryDelta - 1)) and not (MemoryDelta - 1);

Result := Memory;

if NewCapacity <> FCapacity then

begin

if NewCapacity = 0 then

begin

GlobalFreePtr(Memory);

Result := nil;

end else

begin

if Capacity = 0 then

Result := GlobalAllocPtr(HeapAllocFlags, NewCapacity)

else

Result := GlobalReallocPtr(Memory, NewCapacity, HeapAllocFlags);

if Result = nil then raise EStreamError.CreateRes(SMemoryStreamError);

end;

end;

end;

¡@

¡@¡@Realloc¤èªk¬O¥H8K¬°³æ¦ì¤À°t°ÊºA°O¾ÐÅ骺¡A¤èªk¤¤ªº²Ä¤@¥yif»y¥y´N¬O°õ¦æ¸Ó¾Þ§@¡C¦pªG¶Ç¤JªºNewCapacity°Ñ¼Æ­È¬°0¡A«hÄÀ©ñ¬y¤¤ªº°O¾ÐÅé¡CRealloc¤èªk¥ÎGLobal FreePtr¨ç¼ÆÄÀ©ñ°O¾ÐÅé¡A¥ÎGlobalAllocPtr¤À°t°O¾ÐÅé¡A¥ÎGlobalReallocPtr¶i¦æ°O¾ÐÅ骺­«¤À°t¡C¦pªG­ì¨ÓªºCapacityÄݩʭȬ°0¡A«h©I¥sGloba|AllocPtr§_«h©I¥sGlobalReallocPtr¡C³Ì«á¦pªGResult¬°nil«hIJµo°O¾ÐÅé¬y¿ùªº²§±`¨Æ¥ó¡A§_«hªð¦^«ü¦V¤À°tªº°O¾ÐÅ骺«ü¼Ð¡C

¡@¡@Ƭ Write¤èªk

¡@¡@Write¤èªk±q°O¾ÐÅé¬y¤º³¡½w½Ä¦Àªº¥Ø«e¦ì¸m¶}©l¼g¤J¤G¶i¨î¼Æ¾Ú¡C¨ä¹ê²{¦p¤U¡G

¡@

function TMemoryStream.Write(const Buffer; Count: Longint): Longint;

var

Pos: Longint;

begin

if (FPosition >= 0) and (Count >= 0) then

begin

Pos := FPosition + Count;

if Pos > 0 then

begin

if Pos > FSize then

begin

if Pos > FCapacity then

SetCapacity(Pos);

FSize := Pos;

end;

System.Move(Buffer, Pointer(Longint(FMemory) + FPosition)^, Count);

FPosition := Pos;

Result := Count;

Exit;

end;

end;

Result := 0;

end;

¡@

¡@¡@Buffer¤¤¦sÀx­n¼g¤J¬yªº¤G¶i¨î¼Æ¾Ú¡A¦pªG­n¼g¤Jªº¼Æ¾Úªº¦ì¤¸¸s²Õ¶W¥X¤F¬yªº°O¾ÐÅé¦Àªº¤j¤p¡A«h©I¥sSetCapacity¤èªk¦A¤À°t°O¾ÐÅé¡AµM«á¥Î°O¾ÐÅé´_¨î¨ç¼Æ±NBuffer¤¤ªº¼Æ¾Ú´_¨î¨ìFMemory¤¤¡C±µµÛ²¾°Ê¦ì¸m«ü¼Ð¡A¨Ãªð¦^¼g¤J¼Æ¾Úªº¦ì¤¸¸s²Õ¼Æ¡C¤ÀªR³o¬qµ{¦¡¥i¥Hª¾¹D¡AFCapacityªº­È©MFSizeªº­È¬O¤£¦Pªº¡C

¡@¡@Æ­ Clear¤èªk

¡@¡@Clear¤èªk®ø°£°O¾ÐÅé¬y¤¤ªº¼Æ¾Ú¡A±NMemoryÄݩʸm¬°nil¡A¨Ã±NFSize©MFPosition ªº­È³]¬°0¡C¨ä¹ê²{¦p¤U¡G

¡@

procedure TMemoryStream.Clear;

begin

SetCapacity(0);

FSize := 0;

FPosition := 0;

end;

¡@

¡@¡@Æ® LoadFromStream©MLoadFromFile¤èªk

¡@¡@LoadFromStream¤èªk­º¥ý®Ú¾Ú¶Ç¤JªºStreamªºSizeÄÝ©Ê­È­«·s¤À°t°ÊºA°O¾ÐÅé¡AµM«á©I¥sStreamªºReadBuffer¤èªk©¹FMemory¤¤´_¨î¼Æ¾Ú¡Aµ²ªGStreamªº¥þ³¡¤º®e¦b°O¾ÐÅ餤¦³¤F¤@¥÷§¹¾ã«þ¨©¡C¨ä¹ê²{¦p¤U¡G

¡@

procedure TMemoryStream.LoadFromStream(Stream: TStream);

var

Count: Longint;

begin

Stream.Position := 0;

Count := Stream.Size;

SetSize(Count);

if Count <> 0 then Stream.ReadBuffer(FMemory^, Count);

end;¡@

¡@ LoadFromFile»PLoadFromStream¬O¤@¹ï¤èªk¡CLoadFromFile­º¥ý«Ø¥ß¤F¤@­ÓTFileStreamª«¥ó¡AµM«á©I¥sLoadFromStream¤èªk¡A±NFileStream¤å¥ó¬y¤¤ªº¼Æ¾Ú¼g¤JMemoryStream¤¤¡C


«á¤@­¶
«e¤@­¶
¦^¥Ø¿ý
¦^­º­¶