Advertisement
devsaider

ByteArray.PHP

Mar 24th, 2013
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.48 KB | None | 0 0
  1. /*
  2. @author: Devsaider;
  3. @author-uri: devsaider.ru, devmices.ru;
  4. @description: Original ByteArray by L'fleur rewrite for PHP by Devsaider;
  5. http://pastebin.com/u/Devsaider
  6. */
  7.  
  8. class ByteArray{
  9.  
  10. private $BytesString;
  11.  
  12. function ByteArray($bytes = "") {
  13.     $this->BytesString = $bytes;
  14. }
  15.  
  16. function writeBoolean($value = 1) {
  17.     $this->BytesString .= $this->writeByte($value, False);
  18. }
  19.  
  20. function writeByte($value, $noReturning=True) {
  21.     if ($noReturning) $this->BytesString .= pack("C", $value);
  22.     else return pack("C", $value);
  23. }
  24.  
  25. function writeBytes($value) {
  26.     $this->BytesString .= $value;
  27. }
  28.  
  29. function writeInt($value) {
  30.     $this->BytesString .= pack('N', $value);
  31. }
  32.  
  33. function writeShort($value) {
  34.     $this->BytesString .= pack('n', $value);
  35. }
  36.  
  37. function writeUTF($value) {
  38.     $valueSize = strlen($value);
  39.     $this->writeShort($valueSize);
  40.     $this->writeUTFBytes($value);
  41. }
  42.  
  43. function writeUTFBytes($value) {
  44.     $this->BytesString .= $value;
  45. }
  46.  
  47. function length() {
  48.     return strlen($this->BytesString);
  49. }
  50.  
  51. function toString() {
  52.     return $this->BytesString;
  53. }
  54.  
  55. function toPack() {
  56.     $value = pack('N', strlen($this->BytesString)+4);
  57.     return $value.$this->BytesString;
  58. }
  59.  
  60. function getSize() {
  61.     $value = unpack('N', substr($this->BytesString, 0, 4));
  62.     return $value[1];
  63. }
  64.  
  65. function readBy($Pos) {
  66.     $this->BytesString = substr($this->BytesString, $Pos);
  67.     return $this->BytesString;
  68. }
  69.  
  70. function loc($byte) {
  71.     $loc = substr($this->BytesString, 0, $byte);
  72.     $this->BytesString = substr($this->BytesString, $byte);
  73.     return unpack('C', $loc);
  74. }
  75.  
  76. function readInt() {
  77.     $size = unpack('N', substr($this->BytesString, 0, 4)); $size = $size[1];
  78.     $this->BytesString = substr($this->BytesString, 4);
  79.     return $size;
  80. }
  81.  
  82. function readUTF() {
  83.     $size = unpack('n', substr($this->BytesString, 0, 2)); $size = $size[1];
  84.     $string = substr($this->BytesString, 2, $size);
  85.     $this->BytesString = substr($this->BytesString, $size + 2);
  86.     return $string;
  87. }
  88.  
  89. function readShort() {
  90.     $size = unpack('n', substr($this->BytesString, 0, 2)); $size = $size[1];
  91.     $this->BytesString = substr($this->BytesString, 2);
  92.     return $size;
  93. }
  94.  
  95. function readBoolean() {
  96.     $loc = unpack('C', substr($this->BytesString, 0, 1)); $loc = $loc[1];
  97.     $this->BytesString = substr($this->BytesString, 1);
  98.     if ($loc == 1) return True;
  99.     else return False;
  100. }
  101.  
  102. function readByte() {
  103.     $byte = unpack('C', substr($this->BytesString, 0, 1)); $byte = $byte[1];
  104.     $this->BytesString = substr($this->BytesString, 1);
  105.     return $byte;
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement