1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.services.io.FormatIdUtil |
4 | |
5 | Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.services.io; |
22 | |
23 | import java.io.DataInput; |
24 | import java.io.DataOutput; |
25 | import java.io.IOException; |
26 | |
27 | /** |
28 | Utility class with static methods for constructing and reading the byte array |
29 | representation of format id's. |
30 | |
31 | <P>This utility supports a number of families of format ids. The byte array |
32 | form of each family is a different length. In all cases the first two bits |
33 | of the first byte indicate the family for an id. The list below describes |
34 | each family and gives its two bit identifier in parens. |
35 | |
36 | <UL> |
37 | <LI> (0) - The format id is a one byte number between 0 and 63 inclusive. |
38 | The byte[] encoding fits in one byte. |
39 | <LI> (1) - The format id is a two byte number between 16384 to 32767 |
40 | inclusive. The byte[] encoding stores the high order byte |
41 | first. |
42 | <LI> (2) - The format id is four byte number between 2147483648 and |
43 | 3221225471 inclusive. The byte[] encoding stores the high |
44 | order byte first. |
45 | <LI> (3) - Future expansion. |
46 | </UL> |
47 | */ |
48 | public final class FormatIdUtil |
49 | { |
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) throws IOException { |
58 | out.writeShort(formatId); |
59 | } |
60 | |
61 | public static int readFormatIdInteger(DataInput in) |
62 | throws IOException { |
63 | |
64 | return in.readUnsignedShort(); |
65 | } |
66 | |
67 | public static int readFormatIdInteger(byte[] data) { |
68 | |
69 | int a = data[0]; |
70 | int b = data[1]; |
71 | return (((a & 0xff) << 8) | (b & 0xff)); |
72 | } |
73 | |
74 | public static String formatIdToString(int fmtId) { |
75 | |
76 | return Integer.toString(fmtId); |
77 | } |
78 | } |