001: /*
002:
003: Derby - Class org.apache.derby.client.am.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.client.am;
022:
023: public class SignedBinary {
024: // Hide the default constructor, this is a static class.
025: private SignedBinary() {
026: }
027:
028: /**
029: * Unix byte-order for signed binary representations.
030: */
031: public final static int BIG_ENDIAN = 1;
032:
033: /**
034: * Intel 80/86 reversed byte-order for signed binary representations.
035: */
036: public final static int LITTLE_ENDIAN = 2;
037:
038: /**
039: * Build a Java short from a 2-byte signed binary representation.
040: *
041: * @throws IllegalArgumentException if the specified byte order is not recognized.
042: */
043: public static final short getShort(byte[] buffer, int offset) {
044: return (short) (((buffer[offset + 0] & 0xff) << 8) + ((buffer[offset + 1] & 0xff) << 0));
045: }
046:
047: /**
048: * Build a Java int from a 4-byte signed binary representation.
049: *
050: * @throws IllegalArgumentException if the specified byte order is not recognized.
051: */
052: public static final int getInt(byte[] buffer, int offset) {
053: return (int) (((buffer[offset + 0] & 0xff) << 24)
054: + ((buffer[offset + 1] & 0xff) << 16)
055: + ((buffer[offset + 2] & 0xff) << 8) + ((buffer[offset + 3] & 0xff) << 0));
056: }
057:
058: /**
059: * Build a Java long from an 8-byte signed binary representation.
060: *
061: * @throws IllegalArgumentException if the specified byte order is not recognized.
062: */
063: public static final long getLong(byte[] buffer, int offset) {
064: return (long) (((buffer[offset + 0] & 0xffL) << 56)
065: + ((buffer[offset + 1] & 0xffL) << 48)
066: + ((buffer[offset + 2] & 0xffL) << 40)
067: + ((buffer[offset + 3] & 0xffL) << 32)
068: + ((buffer[offset + 4] & 0xffL) << 24)
069: + ((buffer[offset + 5] & 0xffL) << 16)
070: + ((buffer[offset + 6] & 0xffL) << 8) + ((buffer[offset + 7] & 0xffL) << 0));
071: }
072:
073: //--------------------- input converters -------------------------------------
074:
075: /**
076: * Write a Java short to a 2-byte big endian signed binary representation.
077: */
078: public static final void shortToBigEndianBytes(byte[] buffer,
079: int offset, short v) {
080: buffer[offset++] = (byte) ((v >>> 8) & 0xFF);
081: buffer[offset++] = (byte) ((v >>> 0) & 0xFF);
082: }
083:
084: /**
085: * Write a Java int to a 4-byte big endian signed binary representation.
086: */
087: public static final void intToBigEndianBytes(byte[] buffer,
088: int offset, int v) {
089: buffer[offset++] = (byte) ((v >>> 24) & 0xFF);
090: buffer[offset++] = (byte) ((v >>> 16) & 0xFF);
091: buffer[offset++] = (byte) ((v >>> 8) & 0xFF);
092: buffer[offset++] = (byte) ((v >>> 0) & 0xFF);
093: }
094:
095: /**
096: * Write a Java long to an 8-byte big endian signed binary representation.
097: */
098: public static final void longToBigEndianBytes(byte[] buffer,
099: 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: }
|