You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ByteStream.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. final class ByteStream extends OutputStream {
  20. private byte[] buf;
  21. private int count;
  22. public ByteStream() { this(32); }
  23. public ByteStream(int size) {
  24. buf = new byte[size];
  25. count = 0;
  26. }
  27. public int getPos() { return count; }
  28. public int size() { return count; }
  29. public void writeBlank(int len) {
  30. enlarge(len);
  31. count += len;
  32. }
  33. @Override
  34. public void write(byte[] data) {
  35. write(data, 0, data.length);
  36. }
  37. @Override
  38. public void write(byte[] data, int off, int len) {
  39. enlarge(len);
  40. System.arraycopy(data, off, buf, count, len);
  41. count += len;
  42. }
  43. @Override
  44. public void write(int b) {
  45. enlarge(1);
  46. int oldCount = count;
  47. buf[oldCount] = (byte)b;
  48. count = oldCount + 1;
  49. }
  50. public void writeShort(int s) {
  51. enlarge(2);
  52. int oldCount = count;
  53. buf[oldCount] = (byte)(s >>> 8);
  54. buf[oldCount + 1] = (byte)s;
  55. count = oldCount + 2;
  56. }
  57. public void writeInt(int i) {
  58. enlarge(4);
  59. int oldCount = count;
  60. buf[oldCount] = (byte)(i >>> 24);
  61. buf[oldCount + 1] = (byte)(i >>> 16);
  62. buf[oldCount + 2] = (byte)(i >>> 8);
  63. buf[oldCount + 3] = (byte)i;
  64. count = oldCount + 4;
  65. }
  66. public void writeLong(long i) {
  67. enlarge(8);
  68. int oldCount = count;
  69. buf[oldCount] = (byte)(i >>> 56);
  70. buf[oldCount + 1] = (byte)(i >>> 48);
  71. buf[oldCount + 2] = (byte)(i >>> 40);
  72. buf[oldCount + 3] = (byte)(i >>> 32);
  73. buf[oldCount + 4] = (byte)(i >>> 24);
  74. buf[oldCount + 5] = (byte)(i >>> 16);
  75. buf[oldCount + 6] = (byte)(i >>> 8);
  76. buf[oldCount + 7] = (byte)i;
  77. count = oldCount + 8;
  78. }
  79. public void writeFloat(float v) {
  80. writeInt(Float.floatToIntBits(v));
  81. }
  82. public void writeDouble(double v) {
  83. writeLong(Double.doubleToLongBits(v));
  84. }
  85. public void writeUTF(String s) {
  86. int sLen = s.length();
  87. int pos = count;
  88. enlarge(sLen + 2);
  89. byte[] buffer = buf;
  90. buffer[pos++] = (byte)(sLen >>> 8);
  91. buffer[pos++] = (byte)sLen;
  92. for (int i = 0; i < sLen; ++i) {
  93. char c = s.charAt(i);
  94. if (0x01 <= c && c <= 0x7f)
  95. buffer[pos++] = (byte)c;
  96. else {
  97. writeUTF2(s, sLen, i);
  98. return;
  99. }
  100. }
  101. count = pos;
  102. }
  103. private void writeUTF2(String s, int sLen, int offset) {
  104. int size = sLen;
  105. for (int i = offset; i < sLen; i++) {
  106. int c = s.charAt(i);
  107. if (c > 0x7ff)
  108. size += 2; // 3 bytes code
  109. else if (c == 0 || c > 0x7f)
  110. ++size; // 2 bytes code
  111. }
  112. if (size > 65535)
  113. throw new RuntimeException(
  114. "encoded string too long: " + sLen + size + " bytes");
  115. enlarge(size + 2);
  116. int pos = count;
  117. byte[] buffer = buf;
  118. buffer[pos] = (byte)(size >>> 8);
  119. buffer[pos + 1] = (byte)size;
  120. pos += 2 + offset;
  121. for (int j = offset; j < sLen; ++j) {
  122. int c = s.charAt(j);
  123. if (0x01 <= c && c <= 0x7f)
  124. buffer[pos++] = (byte) c;
  125. else if (c > 0x07ff) {
  126. buffer[pos] = (byte)(0xe0 | ((c >> 12) & 0x0f));
  127. buffer[pos + 1] = (byte)(0x80 | ((c >> 6) & 0x3f));
  128. buffer[pos + 2] = (byte)(0x80 | (c & 0x3f));
  129. pos += 3;
  130. }
  131. else {
  132. buffer[pos] = (byte)(0xc0 | ((c >> 6) & 0x1f));
  133. buffer[pos + 1] = (byte)(0x80 | (c & 0x3f));
  134. pos += 2;
  135. }
  136. }
  137. count = pos;
  138. }
  139. public void write(int pos, int value) {
  140. buf[pos] = (byte)value;
  141. }
  142. public void writeShort(int pos, int value) {
  143. buf[pos] = (byte)(value >>> 8);
  144. buf[pos + 1] = (byte)value;
  145. }
  146. public void writeInt(int pos, int value) {
  147. buf[pos] = (byte)(value >>> 24);
  148. buf[pos + 1] = (byte)(value >>> 16);
  149. buf[pos + 2] = (byte)(value >>> 8);
  150. buf[pos + 3] = (byte)value;
  151. }
  152. public byte[] toByteArray() {
  153. byte[] buf2 = new byte[count];
  154. System.arraycopy(buf, 0, buf2, 0, count);
  155. return buf2;
  156. }
  157. public void writeTo(OutputStream out) throws IOException {
  158. out.write(buf, 0, count);
  159. }
  160. public void enlarge(int delta) {
  161. int newCount = count + delta;
  162. if (newCount > buf.length) {
  163. int newLen = buf.length << 1;
  164. byte[] newBuf = new byte[newLen > newCount ? newLen : newCount];
  165. System.arraycopy(buf, 0, newBuf, 0, count);
  166. buf = newBuf;
  167. }
  168. }
  169. }