KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > store > DataPageBinary


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.store;
6
7 import java.sql.SQLException JavaDoc;
8
9 /**
10  * @author Thomas
11  */

12 public class DataPageBinary extends DataPage {
13 // private final static boolean UTF8 = true;
14

15     public DataPageBinary(DataHandler handler, byte[] data) {
16         super(handler, data);
17     }
18     
19     public void updateChecksum() {
20         if(CHECKSUM) {
21             int x = handler.getChecksum(data, 0, pos-2);
22             data[pos-2] = (byte)x;
23         }
24     }
25     
26     public void check(int len) throws SQLException JavaDoc {
27         if(CHECKSUM) {
28             int x = handler.getChecksum(data, 0, len-2);
29             if(data[len-2] == (byte)x) {
30                 return;
31             }
32             handler.handleInvalidChecksum();
33         }
34     }
35     
36     public int getFillerLength() {
37         return 2;
38     }
39
40     public void writeByte(byte x) {
41         data[pos++] = x;
42     }
43
44     public int readByte() {
45         return data[pos++];
46     }
47
48     public void writeInt(int x) {
49         byte[] buff = data;
50         buff[pos++] = (byte) (x >> 24);
51         buff[pos++] = (byte) (x >> 16);
52         buff[pos++] = (byte) (x >> 8);
53         buff[pos++] = (byte) x;
54     }
55
56     public void setInt(int pos, int x) {
57         byte[] buff = data;
58         buff[pos] = (byte) (x >> 24);
59         buff[pos+1] = (byte) (x >> 16);
60         buff[pos+2] = (byte) (x >> 8);
61         buff[pos+3] = (byte) x;
62     }
63     
64     public int readInt() {
65         byte[] buff = data;
66         return (buff[pos++]<< 24) + ((buff[pos++] & 0xff) << 16)
67         + ((buff[pos++] & 0xff) << 8) + (buff[pos++] & 0xff);
68     }
69
70 // private static int getStringLenChar(String s) {
71
// return 4 + s.length() * 2;
72
// }
73

74 // private String readStringChar() {
75
// int len = ((data[pos++] & 0xff) << 24) + ((data[pos++] & 0xff) << 16) + ((data[pos++] & 0xff) << 8) + (data[pos++] & 0xff);
76
// char[] chars = new char[len];
77
// for(int i=0; i<len; i++) {
78
// chars[i] = (char)(((data[pos++] & 0xff) << 8) + (data[pos++] & 0xff));
79
// }
80
// return new String(chars);
81
// }
82

83 // private void writeStringChar(String s) {
84
// checkCapacity(s.length()*2+4);
85
// int len = s.length();
86
// data[pos++] = (byte) ((len >> 24) & 0xff);
87
// data[pos++] = (byte) ((len >> 16) & 0xff);
88
// data[pos++] = (byte) ((len >> 8) & 0xff);
89
// data[pos++] = (byte) (len & 0xff);
90
// for(int i=0; i<s.length(); i++) {
91
// int c = s.charAt(i);
92
// data[pos++] = (byte)(c >> 8);
93
// data[pos++] = (byte)c;
94
// }
95
// }
96

97     private static int getStringLenUTF8(String JavaDoc s) {
98         int plus = 4, len = s.length();
99         for (int i = 0; i < len; i++) {
100             char c = s.charAt(i);
101             if (c >= 0x800) {
102                 plus += 2;
103             } else if (c == 0 || c >= 0x80) {
104                 plus++;
105             }
106         }
107         return len + plus;
108     }
109     
110     private void writeStringUTF8(String JavaDoc s) {
111         int len = s.length();
112         checkCapacity(len*3+4);
113         int p = pos;
114         byte[] buff = data;
115         buff[p++] = (byte) (len >> 24);
116         buff[p++] = (byte) (len >> 16);
117         buff[p++] = (byte) (len >> 8);
118         buff[p++] = (byte) len;
119         for(int i=0; i<len; i++) {
120             int c = s.charAt(i);
121             if(c>0 && c<0x80) {
122                 buff[p++] = (byte)c;
123             } else if(c>=0x800) {
124                 buff[p++] = (byte)(0xe0 | (c >> 12));
125                 buff[p++] = (byte)(0x80 | ((c >> 6) & 0x3f));
126                 buff[p++] = (byte)(0x80 | (c & 0x3f));
127             } else {
128                 buff[p++] = (byte)(0xc0 | (c >> 6));
129                 buff[p++] = (byte)(0x80 | (c & 0x3f));
130             }
131         }
132         pos = p;
133     }
134
135     private String JavaDoc readStringUTF8() {
136         byte[] buff = data;
137         int p = pos;
138         int len = ((buff[p++] & 0xff) << 24) + ((buff[p++] & 0xff) << 16) + ((buff[p++] & 0xff) << 8) + (buff[p++] & 0xff);
139         char[] chars = new char[len];
140         for(int i=0; i<len; i++) {
141             int x = buff[p++] & 0xff;
142             if(x < 0x80) {
143                 chars[i] = (char)x;
144             } else if(x >= 0xe0) {
145                 chars[i] = (char)(((x & 0xf) << 12) + ((buff[p++] & 0x3f) << 6) + (buff[p++] & 0x3f));
146             } else {
147                 chars[i] = (char)(((x & 0x1f) << 6) + (buff[p++] & 0x3f));
148             }
149         }
150         pos = p;
151         return new String JavaDoc(chars);
152     }
153
154     public void writeString(String JavaDoc s) {
155 // if(UTF8) {
156
writeStringUTF8(s);
157 // } else {
158
// writeStringChar(s);
159
// }
160
}
161
162     public String JavaDoc readString() {
163 // if(UTF8) {
164
return readStringUTF8();
165 // }
166
// return readStringChar();
167
}
168
169     public int getIntLen() {
170         return 4;
171     }
172
173     public int getLongLen(long x) {
174         return 8;
175     }
176
177     public int getStringLen(String JavaDoc s) {
178 // if(UTF8) {
179
return getStringLenUTF8(s);
180 // }
181
// return getStringLenChar(s);
182
}
183
184     public void fill(int len) {
185         if (pos > len) {
186             pos = len;
187         }
188         checkCapacity(len-pos);
189         pos = len;
190     }
191
192 }
193
Popular Tags