avmshell::ByteArrayObject Class Reference

#include <ByteArrayGlue.h>

List of all members.

Public Member Functions

 ByteArrayObject (VTable *ivtable, ScriptObject *delegate)
void fill (const void *b, int len)
void checkNull (void *instance, const char *name)
uint32 getLength ()
void setLength (uint32 newLength)
virtual bool hasAtomProperty (Atom name) const
virtual void setAtomProperty (Atom name, Atom value)
virtual Atom getAtomProperty (Atom name) const
virtual Atom getUintProperty (uint32 i) const
virtual void setUintProperty (uint32 i, Atom value)
void readBytes (ByteArrayObject *bytes, uint32 offset, uint32 length)
void writeBytes (ByteArrayObject *bytes, uint32 offset, uint32 length)
String_toString ()
void zlib_compress ()
void zlib_uncompress ()
void writeBoolean (bool value)
void writeByte (int value)
void writeShort (int value)
void writeInt (int value)
void writeUnsignedInt (uint32 value)
void writeFloat (double value)
void writeDouble (double value)
void writeUTF (String *value)
void writeUTFBytes (String *value)
bool readBoolean ()
int readByte ()
int readUnsignedByte ()
int readShort ()
int readUnsignedShort ()
int readInt ()
uint32 readUnsignedInt ()
double readFloat ()
double readDouble ()
StringreadUTF ()
StringreadUTFBytes (uint32 length)
int available ()
int getFilePointer ()
void seek (int offset)
uint32 get_length ()
void set_length (uint32 value)
Stringp get_endian ()
void set_endian (Stringp type)
ByteArrayGetByteArray ()
void writeFile (Stringp filename)

Private Attributes

MMgc::Cleaner c
ByteArrayFile m_byteArray


Detailed Description

Definition at line 90 of file ByteArrayGlue.h.


Constructor & Destructor Documentation

avmshell::ByteArrayObject::ByteArrayObject VTable *  ivtable,
ScriptObject *  delegate
 


Member Function Documentation

String * avmshell::ByteArrayObject::_toString  ) 
 

Definition at line 347 of file ByteArrayGlue.cpp.

References buffer, c, avmshell::ByteArray::GetBuffer(), avmshell::ByteArray::GetLength(), util::threadpool::i, avmplus::String::lockBuffer(), and m_byteArray.

00348     {
00349         unsigned char *c = (unsigned char*)m_byteArray.GetBuffer();
00350         uint32 len = m_byteArray.GetLength();
00351 
00352         if (len >= 3)
00353         {
00354             // UTF8 BOM
00355             if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf))
00356             {
00357                 return core()->newString(((char *)c) + 3, len - 3);
00358             }
00359             else if ((c[0] == 0xfe) && (c[1] == 0xff))
00360             {
00361                 //UTF-16 big endian
00362                 c += 2;
00363                 len = (len - 2) >> 1;
00364                 Stringp out = new (core()->GetGC()) String(len);
00365                 wchar *buffer = out->lockBuffer();
00366                 for (uint32 i = 0; i < len; i++)
00367                 {
00368                     buffer[i] = (c[0] << 8) + c[1];
00369                     c += 2;
00370                 }
00371                 out->unlockBuffer();
00372 
00373                 return out;
00374             }
00375             else if ((c[0] == 0xff) && (c[1] == 0xfe))
00376             {
00377                 //UTF-16 little endian
00378                 c += 2;
00379                 len = (len - 2) >> 1;
00380                 Stringp out = new (core()->GetGC()) String(len);
00381                 wchar *buffer = out->lockBuffer();
00382                 for (uint32 i = 0; i < len; i++)
00383                 {
00384                     buffer[i] = (c[1] << 8) + c[0];
00385                     c += 2;
00386                 }
00387                 out->unlockBuffer();
00388                 return out;
00389             }
00390         }
00391 
00392         return core()->newString(((char *)c), len);
00393     }

