datastream.monkey2 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. Namespace std.stream
  2. #rem monkeydoc DataStream class.
  3. #end
  4. Class DataStream Extends Stream
  5. #rem monkeydoc Creates a new datastream.
  6. #end
  7. Method New( buf:DataBuffer )
  8. _buf=buf
  9. _pos=0
  10. _end=_buf.Length
  11. End
  12. #rem monkeydoc True if no more data can be read from or written to the stream.
  13. #end
  14. Property Eof:Bool() Override
  15. Return _pos>=_end
  16. End
  17. #rem monkeydoc Current datastream position.
  18. #end
  19. Property Position:Int() Override
  20. Return _pos
  21. End
  22. #rem monkeydoc Current datastream length.
  23. #end
  24. Property Length:Int() Override
  25. Return _end
  26. End
  27. #rem monkeydoc Closes the datastream.
  28. Closing a datastream also sets its position and length to 0.
  29. #end
  30. Method Close() Override
  31. If Not _buf Return
  32. _buf=Null
  33. _pos=0
  34. _end=0
  35. End
  36. #rem monkeydoc Seeks to a position in the datastream.
  37. @param position The position to seek to.
  38. #end
  39. Method Seek( position:Int ) Override
  40. DebugAssert( position>=0 And position<=_end )
  41. _pos=position
  42. End
  43. #rem monkeydoc Reads data from the datastream.
  44. @param buf A pointer to the memory to read the data into.
  45. @param count The number of bytes to read.
  46. @return The number of bytes actually read.
  47. #end
  48. Method Read:Int( buf:Void Ptr,count:Int ) Override
  49. count=Clamp( count,0,_end-_pos )
  50. libc.memcpy( buf,_buf.Data+_pos,count )
  51. _pos+=count
  52. Return count
  53. End
  54. #rem monkeydoc Writes data to the datastream.
  55. @param buf A pointer to the memory to write the data from.
  56. @param count The number of bytes to write.
  57. @return The number of bytes actually written.
  58. #end
  59. Method Write:Int( buf:Void Ptr,count:Int ) Override
  60. count=Clamp( count,0,_end-_pos )
  61. libc.memcpy( _buf.Data+_pos,buf,count )
  62. _pos+=count
  63. Return count
  64. End
  65. Private
  66. Field _buf:DataBuffer
  67. Field _pos:Int
  68. Field _end:Int
  69. End