00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CATStream.h"
00014 #include "CATStreamSub.h"
00015
00016
00017 CATStream* CATStream::DefSubStreamBuilder(CATInt64 offset,
00018 CATInt64 length,
00019 CATStream* parent,
00020 void* param1,
00021 CATUInt32 param2)
00022 {
00023 CATStream* subStream = 0;
00024 try
00025 {
00026 subStream = new CATStreamSub(offset,length,parent);
00027 }
00028 catch (...)
00029 {
00030 return 0;
00031 }
00032 return subStream;
00033 }
00034
00035
00036
00037
00038
00039 CATStream* CATStream::CreateSubStream(CATInt64 streamOffset,
00040 CATInt64 streamLength,
00041 CATSUBSTREAMBUILDER builder,
00042 void* param1,
00043 CATUInt32 param2)
00044 {
00045 CATResult result = CAT_SUCCESS;
00046 CATStream* subStream = 0;
00047 if (!IsOpen())
00048 {
00049 CATASSERT(false,"File must be open to create a sub stream.");
00050 return 0;
00051 }
00052
00053 CATASSERT(builder != 0, "Must pass a builder - use the default if nothing else.");
00054 if (builder == 0)
00055 {
00056 return 0;
00057 }
00058
00059 subStream = builder(streamOffset,streamLength,this,param1,param2);
00060
00061 if (subStream != 0)
00062 {
00063 if (CATFAILED(result = ((CATStreamSub*)subStream)->OnAcquireSub()))
00064 {
00065 delete subStream;
00066 subStream = 0;
00067 return 0;
00068 }
00069
00070 fSubCount++;
00071 return subStream;
00072 }
00073
00074 return 0;
00075 }
00076
00077
00078
00079
00080
00081 CATResult CATStream::ReleaseSubStream( CATStream*& subStream )
00082 {
00083 CATASSERT(subStream != 0, "Null substream passed to ReleaseSubStream().");
00084 if (subStream == 0)
00085 {
00086 return CATRESULT(CAT_ERR_INVALID_PARAM);
00087 }
00088
00089 CATASSERT(fSubCount != 0, "No substreams registered.");
00090 CATResult result = ((CATStreamSub*)subStream)->OnReleaseSub();
00091 delete subStream;
00092 subStream = 0;
00093 fSubCount--;
00094
00095 return result;
00096 }
00097
00098
00099 CATResult CATStream::CopyToStream( CATStream* outputStream,
00100 CATUInt32 bufSize,
00101 CATInt64 offset,
00102 CATInt64 length)
00103 {
00104 CATResult result;
00105
00106 if (outputStream == 0)
00107 return CATRESULT(CAT_ERR_INVALID_PARAM);
00108
00109 if (bufSize == 0)
00110 bufSize = kCAT_DEFAULT_STREAM_BUF_SIZE;
00111
00112 this->SeekAbsolute(offset);
00113 if (length == 0)
00114 {
00115 this->Size(length);
00116 length -= offset;
00117 }
00118
00119 CATUInt8 *buffer = new CATUInt8[bufSize];
00120 if (buffer == 0)
00121 {
00122 return CATRESULT(CAT_ERR_OUT_OF_MEMORY);
00123 }
00124
00125 while (length)
00126 {
00127 CATUInt32 amountRead = bufSize;
00128 if (length < bufSize)
00129 {
00130 amountRead = (CATUInt32)length;
00131 }
00132
00133 if (CATFAILED(result = this->Read(buffer,amountRead)))
00134 {
00135 delete [] buffer;
00136 return result;
00137 }
00138
00139 if (CATFAILED(result = outputStream->Write(buffer, amountRead)))
00140 {
00141 delete [] buffer;
00142 return result;
00143 }
00144
00145 if (amountRead == 0)
00146 length = 0;
00147
00148 length -= amountRead;
00149 }
00150
00151 delete [] buffer;
00152 return CATRESULT(CAT_SUCCESS);
00153 }