int avmshell::ByteArrayObject::available  ) 
 

Definition at line 335 of file ByteArrayGlue.cpp.

References avmshell::ByteArrayFile::Available(), and m_byteArray.

00336     {
00337         return m_byteArray.Available();
00338     }

void avmshell::ByteArrayObject::checkNull void *  instance,
const char *  name
 

Definition at line 540 of file ByteArrayGlue.cpp.

References avmplus::ErrorConstants::kNullArgumentError, NULL, and toplevel.

Referenced by readBytes(), and writeBytes().

00541     {
00542         if (instance == NULL) {
00543             toplevel()->throwTypeError(kNullArgumentError, core()->toErrorString(name));
00544         }
00545     }   

void avmshell::ByteArrayObject::fill const void *  b,
int  len
 

Definition at line 598 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::ByteArrayFile::Write().

00599     {
00600         m_byteArray.Write(b, len);
00601     }

Stringp avmshell::ByteArrayObject::get_endian  ) 
 

Definition at line 664 of file ByteArrayGlue.cpp.

References avmshell::DataIOBase::GetEndian(), avmshell::kBigEndian, and m_byteArray.

00665     {
00666         return (m_byteArray.GetEndian() == kBigEndian) ? core()->constantString("bigEndian") : core()->constantString("littleEndian");
00667     }

uint32 avmshell::ByteArrayObject::get_length  ) 
 

Definition at line 320 of file ByteArrayGlue.cpp.

References avmshell::ByteArray::GetLength(), and m_byteArray.

Referenced by avmshell::DomainObject::loadBytes().

00321     {
00322         return m_byteArray.GetLength();
00323     }

Atom avmshell::ByteArrayObject::getAtomProperty Atom  name  )  const [virtual]
 

Definition at line 283 of file ByteArrayGlue.cpp.

References avmplus::AvmCore::getIndexFromAtom(), avmshell::ByteArray::GetLength(), avmplus::AvmCore::intToAtom(), and m_byteArray.

Referenced by hasAtomProperty().

00284     {
00285         AvmCore *core = this->core();
00286         uint32 index;
00287         if (core->getIndexFromAtom(name, &index)) {
00288             if (index < (uint32) m_byteArray.GetLength()) {
00289                 return core->intToAtom(m_byteArray[index]);
00290             } else {
00291                 return undefinedAtom;
00292             }
00293         }
00294 
00295         return ScriptObject::getAtomProperty(name);
00296     }

ByteArray& avmshell::ByteArrayObject::GetByteArray  )  [inline]
 

Definition at line 149 of file ByteArrayGlue.h.

References m_byteArray.

Referenced by avmshell::DomainObject::loadBytes(), readBytes(), and writeBytes().

00149 { return m_byteArray; }

int avmshell::ByteArrayObject::getFilePointer  ) 
 

Definition at line 330 of file ByteArrayGlue.cpp.

References avmshell::ByteArrayFile::GetFilePointer(), and m_byteArray.

00331     {
00332         return m_byteArray.GetFilePointer();
00333     }

uint32 avmshell::ByteArrayObject::getLength  )  [inline]
 

Definition at line 99 of file ByteArrayGlue.h.

References avmshell::ByteArray::GetLength(), and m_byteArray.

Referenced by writeBytes().

00099 { return m_byteArray.GetLength(); }

Atom avmshell::ByteArrayObject::getUintProperty uint32  i  )  const [virtual]
 

Definition at line 269 of file ByteArrayGlue.cpp.

References avmshell::ByteArray::GetLength(), and m_byteArray.

00270     {
00271         if (i < (uint32)m_byteArray.GetLength()) {
00272             return core()->intToAtom(m_byteArray[i]);
00273         } else {
00274             return undefinedAtom;
00275         }
00276     }

bool avmshell::ByteArrayObject::hasAtomProperty Atom  name  )  const [virtual]
 

