KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > SignedBinary


1 /*
2
3    Derby - Class org.apache.derby.client.am.SignedBinary
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21 package org.apache.derby.client.am;
22
23 public class SignedBinary {
24     // Hide the default constructor, this is a static class.
25
private SignedBinary() {
26     }
27
28     /**
29      * Unix byte-order for signed binary representations.
30      */

31     public final static int BIG_ENDIAN = 1;
32
33     /**
34      * Intel 80/86 reversed byte-order for signed binary representations.
35      */

36     public final static int LITTLE_ENDIAN = 2;
37
38     /**
39      * Build a Java short from a 2-byte signed binary representation.
40      *
41      * @throws IllegalArgumentException if the specified byte order is not recognized.
42      */

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     /**
49      * Build a Java int from a 4-byte signed binary representation.
50      *
51      * @throws IllegalArgumentException if the specified byte order is not recognized.
52      */

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     /**
61      * Build a Java long from an 8-byte signed binary representation.
62      *
63      * @throws IllegalArgumentException if the specified byte order is not recognized.
64      */

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     //--------------------- input converters -------------------------------------
77

78     /**
79      * Write a Java short to a 2-byte big endian signed binary representation.
80      */

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     /**
87      * Write a Java int to a 4-byte big endian signed binary representation.
88      */

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     /**
97      * Write a Java long to an 8-byte big endian signed binary representation.
98      */

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