1 21 package org.apache.derby.client.am; 22 23 public class SignedBinary { 24 private SignedBinary() { 26 } 27 28 31 public final static int BIG_ENDIAN = 1; 32 33 36 public final static int LITTLE_ENDIAN = 2; 37 38 43 public static final short getShort(byte[] buffer, int offset) { 44 return (short) (((buffer[offset + 0] & 0xff) << 8) + 45 ((buffer[offset + 1] & 0xff) << 0)); 46 } 47 48 53 public static final int getInt(byte[] buffer, int offset) { 54 return (int) (((buffer[offset + 0] & 0xff) << 24) + 55 ((buffer[offset + 1] & 0xff) << 16) + 56 ((buffer[offset + 2] & 0xff) << 8) + 57 ((buffer[offset + 3] & 0xff) << 0)); 58 } 59 60 65 public static final long getLong(byte[] buffer, int offset) { 66 return (long) (((buffer[offset + 0] & 0xffL) << 56) + 67 ((buffer[offset + 1] & 0xffL) << 48) + 68 ((buffer[offset + 2] & 0xffL) << 40) + 69 ((buffer[offset + 3] & 0xffL) << 32) + 70 ((buffer[offset + 4] & 0xffL) << 24) + 71 ((buffer[offset + 5] & 0xffL) << 16) + 72 ((buffer[offset + 6] & 0xffL) << 8) + 73 ((buffer[offset + 7] & 0xffL) << 0)); 74 } 75 76 78 81 public static final void shortToBigEndianBytes(byte[] buffer, int offset, short v) { 82 buffer[offset++] = (byte) ((v >>> 8) & 0xFF); 83 buffer[offset++] = (byte) ((v >>> 0) & 0xFF); 84 } 85 86 89 public static final void intToBigEndianBytes(byte[] buffer, int offset, int v) { 90 buffer[offset++] = (byte) ((v >>> 24) & 0xFF); 91 buffer[offset++] = (byte) ((v >>> 16) & 0xFF); 92 buffer[offset++] = (byte) ((v >>> 8) & 0xFF); 93 buffer[offset++] = (byte) ((v >>> 0) & 0xFF); 94 } 95 96 99 public static final void longToBigEndianBytes(byte[] buffer, int offset, long v) { 100 buffer[offset++] = (byte) ((v >>> 56) & 0xFF); 101 buffer[offset++] = (byte) ((v >>> 48) & 0xFF); 102 buffer[offset++] = (byte) ((v >>> 40) & 0xFF); 103 buffer[offset++] = (byte) ((v >>> 32) & 0xFF); 104 buffer[offset++] = (byte) ((v >>> 24) & 0xFF); 105 buffer[offset++] = (byte) ((v >>> 16) & 0xFF); 106 buffer[offset++] = (byte) ((v >>> 8) & 0xFF); 107 buffer[offset++] = (byte) ((v >>> 0) & 0xFF); 108 } 109 } 110 | Popular Tags |