Definition at line 310 of file ByteArrayGlue.cpp.

References getAtomProperty(), and avmplus::AtomConstants::undefinedAtom.

00311     {
00312         return ScriptObject::hasAtomProperty(name) || getAtomProperty(name) != undefinedAtom;
00313     }

bool avmshell::ByteArrayObject::readBoolean  ) 
 

Definition at line 435 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadBoolean().

00436     {
00437         return m_byteArray.ReadBoolean();
00438     }

int avmshell::ByteArrayObject::readByte  ) 
 

Definition at line 395 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadU8().

00396     {
00397         return (signed char)m_byteArray.ReadU8();
00398     }

void avmshell::ByteArrayObject::readBytes ByteArrayObject bytes,
uint32  offset,
uint32  length
 

Definition at line 561 of file ByteArrayGlue.cpp.

References avmshell::ByteArrayFile::Available(), checkNull(), GetByteArray(), m_byteArray, and avmshell::DataInput::ReadByteArray().

00564     {
00565         checkNull(bytes, "bytes");
00566 
00567         if (length == 0) {
00568             length = m_byteArray.Available();
00569         }
00570         
00571         m_byteArray.ReadByteArray(bytes->GetByteArray(),
00572                                   offset,
00573                                   length);
00574     }

double avmshell::ByteArrayObject::readDouble  ) 
 

Definition at line 430 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadDouble().

00431     {
00432         return m_byteArray.ReadDouble();
00433     }

double avmshell::ByteArrayObject::readFloat  ) 
 

Definition at line 425 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadFloat().

00426     {
00427         return m_byteArray.ReadFloat();
00428     }

int avmshell::ByteArrayObject::readInt  ) 
 

Definition at line 415 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadU32().

00416     {
00417         return (int)m_byteArray.ReadU32();      
00418     }

int avmshell::ByteArrayObject::readShort  ) 
 

Definition at line 405 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadU16().

00406     {
00407         return (short)m_byteArray.ReadU16();
00408     }

int avmshell::ByteArrayObject::readUnsignedByte  ) 
 

Definition at line 400 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadU8().

00401     {
00402         return m_byteArray.ReadU8();
00403     }

uint32 avmshell::ByteArrayObject::readUnsignedInt  ) 
 

Definition at line 420 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadU32().

00421     {
00422         return m_byteArray.ReadU32();       
00423     }

int avmshell::ByteArrayObject::readUnsignedShort  ) 
 

Definition at line 410 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadU16().

00411     {
00412         return m_byteArray.ReadU16();
00413     }

String * avmshell::ByteArrayObject::readUTF  ) 
 

Definition at line 576 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadUTF().

00577     {
00578         return m_byteArray.ReadUTF();
00579     }

String * avmshell::ByteArrayObject::readUTFBytes uint32  length  ) 
 

Definition at line 581 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataInput::ReadUTFBytes().

00582     {
00583         return m_byteArray.ReadUTFBytes(length);
00584     }

void avmshell::ByteArrayObject::seek int  offset  ) 
 

Definition at line 340 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::ByteArrayFile::Seek().

00341     {
00342         if (offset >= 0) {
00343             m_byteArray.Seek(offset);
00344         }
00345     }

void avmshell::ByteArrayObject::set_endian Stringp  type  ) 
 

void avmshell::ByteArrayObject::set_length uint32  value  ) 
 

Definition at line 325 of file ByteArrayGlue.cpp.

References setLength().

00326     {
00327         setLength(value);
00328     }

void avmshell::ByteArrayObject::setAtomProperty Atom  name,
Atom  value
[virtual]
 

Definition at line 298 of file ByteArrayGlue.cpp.

References avmplus::AvmCore::getIndexFromAtom(), avmplus::AvmCore::integer(), and m_byteArray.

