PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/org/objectweb/asm/ByteVector.java

#
Java | 281 lines | 144 code | 29 blank | 108 comment | 23 complexity | 5d66f621a960a23629a53e3eb50258c1 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /***
  2. * ASM: a very small and fast Java bytecode manipulation framework
  3. * Copyright (C) 2000 INRIA, France Telecom
  4. * Copyright (C) 2002 France Telecom
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * Contact: Eric.Bruneton@rd.francetelecom.com
  21. *
  22. * Author: Eric Bruneton
  23. */
  24. package org.gjt.sp.jedit.bsh.org.objectweb.asm;
  25. /**
  26. * A dynamically extensible vector of bytes. This class is roughly equivalent to
  27. * a DataOutputStream on top of a ByteArrayOutputStream, but is more efficient.
  28. */
  29. final class ByteVector {
  30. /**
  31. * The content of this vector.
  32. */
  33. byte[] data;
  34. /**
  35. * Actual number of bytes in this vector.
  36. */
  37. int length;
  38. /**
  39. * Constructs a new {@link ByteVector ByteVector} with a default initial size.
  40. */
  41. public ByteVector () {
  42. data = new byte[64];
  43. }
  44. /**
  45. * Constructs a new {@link ByteVector ByteVector} with the given initial size.
  46. *
  47. * @param initialSize the initial size of the byte vector to be constructed.
  48. */
  49. public ByteVector (final int initialSize) {
  50. data = new byte[initialSize];
  51. }
  52. /**
  53. * Puts a byte into this byte vector. The byte vector is automatically
  54. * enlarged if necessary.
  55. *
  56. * @param b a byte.
  57. * @return this byte vector.
  58. */
  59. public ByteVector put1 (final int b) {
  60. int length = this.length;
  61. if (length + 1 > data.length) {
  62. enlarge(1);
  63. }
  64. data[length++] = (byte)b;
  65. this.length = length;
  66. return this;
  67. }
  68. /**
  69. * Puts two bytes into this byte vector. The byte vector is automatically
  70. * enlarged if necessary.
  71. *
  72. * @param b1 a byte.
  73. * @param b2 another byte.
  74. * @return this byte vector.
  75. */
  76. public ByteVector put11 (final int b1, final int b2) {
  77. int length = this.length;
  78. if (length + 2 > data.length) {
  79. enlarge(2);
  80. }
  81. byte[] data = this.data;
  82. data[length++] = (byte)b1;
  83. data[length++] = (byte)b2;
  84. this.length = length;
  85. return this;
  86. }
  87. /**
  88. * Puts a short into this byte vector. The byte vector is automatically
  89. * enlarged if necessary.
  90. *
  91. * @param s a short.
  92. * @return this byte vector.
  93. */
  94. public ByteVector put2 (final int s) {
  95. int length = this.length;
  96. if (length + 2 > data.length) {
  97. enlarge(2);
  98. }
  99. byte[] data = this.data;
  100. data[length++] = (byte)(s >>> 8);
  101. data[length++] = (byte)s;
  102. this.length = length;
  103. return this;
  104. }
  105. /**
  106. * Puts a byte and a short into this byte vector. The byte vector is
  107. * automatically enlarged if necessary.
  108. *
  109. * @param b a byte.
  110. * @param s a short.
  111. * @return this byte vector.
  112. */
  113. public ByteVector put12 (final int b, final int s) {
  114. int length = this.length;
  115. if (length + 3 > data.length) {
  116. enlarge(3);
  117. }
  118. byte[] data = this.data;
  119. data[length++] = (byte)b;
  120. data[length++] = (byte)(s >>> 8);
  121. data[length++] = (byte)s;
  122. this.length = length;
  123. return this;
  124. }
  125. /**
  126. * Puts an int into this byte vector. The byte vector is automatically
  127. * enlarged if necessary.
  128. *
  129. * @param i an int.
  130. * @return this byte vector.
  131. */
  132. public ByteVector put4 (final int i) {
  133. int length = this.length;
  134. if (length + 4 > data.length) {
  135. enlarge(4);
  136. }
  137. byte[] data = this.data;
  138. data[length++] = (byte)(i >>> 24);
  139. data[length++] = (byte)(i >>> 16);
  140. data[length++] = (byte)(i >>> 8);
  141. data[length++] = (byte)i;
  142. this.length = length;
  143. return this;
  144. }
  145. /**
  146. * Puts a long into this byte vector. The byte vector is automatically
  147. * enlarged if necessary.
  148. *
  149. * @param l a long.
  150. * @return this byte vector.
  151. */
  152. public ByteVector put8 (final long l) {
  153. int length = this.length;
  154. if (length + 8 > data.length) {
  155. enlarge(8);
  156. }
  157. byte[] data = this.data;
  158. int i = (int)(l >>> 32);
  159. data[length++] = (byte)(i >>> 24);
  160. data[length++] = (byte)(i >>> 16);
  161. data[length++] = (byte)(i >>> 8);
  162. data[length++] = (byte)i;
  163. i = (int)l;
  164. data[length++] = (byte)(i >>> 24);
  165. data[length++] = (byte)(i >>> 16);
  166. data[length++] = (byte)(i >>> 8);
  167. data[length++] = (byte)i;
  168. this.length = length;
  169. return this;
  170. }
  171. /**
  172. * Puts a String in UTF format into this byte vector. The byte vector is
  173. * automatically enlarged if necessary.
  174. *
  175. * @param s a String.
  176. * @return this byte vector.
  177. */
  178. public ByteVector putUTF (final String s) {
  179. int charLength = s.length();
  180. int byteLength = 0;
  181. for (int i = 0; i < charLength; ++i) {
  182. char c = s.charAt(i);
  183. if (c >= '\001' && c <= '\177') {
  184. byteLength++;
  185. } else if (c > '\u07FF') {
  186. byteLength += 3;
  187. } else {
  188. byteLength += 2;
  189. }
  190. }
  191. if (byteLength > 65535) {
  192. throw new IllegalArgumentException();
  193. }
  194. int length = this.length;
  195. if (length + 2 + byteLength > data.length) {
  196. enlarge(2 + byteLength);
  197. }
  198. byte[] data = this.data;
  199. data[length++] = (byte)(byteLength >>> 8);
  200. data[length++] = (byte)(byteLength);
  201. for (int i = 0; i < charLength; ++i) {
  202. char c = s.charAt(i);
  203. if (c >= '\001' && c <= '\177') {
  204. data[length++] = (byte)c;
  205. } else if (c > '\u07FF') {
  206. data[length++] = (byte)(0xE0 | c >> 12 & 0xF);
  207. data[length++] = (byte)(0x80 | c >> 6 & 0x3F);
  208. data[length++] = (byte)(0x80 | c & 0x3F);
  209. } else {
  210. data[length++] = (byte)(0xC0 | c >> 6 & 0x1F);
  211. data[length++] = (byte)(0x80 | c & 0x3F);
  212. }
  213. }
  214. this.length = length;
  215. return this;
  216. }
  217. /**
  218. * Puts an array of bytes into this byte vector. The byte vector is
  219. * automatically enlarged if necessary.
  220. *
  221. * @param b an array of bytes. May be <tt>null</tt> to put <tt>len</tt> null
  222. * bytes into this byte vector.
  223. * @param off index of the fist byte of b that must be copied.
  224. * @param len number of bytes of b that must be copied.
  225. * @return this byte vector.
  226. */
  227. public ByteVector putByteArray (
  228. final byte[] b,
  229. final int off,
  230. final int len)
  231. {
  232. if (length + len > data.length) {
  233. enlarge(len);
  234. }
  235. if (b != null) {
  236. System.arraycopy(b, off, data, length, len);
  237. }
  238. length += len;
  239. return this;
  240. }
  241. /**
  242. * Enlarge this byte vector so that it can receive n more bytes.
  243. *
  244. * @param size number of additional bytes that this byte vector should be
  245. * able to receive.
  246. */
  247. private void enlarge (final int size) {
  248. byte[] newData = new byte[Math.max(2*data.length, length + size)];
  249. System.arraycopy(data, 0, newData, 0, length);
  250. data = newData;
  251. }
  252. }