#include <ByteArray.h>
Inheritance diagram for Core::ByteArray:
Public Member Functions | |
ByteArray (int size=0) | |
Creates a new ByteArray with specified size. | |
ByteArray (const ByteArray &origin) | |
Copy constructor. | |
ByteArray (const char *buffer, int size) | |
Create a new ByteArray with bytes in buffer . | |
~ByteArray (void) | |
virtual String * | className (void) const |
Answer receiver class name. | |
virtual long int | size () const |
Returns number of bytes in the array. | |
virtual Object * | at (int index) const |
Returns Integer instance, which value is less or equal to 255 - the value represents byte value. | |
virtual void | put (int index, Object *obj) |
Put byte on specified index. | |
virtual void | replace (long start, long stop, const SequenceableCollection *collection) |
virtual void | replace (long start, long stop, unsigned char *collection) |
virtual void | replace (long start, long stop, const SequenceableCollection *collection, long startIndex) |
virtual void | replace (long start, long stop, unsigned char *collection, long startIndex) |
virtual void | changeSize (long newSize) |
Changes size to be exactly newSize bytes If newSize is smaller than actual size, array is silently truncated to newSize . | |
virtual void | add (Object *obj) |
Raises an exception - you cannot add to array. | |
virtual Object * | copy (void) |
Creates a new copy of this object, containing same items as original. | |
virtual Object * | copy (long from, long to) |
Returns a new collection with the same items as the specified portion of this collection. | |
virtual Object * | copyEmpty (long int size) |
Creates an empty copy (ie. | |
virtual unsigned char * | rawBytes (void) const |
Returns raw bytes, i.e pointer to byte array, It malloc()s a new byte array, so after work is done, you must free() the returned array to avoid memory leak. | |
virtual const unsigned char * | rawBytesReadOnly (void) const |
Returns raw bytes, i.e. | |
virtual long | hash (void) const |
Answer object hash value. | |
virtual bool | isEqual (const Object *object) const |
Compare receiver with given object. | |
virtual bool | isEqual (const Object &object) const |
Compare receiver with given object. | |
virtual bool | isEqual (const char *array, int size) const |
virtual bool | isEqualToByteArray (const Object *array) const |
virtual ByteArray * | asByteArray (void) |
virtual char * | asCString (void) |
Answer zero terminated c-array of char. | |
virtual String * | asString (void) |
virtual String * | asStringWithEncoding (String *encoding) |
virtual String * | asStringWithEncoding (const char *encoding) |
virtual unsigned long | asInteger (void) |
virtual unsigned long | asNumberWithRadix (int radix) |
virtual InternalEncodedStreamFactory * | withEncoding (String *encoding) |
virtual InternalEncodedStreamFactory * | withEncoding (const char *encoding) |
virtual bool | isByteArray (void) |
Static Public Member Functions | |
static ByteArray * | fromStringEncoding (const String *string, String *encoding) |
Protected Attributes | |
unsigned char * | bytes |
Buffer that holds bytes. | |
long | tally |
Length of bytes - how much memory is allocated. | |
Private Member Functions | |
virtual void | checkObject (Object *obj, const char *sel) |
This is helper method, it raises an Error if Object is not Integer instance and if it's value is not between <0,255>. |
|
Creates a new ByteArray with specified size. Content are set to zero. Definition at line 34 of file ByteArray.cc. Referenced by copy(), copyEmpty(), and fromStringEncoding(). 00035 { 00036 bytes = NULL; 00037 tally = size; 00038 if (tally) { 00039 bytes = (unsigned char *) GC_malloc(tally * sizeof(unsigned char)); 00040 bzero(bytes, tally); 00041 } 00042 }
|
|
Copy constructor.
Definition at line 44 of file ByteArray.cc. 00045 { 00046 bytes = NULL; 00047 tally = origin.tally; 00048 if (tally) { 00049 bytes = (unsigned char *) GC_malloc(tally * sizeof(unsigned char)); 00050 memcpy(bytes, origin.bytes, tally); 00051 } 00052 }
|
|
Create a new ByteArray with bytes in bytes are copied, so buffer could be released (free()ed) after ByteArray is created.
Definition at line 54 of file ByteArray.cc. 00055 { 00056 bytes = NULL; 00057 tally = size; 00058 if (tally) { 00059 bytes = (unsigned char *) GC_malloc(tally * sizeof(unsigned char)); 00060 memcpy(bytes, buffer, tally); 00061 } 00062 }
|
|
Definition at line 64 of file ByteArray.cc.
|
|
Raises an exception - you cannot add to array.
You may changed the size and then Reimplemented from Core::Collection. Definition at line 133 of file ByteArray.cc. References Core::Object::shouldNotImplement(). 00134 { 00135 shouldNotImplement(__PRETTY_FUNCTION__); 00136 }
|
|
Reimplemented from Core::SequenceableCollection. Definition at line 183 of file ByteArray.cc. 00184 { 00185 return this; 00186 }
|
|
Answer zero terminated c-array of char.
Definition at line 188 of file ByteArray.cc. Referenced by Core::EncodedStreamFactory::createCharacterEncoder(). 00189 { 00190 char *cString = (char *) GC_malloc((tally + 1) * sizeof(char)); 00191 memcpy(cString, bytes, tally); 00192 cString[tally] = 0; 00193 return cString; 00194 }
|
|
Definition at line 211 of file ByteArray.cc. References asNumberWithRadix(). 00212 { 00213 return asNumberWithRadix(256); 00214 }
|
|
Definition at line 216 of file ByteArray.cc. Referenced by asInteger(). 00217 { 00218 unsigned long number = 0; 00219 00220 for (long i = 0; i < tally; i++) { 00221 number = number * radix + bytes[i]; 00222 } 00223 return number; 00224 }
|
|
Reimplemented from Core::SequenceableCollection. Definition at line 196 of file ByteArray.cc. 00197 { 00198 return new String(this); 00199 }
|
|
Definition at line 206 of file ByteArray.cc. References asStringWithEncoding(). 00207 { 00208 return asStringWithEncoding(new String(encoding)); 00209 }
|
|
Definition at line 201 of file ByteArray.cc. References withEncoding(). Referenced by asStringWithEncoding(). 00202 { 00203 return dynamic_cast<String *>(withEncoding(encoding)->readStream()->upToEnd()); 00204 }
|
|
Returns Integer instance, which value is less or equal to 255 - the value represents byte value. If index is out of bound, an Error is raised.
Reimplemented from Core::SequenceableCollection. Definition at line 79 of file ByteArray.cc. References bytes, Core::GenericException::raiseFrom(), tally, and Core::Integer::value(). Referenced by Core::Stream::nextBigEndianLong(), Core::Stream::nextBigEndianShort(), Core::Stream::nextLittleEndianLong(), and Core::Stream::nextLittleEndianShort(). 00080 { 00081 if (index < 0 || index >= tally) { 00082 SubscriptOutOfBoundsError *ex; 00083 ex = new SubscriptOutOfBoundsError(__PRETTY_FUNCTION__, index); 00084 ex->raiseFrom(this); 00085 } 00086 return Integer::value(bytes[index] & 0xFF); 00087 }
|
|
Changes size to be exactly
If
Reimplemented from Core::SequenceableCollection. Definition at line 112 of file ByteArray.cc. 00113 { 00114 unsigned char *oldBytes = bytes; 00115 long i; 00116 00117 bytes = (unsigned char *) GC_malloc(newSize * sizeof(unsigned char)); 00118 // MC: This will fail when shinking the content 00119 // memset(bytes, 0, newSize); 00120 // memcpy(bytes, oldBytes, (newSize > tally) ? newSize : tally); 00121 /* Copy old content or the part that fits */ 00122 for (i = 0; i < newSize && i < tally; i++) { 00123 bytes[i] = oldBytes[i]; 00124 } 00125 /* Fill the rest with 0s */ 00126 for (; i < newSize; i++) { 00127 bytes[i] = 0; 00128 } 00129 tally = newSize; 00130 GC_free(oldBytes); 00131 }
|
|
This is helper method, it raises an Error if Object is not Integer instance and if it's value is not between <0,255>.
Definition at line 260 of file ByteArray.cc. References Core::Number::asLongLong(), Core::Object::isNumber(), and Core::GenericException::raiseFrom(). Referenced by put(). 00261 { 00262 if (obj->isNumber()) { 00263 Number* n = (Number *) obj; 00264 long long l = n->asLongLong(); 00265 if (l < 0 || l > 255) { 00266 Error *e = new Error(new String("Invalid value!"), new String(sel), obj); 00267 e->raiseFrom(this); 00268 } 00269 } else { 00270 Error *e = new Error(new String("Not a Number instance!"), new String(sel), obj); 00271 e->raiseFrom(this); 00272 } 00273 }
|
|
Answer receiver class name. Because there isn't any standard way to obtain class name this method comes to place. Every class should rewrite this method but many didn't (yet). Reimplemented from Core::Collection. Definition at line 65 of file ByteArray.h. 00066 { return new String("ByteArray"); }
|
|
Returns a new collection with the same items as the specified portion of this collection.
Reimplemented from Core::SequenceableCollection. Definition at line 131 of file ByteArray.h. References Core::SequenceableCollection::copy(). 00132 { return SequenceableCollection::copy(from, to); }
|
|
Creates a new copy of this object, containing same items as original.
Reimplemented from Core::SequenceableCollection. Definition at line 138 of file ByteArray.cc. References ByteArray(). 00139 { 00140 return new ByteArray(*this); 00141 }
|
|
Creates an empty copy (ie. the copy has same size but contains no items) Definition at line 143 of file ByteArray.cc. References ByteArray().
|
|
Definition at line 71 of file ByteArray.cc. References ByteArray(), Core::StreamDecorator::contents(), Core::Stream::nextPutAll(), and Core::String::size(). Referenced by Core::String::asByteArrayEncoding(). 00072 { 00073 EncodedStream *stream = (new ByteArray(string->size()))->withEncoding(encoding)->writeStream(); 00074 stream->nextPutAll(string); 00075 return dynamic_cast<ByteArray *>(stream->contents()); 00076 }
|
|
Answer object hash value. The value should be same for objects found equal with isEqual() method. So if you rewrite this method you should rewrite isEqual() method too. Reimplemented from Core::Object. Definition at line 149 of file ByteArray.cc. 00150 { 00151 long hashConst = 0x23451; 00152 int length = size(); 00153 00154 if (length > 0) { 00155 hashConst ^= bytes[0]; 00156 if (length > 1) { 00157 hashConst ^= bytes[length-1]; 00158 if (length > 3) { 00159 hashConst ^= bytes[length/2-1] ^ bytes[length/2+1]; 00160 } 00161 } 00162 } 00163 return hashConst; 00164 }
|
|
Reimplemented from Core::Collection. Definition at line 254 of file ByteArray.cc. 00255 { 00256 return true; 00257 }
|
|
Definition at line 171 of file ByteArray.cc. 00172 { 00173 if (size != tally) return false; 00174 return memcmp(bytes, array, tally) == 0; 00175 }
|
|
Compare receiver with given object. Do not rewrite this method. It just a sugar for Object::isEqual(Object *).
Reimplemented from Core::Object. Definition at line 160 of file ByteArray.h. References Core::Object::isEqual(). 00161 { return SequenceableCollection::isEqual(object); }
|
|
Compare receiver with given object. This method could compare objects with more sophisticated algorithm (eg. based on instance variables comparing or so). If you rewrite this method you should rewrite hash() too.
Reimplemented from Core::Object. Definition at line 166 of file ByteArray.cc. References Core::Object::isEqualToByteArray(). 00167 { 00168 return object->isEqualToByteArray(this); 00169 }
|
|
Reimplemented from Core::Object. Definition at line 177 of file ByteArray.cc. 00178 { 00179 return dynamic_cast<const ByteArray *>(array)->isEqual((char *) bytes, tally); 00180 }
|
|
Put byte on specified index.
An Error is raised, when
Reimplemented from Core::SequenceableCollection. Definition at line 89 of file ByteArray.cc. References bytes, checkObject(), Core::GenericException::raiseFrom(), and tally. Referenced by Core::Stream::nextPutBigEndianLong(), Core::Stream::nextPutBigEndianShort(), Core::Stream::nextPutLittleEndianLong(), Core::Stream::nextPutLittleEndianShort(), and Net::IPSocketAddress::stringToBytes(). 00090 { 00091 if (index < 0 || index >= tally) { 00092 SubscriptOutOfBoundsError *ex; 00093 ex = new SubscriptOutOfBoundsError(__PRETTY_FUNCTION__, index); 00094 ex->raiseFrom(this); 00095 } 00096 checkObject(obj, __PRETTY_FUNCTION__); 00097 unsigned char val = ((Number *) obj)->asLong(); 00098 bytes[index] = val & 0xFF; 00099 }
|
|
Returns raw bytes, i.e pointer to byte array, It malloc()s a new byte array, so after work is done, you must free() the returned array to avoid memory leak.
Definition at line 226 of file ByteArray.cc. 00227 { 00228 //#ifdef NOGC 00229 // unsigned char *retbytes = (unsigned char *) malloc(tally); 00230 //#else 00231 unsigned char *retbytes = (unsigned char *) GC_malloc(tally * sizeof(unsigned char *)); 00232 //#endif 00233 memcpy(retbytes, bytes, tally); 00234 return retbytes; 00235 }
|
|
Returns raw bytes, i.e.
pointer to byte array. This method (as oppostite to
Definition at line 237 of file ByteArray.cc. References bytes. Referenced by Net::IPSocketAddress::hostNameByAddress(), and OS::MessageQueue::writeFrom(). 00238 { 00239 return bytes; 00240 }
|
|
Definition at line 106 of file ByteArray.cc. References bytes. 00108 { 00109 memcpy(&bytes[start], &collection[startIndex], stop - start); 00110 }
|
|
Reimplemented from Core::SequenceableCollection. Definition at line 103 of file ByteArray.h. References Core::SequenceableCollection::replace(). 00105 { SequenceableCollection::replace(start, stop, collection, startIndex); }
|
|
Definition at line 101 of file ByteArray.cc. References replace(). 00102 { 00103 replace(start, stop, collection, 0); 00104 }
|
|
Reimplemented from Core::SequenceableCollection. Definition at line 99 of file ByteArray.h. References Core::SequenceableCollection::replace(). Referenced by Net::SocketAccessor::readInto(), OS::MessageQueue::readInto(), Core::FileAccessor::readInto(), and replace(). 00101 { SequenceableCollection::replace(start, stop, collection); }
|
|
Returns number of bytes in the array.
Reimplemented from Core::SequenceableCollection. Definition at line 73 of file ByteArray.h. References tally. Referenced by Core::EncodedStreamFactory::createCharacterEncoder(), hash(), Net::IPSocketAddress::hostNameByAddress(), Core::IOBuffer::lastWriteablePosition(), Core::IOBuffer::nextAndSetOffset(), Core::IOBuffer::readBufferStartingAt(), Core::IOAccessor::readInto(), and Core::IOAccessor::writeFrom(). 00074 { return tally; }
|
|
Definition at line 248 of file ByteArray.cc. References withEncoding(). 00249 { 00250 return withEncoding(new String(encoding)); 00251 }
|
|
Definition at line 243 of file ByteArray.cc. Referenced by asStringWithEncoding(), and withEncoding(). 00244 { 00245 return new InternalEncodedStreamFactory(this, encoding); 00246 }
|
|
Buffer that holds bytes.
Definition at line 39 of file ByteArray.h. Referenced by asCString(), asNumberWithRadix(), at(), ByteArray(), changeSize(), hash(), isEqual(), isEqualToByteArray(), put(), rawBytes(), rawBytesReadOnly(), replace(), and ~ByteArray(). |
|
Length of bytes - how much memory is allocated.
Definition at line 42 of file ByteArray.h. Referenced by asCString(), asNumberWithRadix(), at(), ByteArray(), changeSize(), isEqual(), isEqualToByteArray(), put(), rawBytes(), size(), and ~ByteArray(). |