001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.cache.SignedBinary
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021: package org.apache.derby.impl.drda;
022:
023: /**
024: * Converters from signed binary bytes to Java <code>short</code>, <code>int</code>, or <code>long</code>.
025: */
026: class SignedBinary {
027: // Hide the default constructor, this is a static class.
028: private SignedBinary() {
029: }
030:
031: /**
032: * AS/400, Unix, System/390 byte-order for signed binary representations.
033: */
034: public final static int BIG_ENDIAN = 1;
035:
036: /**
037: * Intel 80/86 reversed byte-order for signed binary representations.
038: */
039: public final static int LITTLE_ENDIAN = 2;
040:
041: /**
042: * Build a Java short from a 2-byte signed binary representation.
043: * <p>
044: * Depending on machine type, byte orders are
045: * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
046: * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
047: *
048: * @exception IllegalArgumentException if the specified byte order is not recognized.
049: */
050: public static short getShort(byte[] buffer, int offset,
051: int byteOrder) {
052: switch (byteOrder) {
053: case BIG_ENDIAN:
054: return bigEndianBytesToShort(buffer, offset);
055: case LITTLE_ENDIAN:
056: return littleEndianBytesToShort(buffer, offset);
057: default:
058: throw new java.lang.IllegalArgumentException();
059: }
060: }
061:
062: /**
063: * Build a Java int from a 4-byte signed binary representation.
064: * <p>
065: * Depending on machine type, byte orders are
066: * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
067: * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
068: *
069: * @exception IllegalArgumentException if the specified byte order is not recognized.
070: */
071: public static int getInt(byte[] buffer, int offset, int byteOrder) {
072: switch (byteOrder) {
073: case BIG_ENDIAN:
074: return bigEndianBytesToInt(buffer, offset);
075: case LITTLE_ENDIAN:
076: return littleEndianBytesToInt(buffer, offset);
077: default:
078: throw new java.lang.IllegalArgumentException();
079: }
080: }
081:
082: /**
083: * Build a Java long from an 8-byte signed binary representation.
084: * <p>
085: * Depending on machine type, byte orders are
086: * {@link #BIG_ENDIAN BIG_ENDIAN} for signed binary integers, and
087: * {@link #LITTLE_ENDIAN LITTLE_ENDIAN} for pc8087 signed binary integers.
088: * <p>
089: *
090: * @exception IllegalArgumentException if the specified byte order is not recognized.
091: */
092: public static long getLong(byte[] buffer, int offset, int byteOrder) {
093: switch (byteOrder) {
094: case BIG_ENDIAN:
095: return bigEndianBytesToLong(buffer, offset);
096: case LITTLE_ENDIAN:
097: return littleEndianBytesToLong(buffer, offset);
098: default:
099: throw new java.lang.IllegalArgumentException();
100: }
101: }
102:
103: /**
104: * Build a Java short from a 2-byte big endian signed binary representation.
105: */
106: public static short bigEndianBytesToShort(byte[] buffer, int offset) {
107: return (short) (((buffer[offset + 0] & 0xff) << 8) + ((buffer[offset + 1] & 0xff) << 0));
108: }
109:
110: /**
111: * Build a Java short from a 2-byte little endian signed binary representation.
112: */
113: public static short littleEndianBytesToShort(byte[] buffer,
114: int offset) {
115: return (short) (((buffer[offset + 0] & 0xff) << 0) + ((buffer[offset + 1] & 0xff) << 8));
116: }
117:
118: /**
119: * Build a Java int from a 4-byte big endian signed binary representation.
120: */
121: public static int bigEndianBytesToInt(byte[] buffer, int offset) {
122: return (int) (((buffer[offset + 0] & 0xff) << 24)
123: + ((buffer[offset + 1] & 0xff) << 16)
124: + ((buffer[offset + 2] & 0xff) << 8) + ((buffer[offset + 3] & 0xff) << 0));
125: }
126:
127: /**
128: * Build a Java int from a 4-byte little endian signed binary representation.
129: */
130: public static int littleEndianBytesToInt(byte[] buffer, int offset) {
131: return (int) (((buffer[offset + 0] & 0xff) << 0)
132: + ((buffer[offset + 1] & 0xff) << 8)
133: + ((buffer[offset + 2] & 0xff) << 16) + ((buffer[offset + 3] & 0xff) << 24));
134: }
135:
136: /**
137: * Build a Java long from an 8-byte big endian signed binary representation.
138: */
139: public static long bigEndianBytesToLong(byte[] buffer, int offset) {
140: return (long) (((buffer[offset + 0] & 0xffL) << 56)
141: + ((buffer[offset + 1] & 0xffL) << 48)
142: + ((buffer[offset + 2] & 0xffL) << 40)
143: + ((buffer[offset + 3] & 0xffL) << 32)
144: + ((buffer[offset + 4] & 0xffL) << 24)
145: + ((buffer[offset + 5] & 0xffL) << 16)
146: + ((buffer[offset + 6] & 0xffL) << 8) + ((buffer[offset + 7] & 0xffL) << 0));
147: }
148:
149: /**
150: * Build a Java long from an 8-byte little endian signed binary representation.
151: */
152: public static long littleEndianBytesToLong(byte[] buffer, int offset) {
153: return (long) (((buffer[offset + 0] & 0xffL) << 0)
154: + ((buffer[offset + 1] & 0xffL) << 8)
155: + ((buffer[offset + 2] & 0xffL) << 16)
156: + ((buffer[offset + 3] & 0xffL) << 24)
157: + ((buffer[offset + 4] & 0xffL) << 32)
158: + ((buffer[offset + 5] & 0xffL) << 40)
159: + ((buffer[offset + 6] & 0xffL) << 48) + ((buffer[offset + 7] & 0xffL) << 56));
160: }
161: }
|