KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.h2.engine.Constants;
10 import org.h2.message.Message;
11
12 public class DataPageText extends DataPage {
13
14     public DataPageText(DataHandler handler, byte[] data) {
15         super(handler, data);
16     }
17     
18     public void setInt(int pos, int x) {
19         for(int i=7;i>=0;i--, x>>>= 4) {
20             data[i] = (byte)Character.forDigit(x & 0xf, 16);
21         }
22     }
23
24     public void updateChecksum() {
25         if(CHECKSUM) {
26             int x = handler.getChecksum(data, 0, pos-2);
27             data[pos-2] = (byte)('a' + (((x^(x>>4)) & 0xf)));
28         }
29     }
30
31     public void check(int len) throws SQLException JavaDoc {
32         if(CHECKSUM) {
33             int x = handler.getChecksum(data, 0, len-2);
34             if(data[len-2] == (byte)('a' + (((x^(x>>4)) & 0xf)))) {
35                 return;
36             }
37             handler.handleInvalidChecksum();
38         }
39     }
40
41     public int getFillerLength() {
42         return 2;
43     }
44
45     public void writeInt(int x) {
46         if(Constants.CHECK) {
47             checkCapacity(8);
48         }
49         for(int i=7;i>=0;i--, x>>>= 4) {
50             data[pos+i] = (byte)Character.forDigit(x & 0xf, 16);
51         }
52         pos+=8;
53     }
54
55     public int readInt() {
56         int x = 0;
57         if(data[pos]==' ') {
58             pos += 8;
59             return 0;
60         }
61         for(int i=8, c;i>0;i--) {
62             x <<= 4;
63             x |= (c = data[pos++]) >= 'a' ? (c - 'a'+10) : (c - '0');
64         }
65         return x;
66     }
67
68     public int getIntLen() {
69         return 8;
70     }
71
72     public int getLongLen(long x) {
73         return 16;
74     }
75
76     public int getStringLen(String JavaDoc s) {
77         int len = 2 + s.length() + 1;
78         for (int i = 0; i < s.length(); i++) {
79             char c = s.charAt(i);
80             switch (c) {
81             case '\b':
82             case '\n':
83             case '\r':
84             case '\t':
85             case '\f':
86             case '"':
87             case '\\':
88                 len++;
89                 break;
90             default:
91                 int ch = (c & 0xffff);
92                 if ((ch >= ' ') && (ch <= 0xff)) {
93                     // 0
94
} else {
95                     len += 5;
96                 }
97             }
98         }
99         return len;
100     }
101
102     public String JavaDoc readString() {
103         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(32);
104         if(Constants.CHECK && data[pos] != '"') {
105             throw Message.getInternalError("\" expected");
106         }
107         pos++;
108         while(true) {
109             char x = (char)(data[pos++] & 0xff);
110             if(x == '"') {
111                 break;
112             } else if(x=='\\') {
113                 x = (char)data[pos++];
114                 switch(x) {
115                 case 't':
116                     buff.append('\t');
117                     break;
118                 case 'r':
119                     buff.append('\r');
120                     break;
121                 case 'n':
122                     buff.append('\n');
123                     break;
124                 case 'b':
125                     buff.append('\b');
126                     break;
127                 case 'f':
128                     buff.append('\f');
129                     break;
130                 case '"':
131                     buff.append('"');
132                     break;
133                 case '\\':
134                     buff.append('\\');
135                     break;
136                 case 'u': {
137                     x = 0;
138                     for(int i=3, c;i>=0;i--) {
139                         x <<= 4;
140                         x |= (c = data[pos++]) >= 'a' ? (c - 'a'+10) : (c - '0');
141                     }
142                     buff.append(x);
143                     break;
144                 }
145                 default:
146                     throw Message.getInternalError("unexpected "+x);
147                 }
148             } else {
149                 buff.append(x);
150             }
151         }
152         pos++;
153         return buff.toString();
154     }
155
156     public void writeString(String JavaDoc s) {
157         checkCapacity(s.length()*6+2);
158         data[pos++] = '\"';
159         for (int i = 0; i < s.length(); i++) {
160             char c = s.charAt(i);
161             switch (c) {
162             case '\t':
163                 data[pos++] = '\\'; data[pos++] = 't';
164                 break;
165             case '\r':
166                 data[pos++] = '\\'; data[pos++] = 'r';
167                 break;
168             case '\n':
169                 data[pos++] = '\\'; data[pos++] = 'n';
170                 break;
171             case '\b':
172                 data[pos++] = '\\'; data[pos++] = 'b';
173                 break;
174             case '\f':
175                 data[pos++] = '\\'; data[pos++] = 'f';
176                 break;
177             case '"':
178                 data[pos++] = '\\'; data[pos++] = '\"';
179                 break;
180             case '\\':
181                 data[pos++] = '\\'; data[pos++] = '\\';
182                 break;
183             default:
184                 int ch = (c & 0xffff);
185                 if ((ch >= ' ') && (ch <= 0xff)) {
186                     data[pos++] = (byte)ch;
187                 } else {
188                     data[pos++] = '\\';
189                     data[pos++] = 'u';
190                     for(int j=3;j>=0;j--, ch>>>= 4) {
191                         data[pos+j] = (byte)Character.forDigit(ch & 0xf, 16);
192                     }
193                     pos += 4;
194                 }
195             }
196         }
197         data[pos++] = '\"';
198         data[pos++] = ' ';
199     }
200
201     public void fill(int len) {
202         if (pos > len) {
203             pos = len;
204         }
205         checkCapacity(len-pos);
206         while(pos < len) {
207             data[pos++] = ' ';
208         }
209         data[pos-1] = '\n';
210     }
211 }
212
Popular Tags