01: /*
02:
03: Derby - Class org.apache.derby.iapi.services.io.FormatIdUtil
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to you under the Apache License, Version 2.0
09: (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:
22: package org.apache.derby.iapi.services.io;
23:
24: import java.io.DataInput;
25: import java.io.DataOutput;
26: import java.io.IOException;
27:
28: /**
29: Utility class with static methods for constructing and reading the byte array
30: representation of format id's.
31:
32: <P>This utility supports a number of families of format ids. The byte array
33: form of each family is a different length. In all cases the first two bits
34: of the first byte indicate the family for an id. The list below describes
35: each family and gives its two bit identifier in parens.
36:
37: <UL>
38: <LI> (0) - The format id is a one byte number between 0 and 63 inclusive.
39: The byte[] encoding fits in one byte.
40: <LI> (1) - The format id is a two byte number between 16384 to 32767
41: inclusive. The byte[] encoding stores the high order byte
42: first.
43: <LI> (2) - The format id is four byte number between 2147483648 and
44: 3221225471 inclusive. The byte[] encoding stores the high
45: order byte first.
46: <LI> (3) - Future expansion.
47: </UL>
48: */
49: public final class FormatIdUtil {
50: private FormatIdUtil() {
51: }
52:
53: public static int getFormatIdByteLength(int formatId) {
54: return 2;
55: }
56:
57: public static void writeFormatIdInteger(DataOutput out, int formatId)
58: throws IOException {
59: out.writeShort(formatId);
60: }
61:
62: public static int readFormatIdInteger(DataInput in)
63: throws IOException {
64:
65: return in.readUnsignedShort();
66: }
67:
68: public static int readFormatIdInteger(byte[] data) {
69:
70: int a = data[0];
71: int b = data[1];
72: return (((a & 0xff) << 8) | (b & 0xff));
73: }
74:
75: public static String formatIdToString(int fmtId) {
76:
77: return Integer.toString(fmtId);
78: }
79: }
|