00299     {
00300         AvmCore *core = this->core();
00301         uint32 index;
00302         if (core->getIndexFromAtom(name, &index)) {
00303             int intValue = core->integer(value);
00304             m_byteArray[index] = (U8)(intValue);
00305         } else {
00306             ScriptObject::setAtomProperty(name, value);
00307         }
00308     }

void avmshell::ByteArrayObject::setLength uint32  newLength  ) 
 

Definition at line 315 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::ByteArrayFile::SetLength().

Referenced by set_length().

00316     {
00317         m_byteArray.SetLength(newLength);
00318     }

void avmshell::ByteArrayObject::setUintProperty uint32  i,
Atom  value
[virtual]
 

Definition at line 278 of file ByteArrayGlue.cpp.

References m_byteArray.

00279     {
00280         m_byteArray[i] = (U8)(core()->integer(value));
00281     }

void avmshell::ByteArrayObject::writeBoolean bool  value  ) 
 

Definition at line 440 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataOutput::WriteBoolean().

00441     {
00442         m_byteArray.WriteBoolean(value);
00443     }

void avmshell::ByteArrayObject::writeByte int  value  ) 
 

Definition at line 445 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataOutput::WriteU8().

00446     {
00447         m_byteArray.WriteU8((U8)value);
00448     }

void avmshell::ByteArrayObject::writeBytes ByteArrayObject bytes,
uint32  offset,
uint32  length
 

Definition at line 546 of file ByteArrayGlue.cpp.

References checkNull(), GetByteArray(), getLength(), m_byteArray, and avmshell::DataOutput::WriteByteArray().

00549     {
00550         checkNull(bytes, "bytes");
00551 
00552         if (length == 0) {
00553             length = bytes->getLength() - offset;
00554         }
00555         
00556         m_byteArray.WriteByteArray(bytes->GetByteArray(),
00557                                    offset,
00558                                    length);
00559     }

void avmshell::ByteArrayObject::writeDouble double  value  ) 
 

Definition at line 470 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataOutput::WriteDouble().

00471     {
00472         m_byteArray.WriteDouble(value);
00473     }

void avmshell::ByteArrayObject::writeFile Stringp  filename  ) 
 

void avmshell::ByteArrayObject::writeFloat double  value  ) 
 

Definition at line 465 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataOutput::WriteFloat().

00466     {
00467         m_byteArray.WriteFloat((float)value);
00468     }

void avmshell::ByteArrayObject::writeInt int  value  ) 
 

Definition at line 455 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataOutput::WriteU32().

00456     {
00457         m_byteArray.WriteU32((uint32)value);
00458     }

void avmshell::ByteArrayObject::writeShort int  value  ) 
 

Definition at line 450 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataOutput::WriteU16().

00451     {
00452         m_byteArray.WriteU16((unsigned short)value);
00453     }

void avmshell::ByteArrayObject::writeUnsignedInt uint32  value  ) 
 

Definition at line 460 of file ByteArrayGlue.cpp.

References m_byteArray, and avmshell::DataOutput::WriteU32().

00461     {
00462         m_byteArray.WriteU32(value);
00463     }

void avmshell::ByteArrayObject::writeUTF String *  value  ) 
 

void avmshell::ByteArrayObject::writeUTFBytes String *  value  ) 
 

void avmshell::ByteArrayObject::zlib_compress  ) 
 

Definition at line 475 of file ByteArrayGlue.cpp.

References avmshell::ByteArray::GetBuffer(), avmshell::ByteArray::GetLength(), m_byteArray, avmshell::ByteArrayFile::SetLength(), and avmshell::ByteArrayFile::Write().

