Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Core::ByteArray Class Reference

#include <ByteArray.h>

Inheritance diagram for Core::ByteArray:

Inheritance graph
[legend]
Collaboration diagram for Core::ByteArray:

Collaboration graph
[legend]
List of all members.

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 StringclassName (void) const
 Answer receiver class name.
virtual long int size () const
 Returns number of bytes in the array.
virtual Objectat (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 Objectcopy (void)
 Creates a new copy of this object, containing same items as original.
virtual Objectcopy (long from, long to)
 Returns a new collection with the same items as the specified portion of this collection.
virtual ObjectcopyEmpty (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 ByteArrayasByteArray (void)
virtual char * asCString (void)
 Answer zero terminated c-array of char.
virtual StringasString (void)
virtual StringasStringWithEncoding (String *encoding)
virtual StringasStringWithEncoding (const char *encoding)
virtual unsigned long asInteger (void)
virtual unsigned long asNumberWithRadix (int radix)
virtual InternalEncodedStreamFactorywithEncoding (String *encoding)
virtual InternalEncodedStreamFactorywithEncoding (const char *encoding)
virtual bool isByteArray (void)

Static Public Member Functions

static ByteArrayfromStringEncoding (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>.

Constructor & Destructor Documentation

ByteArray::ByteArray int  size = 0  ) 
 

Creates a new ByteArray with specified size.

Content are set to zero.

Definition at line 34 of file ByteArray.cc.

References bytes, and tally.

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 }

ByteArray::ByteArray const ByteArray origin  ) 
 

Copy constructor.

Definition at line 44 of file ByteArray.cc.

References bytes, and tally.

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 }

ByteArray::ByteArray const char *  buffer,
int  size
 

Create a new ByteArray with bytes in buffer.

bytes are copied, so buffer could be released (free()ed) after ByteArray is created.

  • buffer - a source byte buffer
  • size - how many bytes will be taken from buffer. If malformed, SIGSEGV may occur.

Definition at line 54 of file ByteArray.cc.

References bytes, and tally.

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 }

ByteArray::~ByteArray void   ) 
 

Definition at line 64 of file ByteArray.cc.

References bytes, and tally.

00065 {
00066     if (tally)
00067         GC_free(bytes);
00068 }


Member Function Documentation

void ByteArray::add Object obj  )  [virtual]
 

Raises an exception - you cannot add to array.

You may changed the size and then put.

Reimplemented from Core::Collection.

Definition at line 133 of file ByteArray.cc.

References Core::Object::shouldNotImplement().

00134 {
00135     shouldNotImplement(__PRETTY_FUNCTION__);
00136 }

ByteArray * ByteArray::asByteArray void   )  [virtual]
 

Reimplemented from Core::SequenceableCollection.

Definition at line 183 of file ByteArray.cc.

00184 {
00185     return this;
00186 }

char * ByteArray::asCString void   )  [virtual]
 

Answer zero terminated c-array of char.

Definition at line 188 of file ByteArray.cc.

References bytes, and tally.

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 }

unsigned long ByteArray::asInteger void   )  [virtual]
 

Definition at line 211 of file ByteArray.cc.

References asNumberWithRadix().

00212 {
00213     return asNumberWithRadix(256);
00214 }

unsigned long ByteArray::asNumberWithRadix int  radix  )  [virtual]
 

Definition at line 216 of file ByteArray.cc.

References bytes, and tally.

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 }

String * ByteArray::asString void   )  [virtual]
 

Reimplemented from Core::SequenceableCollection.

Definition at line 196 of file ByteArray.cc.

00197 {
00198     return new String(this);
00199 }

String * ByteArray::asStringWithEncoding const char *  encoding  )  [virtual]
 

Definition at line 206 of file ByteArray.cc.

References asStringWithEncoding().

00207 {
00208     return asStringWithEncoding(new String(encoding));
00209 }

String * ByteArray::asStringWithEncoding String encoding  )  [virtual]
 

Definition at line 201 of file ByteArray.cc.

References withEncoding().

Referenced by asStringWithEncoding().

00202 {
00203     return dynamic_cast<String *>(withEncoding(encoding)->readStream()->upToEnd());
00204 }

Object * ByteArray::at int  index  )  const [virtual]
 

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.

Returns:
Integer instance, which value id between 0 and 255
Exceptions:
SubscriptOutOfBoundsError if index is out bo bounds.

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 }

void ByteArray::changeSize long  newSize  )  [virtual]
 

Changes size to be exactly newSize bytes If newSize is smaller than actual size, array is silently truncated to newSize.

If newSize is greater than size(), bytes at positions (newSize - size(), newSize&gt are set to zero;

Reimplemented from Core::SequenceableCollection.

Definition at line 112 of file ByteArray.cc.

References bytes, and tally.

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 }

void ByteArray::checkObject Object obj,
const char *  sel
[private, virtual]
 

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 }

virtual String* Core::ByteArray::className void   )  const [inline, virtual]
 

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"); }

virtual Object* Core::ByteArray::copy long  from,
long  to
[inline, virtual]
 

Returns a new collection with the same items as the specified portion of this collection.

  • from - the first item of this collection, that will be copied
  • to - the item behind the last item of this collection, that will be copied. If to <= from, no items will be copied and the returned collection will zero length.

Reimplemented from Core::SequenceableCollection.

Definition at line 131 of file ByteArray.h.

