您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InStream.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. //
  19. // rdr::InStream marshalls data from a buffer stored in RDR (RFB Data
  20. // Representation).
  21. //
  22. abstract public class InStream {
  23. // check() ensures there is buffer data for at least one item of size
  24. // itemSize bytes. Returns the number of items in the buffer (up to a
  25. // maximum of nItems).
  26. public final int check(int itemSize, int nItems) throws Exception {
  27. if (ptr + itemSize * nItems > end) {
  28. if (ptr + itemSize > end)
  29. return overrun(itemSize, nItems);
  30. nItems = (end - ptr) / itemSize;
  31. }
  32. return nItems;
  33. }
  34. public final void check(int itemSize) throws Exception {
  35. if (ptr + itemSize > end)
  36. overrun(itemSize, 1);
  37. }
  38. // readU/SN() methods read unsigned and signed N-bit integers.
  39. public final int readS8() throws Exception {
  40. check(1); return b[ptr++];
  41. }
  42. public final int readS16() throws Exception {
  43. check(2); int b0 = b[ptr++];
  44. int b1 = b[ptr++] & 0xff; return b0 << 8 | b1;
  45. }
  46. public final int readS32() throws Exception {
  47. check(4); int b0 = b[ptr++];
  48. int b1 = b[ptr++] & 0xff;
  49. int b2 = b[ptr++] & 0xff;
  50. int b3 = b[ptr++] & 0xff;
  51. return b0 << 24 | b1 << 16 | b2 << 8 | b3;
  52. }
  53. public final int readU8() throws Exception {
  54. return readS8() & 0xff;
  55. }
  56. public final int readU16() throws Exception {
  57. return readS16() & 0xffff;
  58. }
  59. public final int readU32() throws Exception {
  60. return readS32() & 0xffffffff;
  61. }
  62. // readString() reads a string - a U32 length followed by the data.
  63. public final String readString() throws Exception {
  64. int len = readU32();
  65. if (len > maxStringLength)
  66. throw new Exception("InStream max string length exceeded");
  67. char[] str = new char[len];
  68. int i = 0;
  69. while (i < len) {
  70. int j = i + check(1, len - i);
  71. while (i < j) {
  72. str[i++] = (char)b[ptr++];
  73. }
  74. }
  75. return new String(str);
  76. }
  77. // maxStringLength protects against allocating a huge buffer. Set it
  78. // higher if you need longer strings.
  79. public static int maxStringLength = 65535;
  80. public final void skip(int bytes) throws Exception {
  81. while (bytes > 0) {
  82. int n = check(1, bytes);
  83. ptr += n;
  84. bytes -= n;
  85. }
  86. }
  87. // readBytes() reads an exact number of bytes into an array at an offset.
  88. public void readBytes(byte[] data, int offset, int length) throws Exception {
  89. int offsetEnd = offset + length;
  90. while (offset < offsetEnd) {
  91. int n = check(1, offsetEnd - offset);
  92. System.arraycopy(b, ptr, data, offset, n);
  93. ptr += n;
  94. offset += n;
  95. }
  96. }
  97. // readOpaqueN() reads a quantity "without byte-swapping". Because java has
  98. // no byte-ordering, we just use big-endian.
  99. public final int readOpaque8() throws Exception {
  100. return readU8();
  101. }
  102. public final int readOpaque16() throws Exception {
  103. return readU16();
  104. }
  105. public final int readOpaque32() throws Exception {
  106. return readU32();
  107. }
  108. public final int readOpaque24A() throws Exception {
  109. check(3); int b0 = b[ptr++];
  110. int b1 = b[ptr++]; int b2 = b[ptr++];
  111. return b0 << 24 | b1 << 16 | b2 << 8;
  112. }
  113. public final int readOpaque24B() throws Exception {
  114. check(3); int b0 = b[ptr++];
  115. int b1 = b[ptr++]; int b2 = b[ptr++];
  116. return b0 << 16 | b1 << 8 | b2;
  117. }
  118. // pos() returns the position in the stream.
  119. abstract public int pos();
  120. // bytesAvailable() returns true if at least one byte can be read from the
  121. // stream without blocking. i.e. if false is returned then readU8() would
  122. // block.
  123. public boolean bytesAvailable() { return end != ptr; }
  124. // getbuf(), getptr(), getend() and setptr() are "dirty" methods which allow
  125. // you to manipulate the buffer directly. This is useful for a stream which
  126. // is a wrapper around an underlying stream.
  127. public final byte[] getbuf() { return b; }
  128. public final int getptr() { return ptr; }
  129. public final int getend() { return end; }
  130. public final void setptr(int p) { ptr = p; }
  131. // overrun() is implemented by a derived class to cope with buffer overrun.
  132. // It ensures there are at least itemSize bytes of buffer data. Returns
  133. // the number of items in the buffer (up to a maximum of nItems). itemSize
  134. // is supposed to be "small" (a few bytes).
  135. abstract protected int overrun(int itemSize, int nItems) throws Exception;
  136. protected InStream() {}
  137. protected byte[] b;
  138. protected int ptr;
  139. protected int end;
  140. }