00476     {
00477         int len = m_byteArray.GetLength();
00478         if (!len) // empty buffer should give us a empty result
00479             return; 
00480         unsigned long gzlen = len * 3/2 + 32; // enough for growth plus zlib headers
00481         U8 *gzdata = new U8[gzlen];
00482 
00483         // Use zlib to compress the data
00484         compress2((U8*)gzdata, (unsigned long*)&gzlen,
00485                 m_byteArray.GetBuffer(), len, 9);
00486 
00487         // Replace the byte array with the compressed data
00488         m_byteArray.SetLength(0);
00489         //m_byteArray.WriteU32((U32)len);
00490         m_byteArray.Write(gzdata, gzlen);
00491 
00492         delete [] gzdata;
00493     }

void avmshell::ByteArrayObject::zlib_uncompress  ) 
 

Definition at line 495 of file ByteArrayGlue.cpp.

References avmshell::PlatformZlibStream::AvailOut(), buffer, avmshell::ByteArray::GetBuffer(), avmshell::ByteArray::GetLength(), avmshell::PlatformZlibStream::InflateWithStatus(), avmplus::ErrorConstants::kShellCompressedDataError, m_byteArray, avmshell::ByteArrayFile::Seek(), avmshell::PlatformZlibStream::SetAvailIn(), avmshell::PlatformZlibStream::SetAvailOut(), avmshell::ByteArrayFile::SetLength(), avmshell::PlatformZlibStream::SetNextIn(), avmshell::PlatformZlibStream::SetNextOut(), toplevel, and avmshell::ByteArrayFile::Write().

00496     {
00497         // Snapshot the compressed data.
00498         unsigned long gzlen = m_byteArray.GetLength();
00499         if (!gzlen) // empty buffer should give us a empty result
00500             return; 
00501 
00502         U8 *gzdata = new U8[gzlen];
00503         memcpy(gzdata, m_byteArray.GetBuffer(), gzlen);
00504 
00505         // Clear the buffer
00506         m_byteArray.Seek(0);
00507         m_byteArray.SetLength(0);
00508 
00509         // The following block is to force destruction
00510         // of zstream before potential exception throw.
00511         int error = Z_OK;
00512         {
00513             // Decompress the data
00514             PlatformZlibStream zstream;
00515             zstream.SetNextIn(gzdata);
00516             zstream.SetAvailIn(gzlen);
00517 
00518             const int kBufferSize = 8192;
00519             U8 *buffer = new U8 [kBufferSize];
00520 
00521             do {
00522                 zstream.SetNextOut(buffer);
00523                 zstream.SetAvailOut(kBufferSize);
00524                 error = zstream.InflateWithStatus();
00525                 m_byteArray.Write(buffer, kBufferSize-zstream.AvailOut());
00526             } while (error == Z_OK);
00527 
00528             delete [] buffer;
00529             delete [] gzdata;
00530         }
00531 
00532         // position byte array at the beginning
00533         m_byteArray.Seek(0);
00534 
00535         if (error != Z_OK && error != Z_STREAM_END) {
00536             toplevel()->throwError(kShellCompressedDataError);
00537         }
00538     }


Member Data Documentation

MMgc::Cleaner avmshell::ByteArrayObject::c [private]
 

Definition at line 154 of file ByteArrayGlue.h.

Referenced by _toString().

ByteArrayFile avmshell::ByteArrayObject::m_byteArray [private]
 

Definition at line 155 of file ByteArrayGlue.h.

Referenced by _toString(), available(), fill(), get_endian(), get_length(), getAtomProperty(), GetByteArray(), getFilePointer(), getLength(), getUintProperty(), readBoolean(), readByte(), readBytes(), readDouble(), readFloat(), readInt(), readShort(), readUnsignedByte(), readUnsignedInt(), readUnsignedShort(), readUTF(), readUTFBytes(), seek(), setAtomProperty(), setLength(), setUintProperty(), writeBoolean(), writeByte(), writeBytes(), writeDouble(), writeFloat(), writeInt(), writeShort(), writeUnsignedInt(), zlib_compress(), and zlib_uncompress().


The documentation for this class was generated from the following files:
Generated on Sun Oct 12 18:51:31 2008 for Tamarin by  doxygen 1.4.6