References Core::SequenceableCollection::copy().

00132       { return SequenceableCollection::copy(from, to); }

Object * ByteArray::copy void   )  [virtual]
 

Creates a new copy of this object, containing same items as original.

Returns:
a new copy of ByteArray

Reimplemented from Core::SequenceableCollection.

Definition at line 138 of file ByteArray.cc.

References ByteArray().

00139 {
00140     return new ByteArray(*this);
00141 }

Object * ByteArray::copyEmpty long int  size  )  [virtual]
 

Creates an empty copy (ie.

the copy has same size but contains no items)

Definition at line 143 of file ByteArray.cc.

References ByteArray().

00144 {
00145     return new ByteArray(size);
00146 }

ByteArray * ByteArray::fromStringEncoding const String string,
String encoding
[static]
 

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 }

long ByteArray::hash void   )  const [virtual]
 

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.

References bytes, and size().

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 }

bool ByteArray::isByteArray void   )  [virtual]
 

Reimplemented from Core::Collection.

Definition at line 254 of file ByteArray.cc.

00255 {
00256     return true;
00257 }

bool ByteArray::isEqual const char *  array,
int  size
const [virtual]
 

Definition at line 171 of file ByteArray.cc.

References bytes, and tally.

00172 {
00173     if (size != tally) return false;
00174     return memcmp(bytes, array, tally) == 0;
00175 }

virtual bool Core::ByteArray::isEqual const Object object  )  const [inline, virtual]
 

Compare receiver with given object.

Do not rewrite this method. It just a sugar for Object::isEqual(Object *).

  • object - object to compare receiver with

Reimplemented from Core::Object.

Definition at line 160 of file ByteArray.h.

References Core::Object::isEqual().

00161       { return SequenceableCollection::isEqual(object); }

bool ByteArray::isEqual const Object object  )  const [virtual]
 

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.

  • object - object to compare receiver with

Reimplemented from Core::Object.

Definition at line 166 of file ByteArray.cc.

References Core::Object::isEqualToByteArray().

00167 {
00168     return object->isEqualToByteArray(this);
00169 }

bool ByteArray::isEqualToByteArray const Object array  )  const [virtual]
 

Reimplemented from Core::Object.

Definition at line 177 of file ByteArray.cc.

References bytes, and tally.

00178 {
00179     return dynamic_cast<const ByteArray *>(array)->isEqual((char *) bytes, tally);
00180 }

void ByteArray::put int  index,
Object obj
[virtual]
 

Put byte on specified index.

An Error is raised, when obj is not instance of Integer or Integer's value is not between 0 an 255. Error is also raised when index is out of bounds.

  • index - an index of byte to be stored
  • obj - a Number instance that holds byte value

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 }

unsigned char * ByteArray::rawBytes void   )  const [virtual]
 

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.

See also:
rawBytesReadOnly(void);

Definition at line 226 of file ByteArray.cc.

References bytes, and tally.

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 }

const unsigned char * ByteArray::rawBytesReadOnly void   )  const [virtual]
 

Returns raw bytes, i.e.

pointer to byte array. This method (as oppostite to rawBytes(void)) returns ByteArray's internal pointer, so this method could be used only when you are sure that you never modify returned buffer.

See also:
rawBytes(void);

Definition at line 237 of file ByteArray.cc.

References bytes.

Referenced by Net::IPSocketAddress::hostNameByAddress(), and OS::MessageQueue::writeFrom().

00238 {
00239     return bytes;
00240 }

void ByteArray::replace long  start,
long  stop,
unsigned char *  collection,
long  startIndex
[virtual]
 

Definition at line 106 of file ByteArray.cc.

References bytes.

00108 {
00109     memcpy(&bytes[start], &collection[startIndex], stop - start);
00110 }

virtual void Core::ByteArray::replace long  start,
long  stop,
const SequenceableCollection collection,
long  startIndex
[inline, virtual]
 

Reimplemented from Core::SequenceableCollection.

Definition at line 103 of file ByteArray.h.

References Core::SequenceableCollection::replace().

00105     { SequenceableCollection::replace(start, stop, collection, startIndex); }

void ByteArray::replace long  start,
long  stop,
unsigned char *  collection
[virtual]
 

Definition at line 101 of file ByteArray.cc.

References replace().

00102 {
00103     replace(start, stop, collection, 0);
00104 }

virtual void Core::ByteArray::replace long  start,
long  stop,
const SequenceableCollection collection
[inline, virtual]
 

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); }

virtual long int Core::ByteArray::size void   )  const [inline, virtual]
 

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; }

InternalEncodedStreamFactory * ByteArray::withEncoding const char *  encoding  )  [virtual]
 

Definition at line 248 of file ByteArray.cc.

References withEncoding().

00249 {
00250     return withEncoding(new String(encoding));
00251 }

InternalEncodedStreamFactory * ByteArray::withEncoding String encoding  )  [virtual]
 

Definition at line 243 of file ByteArray.cc.

Referenced by asStringWithEncoding(), and withEncoding().

00244 {
00245     return new InternalEncodedStreamFactory(this, encoding);
00246 }


Member Data Documentation

unsigned char* Core::ByteArray::bytes [protected]
 

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().

long Core::ByteArray::tally [protected]
 

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().


The documentation for this class was generated from the following files:
Generated on Mon Nov 27 09:51:07 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2