1. -- ************************************************************************
  2. -- Script:   BYTEARRAY INTERFACE
  3. -- Version:  0.2
  4. -- Date:     2008-11-30
  5. --
  6. -- + allows to create ByteArray objects by using ByteArray(<string>) or ByteArray([intSize],[intInitialValue])
  7. -- + provides functions readByteArray/writeByteArray
  8. -- + provides using bytesRemaining
  9. --
  10. -- ************************************************************************
  11.  
  12. on ByteArray p1, p2
  13.   -- return script("ByteArray").new(p1,p2)
  14.  
  15.   -- simulates ByteArray by adding overwriting ilk (as property and function) and string()
  16.   return script("ByteArrayMimicry").new(p1,p2)
  17. end
  18.  
  19. --------------------------------------
  20. -- readByteArray (intBytes,[byteArray], [intOffset] )
  21. -- Reads intBytes number of bytes into ByteArray. If an existing ByteArray is
  22. -- passed as input read data shall be appended to that byte array starting
  23. -- with intOffset position.
  24. --------------------------------------
  25. on readByteArray (fp, intBytes, byteArray, intOffset)
  26.   data = fread(fp, intBytes)
  27.   if byteArray.ilk=#byteArray then
  28.     if integerP(intOffset) then byteArray.position=intOffset+1
  29.     byteArray.writeRawString(data, intBytes)
  30.   else
  31.     return ByteArray(data)      
  32.   end if
  33. end
  34.  
  35. --------------------------------------
  36. -- writeByteArray (byteArray, [intOffset],[intLen])
  37. -- Writes a ByteAray or part of it into a file.
  38. --------------------------------------
  39. on writeByteArray (fp, byteArray, intOffset, intLen)
  40.   if not integerP(intOffset) then intOffset=0
  41.   pos = byteArray.position
  42.   byteArray.position = intOffset+1
  43.   if not integerP(intLen) then intLen=byteArray.length-byteArray.position+1
  44.   data = byteArray.readRawString(intLen)
  45.   byteArray.position = pos
  46.   fwrite(fp, data)
  47. end
  48.  
  49. --------------------------------------
  50. -- to support BytesRemaining as (pseudo)property: e.g. put ba.bytesRemaining
  51. --------------------------------------
  52. on bytesRemaining ba
  53.   return ba.bytesRemaining()
  54. end
  55.  
[raw code]