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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.OutputStream;
  18. import java.io.IOException;
  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. public void write(byte[] data) {
  34. write(data, 0, data.length);
  35. }
  36. public void write(byte[] data, int off, int len) {
  37. enlarge(len);
  38. System.arraycopy(data, off, buf, count, len);
  39. count += len;
  40. }
  41. public void write(int b) {
  42. enlarge(1);
  43. int oldCount = count;
  44. buf[oldCount] = (byte)b;
  45. count = oldCount + 1;
  46. }
  47. public void writeShort(int s) {
  48. enlarge(2);
  49. int oldCount = count;
  50. buf[oldCount] = (byte)(s >>> 8);
  51. buf[oldCount + 1] = (byte)s;
  52. count = oldCount + 2;
  53. }
  54. public void writeInt(int i) {
  55. enlarge(4);
  56. int oldCount = count;
  57. buf[oldCount] = (byte)(i >>> 24);
  58. buf[oldCount + 1] = (byte)(i >>> 16);
  59. buf[oldCount + 2] = (byte)(i >>> 8);
  60. buf[oldCount + 3] = (byte)i;
  61. count = oldCount + 4;
  62. }
  63. public void writeLong(long i) {
  64. enlarge(8);
  65. int oldCount = count;
  66. buf[oldCount] = (byte)(i >>> 56);
  67. buf[oldCount + 1] = (byte)(i >>> 48);
  68. buf[oldCount + 2] = (byte)(i >>> 40);
  69. buf[oldCount + 3] = (byte)(i >>> 32);
  70. buf[oldCount + 4] = (byte)(i >>> 24);
  71. buf[oldCount + 5] = (byte)(i >>> 16);
  72. buf[oldCount + 6] = (byte)(i >>> 8);
  73. buf[oldCount + 7] = (byte)i;
  74. count = oldCount + 8;
  75. }
  76. public void writeFloat(float v) {
  77. writeInt(Float.floatToIntBits(v));
  78. }
  79. public void writeDouble(double v) {
  80. writeLong(Double.doubleToLongBits(v));
  81. }
  82. public void writeUTF(String s) {
  83. int sLen = s.length();
  84. int pos = count;
  85. enlarge(sLen + 2);
  86. byte[] buffer = buf;
  87. buffer[pos++] = (byte)(sLen >>> 8);
  88. buffer[pos++] = (byte)sLen;
  89. for (int i = 0; i < sLen; ++i) {
  90. char c = s.charAt(i);
  91. if (0x01 <= c && c <= 0x7f)
  92. buffer[pos++] = (byte)c;
  93. else {
  94. writeUTF2(s, sLen, i);
  95. return;
  96. }
  97. }
  98. count = pos;
  99. }
  100. private void writeUTF2(String s, int sLen, int offset) {
  101. int size = sLen;
  102. for (int i = offset; i < sLen; i++) {
  103. int c = s.charAt(i);
  104. if (c > 0x7ff)
  105. size += 2; // 3 bytes code
  106. else if (c == 0 || c > 0x7f)
  107. ++size; // 2 bytes code
  108. }
  109. if (size > 65535)
  110. throw new RuntimeException(
  111. "encoded string too long: " + sLen + size + " bytes");
  112. enlarge(size + 2);
  113. int pos = count;
  114. byte[] buffer = buf;
  115. buffer[pos] = (byte)(size >>> 8);
  116. buffer[pos + 1] = (byte)size;
  117. pos += 2 + offset;
  118. for (int j = offset; j < sLen; ++j) {
  119. int c = s.charAt(j);
  120. if (0x01 <= c && c <= 0x7f)
  121. buffer[pos++] = (byte) c;
  122. else if (c > 0x07ff) {
  123. buffer[pos] = (byte)(0xe0 | ((c >> 12) & 0x0f));
  124. buffer[pos + 1] = (byte)(0x80 | ((c >> 6) & 0x3f));
  125. buffer[pos + 2] = (byte)(0x80 | (c & 0x3f));
  126. pos += 3;
  127. }
  128. else {
  129. buffer[pos] = (byte)(0xc0 | ((c >> 6) & 0x1f));
  130. buffer[pos + 1] = (byte)(0x80 | (c & 0x3f));
  131. pos += 2;
  132. }
  133. }
  134. count = pos;
  135. }
  136. public void write(int pos, int value) {
  137. buf[pos] = (byte)value;
  138. }
  139. public void writeShort(int pos, int value) {
  140. buf[pos] = (byte)(value >>> 8);
  141. buf[pos + 1] = (byte)value;
  142. }
  143. public void writeInt(int pos, int value) {
  144. buf[pos] = (byte)(value >>> 24);
  145. buf[pos + 1] = (byte)(value >>> 16);
  146. buf[pos + 2] = (byte)(value >>> 8);
  147. buf[pos + 3] = (byte)value;
  148. }
  149. public byte[] toByteArray() {
  150. byte[] buf2 = new byte[count];
  151. System.arraycopy(buf, 0, buf2, 0, count);
  152. return buf2;
  153. }
  154. public void writeTo(OutputStream out) throws IOException {
  155. out.write(buf, 0, count);
  156. }
  157. public void enlarge(int delta) {
  158. int newCount = count + delta;
  159. if (newCount > buf.length) {
  160. int newLen = buf.length << 1;
  161. byte[] newBuf = new byte[newLen > newCount ? newLen : newCount];
  162. System.arraycopy(buf, 0, newBuf, 0, count);
  163. buf = newBuf;
  164. }
  165. }
  166. }