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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /**
  18. * A collection of static methods for reading and writing a byte array.
  19. */
  20. public class ByteArray {
  21. /**
  22. * Reads an unsigned 16bit integer at the index.
  23. */
  24. public static int readU16bit(byte[] code, int index) {
  25. return ((code[index] & 0xff) << 8) | (code[index + 1] & 0xff);
  26. }
  27. /**
  28. * Reads a signed 16bit integer at the index.
  29. */
  30. public static int readS16bit(byte[] code, int index) {
  31. return (code[index] << 8) | (code[index + 1] & 0xff);
  32. }
  33. /**
  34. * Writes a 16bit integer at the index.
  35. */
  36. public static void write16bit(int value, byte[] code, int index) {
  37. code[index] = (byte)(value >>> 8);
  38. code[index + 1] = (byte)value;
  39. }
  40. /**
  41. * Reads a 32bit integer at the index.
  42. */
  43. public static int read32bit(byte[] code, int index) {
  44. return (code[index] << 24) | ((code[index + 1] & 0xff) << 16)
  45. | ((code[index + 2] & 0xff) << 8) | (code[index + 3] & 0xff);
  46. }
  47. /**
  48. * Writes a 32bit integer at the index.
  49. */
  50. public static void write32bit(int value, byte[] code, int index) {
  51. code[index] = (byte)(value >>> 24);
  52. code[index + 1] = (byte)(value >>> 16);
  53. code[index + 2] = (byte)(value >>> 8);
  54. code[index + 3] = (byte)value;
  55. }
  56. /**
  57. * Copies a 32bit integer.
  58. *
  59. * @param src the source byte array.
  60. * @param isrc the index into the source byte array.
  61. * @param dest the destination byte array.
  62. * @param idest the index into the destination byte array.
  63. */
  64. static void copy32bit(byte[] src, int isrc, byte[] dest, int idest) {
  65. dest[idest] = src[isrc];
  66. dest[idest + 1] = src[isrc + 1];
  67. dest[idest + 2] = src[isrc + 2];
  68. dest[idest + 3] = src[isrc + 3];
  69. }
  70. }