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.

ByteArray.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. /**
  17. * A collection of static methods for reading and writing a byte array.
  18. */
  19. public class ByteArray {
  20. /**
  21. * Reads an unsigned 16bit integer at the index.
  22. */
  23. public static int readU16bit(byte[] code, int index) {
  24. return ((code[index] & 0xff) << 8) | (code[index + 1] & 0xff);
  25. }
  26. /**
  27. * Reads a signed 16bit integer at the index.
  28. */
  29. public static int readS16bit(byte[] code, int index) {
  30. return (code[index] << 8) | (code[index + 1] & 0xff);
  31. }
  32. /**
  33. * Writes a 16bit integer at the index.
  34. */
  35. public static void write16bit(int value, byte[] code, int index) {
  36. code[index] = (byte)(value >>> 8);
  37. code[index + 1] = (byte)value;
  38. }
  39. /**
  40. * Reads a 32bit integer at the index.
  41. */
  42. public static int read32bit(byte[] code, int index) {
  43. return (code[index] << 24) | ((code[index + 1] & 0xff) << 16)
  44. | ((code[index + 2] & 0xff) << 8) | (code[index + 3] & 0xff);
  45. }
  46. /**
  47. * Writes a 32bit integer at the index.
  48. */
  49. public static void write32bit(int value, byte[] code, int index) {
  50. code[index] = (byte)(value >>> 24);
  51. code[index + 1] = (byte)(value >>> 16);
  52. code[index + 2] = (byte)(value >>> 8);
  53. code[index + 3] = (byte)value;
  54. }
  55. /**
  56. * Copies a 32bit integer.
  57. *
  58. * @param src the source byte array.
  59. * @param isrc the index into the source byte array.
  60. * @param dest the destination byte array.
  61. * @param idest the index into the destination byte array.
  62. */
  63. static void copy32bit(byte[] src, int isrc, byte[] dest, int idest) {
  64. dest[idest] = src[isrc];
  65. dest[idest + 1] = src[isrc + 1];
  66. dest[idest + 2] = src[isrc + 2];
  67. dest[idest + 3] = src[isrc + 3];
  68. }
  69. }