001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.store;
007:
008: import java.sql.SQLException;
009:
010: /**
011: * This class represents a byte buffer optimized for performance.
012: * It is the standard mechanism to convert data to a byte array and vice versa.
013: */
014: public class DataPageBinary extends DataPage {
015: // private final static boolean UTF8 = true;
016:
017: public DataPageBinary(DataHandler handler, byte[] data) {
018: super (handler, data);
019: }
020:
021: public void updateChecksum() {
022: if (CHECKSUM) {
023: int x = handler.getChecksum(data, 0, pos - 2);
024: data[pos - 2] = (byte) x;
025: }
026: }
027:
028: public void check(int len) throws SQLException {
029: if (CHECKSUM) {
030: int x = handler.getChecksum(data, 0, len - 2);
031: if (data[len - 2] == (byte) x) {
032: return;
033: }
034: handler.handleInvalidChecksum();
035: }
036: }
037:
038: public int getFillerLength() {
039: return 2;
040: }
041:
042: public void writeByte(byte x) {
043: data[pos++] = x;
044: }
045:
046: public int readByte() {
047: return data[pos++];
048: }
049:
050: public void writeInt(int x) {
051: byte[] buff = data;
052: buff[pos++] = (byte) (x >> 24);
053: buff[pos++] = (byte) (x >> 16);
054: buff[pos++] = (byte) (x >> 8);
055: buff[pos++] = (byte) x;
056: }
057:
058: public void setInt(int pos, int x) {
059: byte[] buff = data;
060: buff[pos] = (byte) (x >> 24);
061: buff[pos + 1] = (byte) (x >> 16);
062: buff[pos + 2] = (byte) (x >> 8);
063: buff[pos + 3] = (byte) x;
064: }
065:
066: public int readInt() {
067: byte[] buff = data;
068: return (buff[pos++] << 24) + ((buff[pos++] & 0xff) << 16)
069: + ((buff[pos++] & 0xff) << 8) + (buff[pos++] & 0xff);
070: }
071:
072: // private static int getStringLenChar(String s) {
073: // return 4 + s.length() * 2;
074: // }
075:
076: // private String readStringChar() {
077: // int len = ((data[pos++] & 0xff) << 24) +
078: // ((data[pos++] & 0xff) << 16) +
079: // ((data[pos++] & 0xff) << 8) + (data[pos++] & 0xff);
080: // char[] chars = new char[len];
081: // for(int i=0; i<len; i++) {
082: // chars[i] = (char)(((data[pos++] & 0xff) << 8) +
083: // (data[pos++] & 0xff));
084: // }
085: // return new String(chars);
086: // }
087:
088: // private void writeStringChar(String s) {
089: // checkCapacity(s.length()*2+4);
090: // int len = s.length();
091: // data[pos++] = (byte) ((len >> 24) & 0xff);
092: // data[pos++] = (byte) ((len >> 16) & 0xff);
093: // data[pos++] = (byte) ((len >> 8) & 0xff);
094: // data[pos++] = (byte) (len & 0xff);
095: // for(int i=0; i<s.length(); i++) {
096: // int c = s.charAt(i);
097: // data[pos++] = (byte)(c >> 8);
098: // data[pos++] = (byte)c;
099: // }
100: // }
101:
102: private static int getStringLenUTF8(String s) {
103: int plus = 4, len = s.length();
104: for (int i = 0; i < len; i++) {
105: char c = s.charAt(i);
106: if (c >= 0x800) {
107: plus += 2;
108: } else if (c == 0 || c >= 0x80) {
109: plus++;
110: }
111: }
112: return len + plus;
113: }
114:
115: private void writeStringUTF8(String s) {
116: int len = s.length();
117: checkCapacity(len * 3 + 4);
118: int p = pos;
119: byte[] buff = data;
120: buff[p++] = (byte) (len >> 24);
121: buff[p++] = (byte) (len >> 16);
122: buff[p++] = (byte) (len >> 8);
123: buff[p++] = (byte) len;
124: for (int i = 0; i < len; i++) {
125: int c = s.charAt(i);
126: if (c > 0 && c < 0x80) {
127: buff[p++] = (byte) c;
128: } else if (c >= 0x800) {
129: buff[p++] = (byte) (0xe0 | (c >> 12));
130: buff[p++] = (byte) (0x80 | ((c >> 6) & 0x3f));
131: buff[p++] = (byte) (0x80 | (c & 0x3f));
132: } else {
133: buff[p++] = (byte) (0xc0 | (c >> 6));
134: buff[p++] = (byte) (0x80 | (c & 0x3f));
135: }
136: }
137: pos = p;
138: }
139:
140: private String readStringUTF8() {
141: byte[] buff = data;
142: int p = pos;
143: int len = ((buff[p++] & 0xff) << 24)
144: + ((buff[p++] & 0xff) << 16)
145: + ((buff[p++] & 0xff) << 8) + (buff[p++] & 0xff);
146: char[] chars = new char[len];
147: for (int i = 0; i < len; i++) {
148: int x = buff[p++] & 0xff;
149: if (x < 0x80) {
150: chars[i] = (char) x;
151: } else if (x >= 0xe0) {
152: chars[i] = (char) (((x & 0xf) << 12)
153: + ((buff[p++] & 0x3f) << 6) + (buff[p++] & 0x3f));
154: } else {
155: chars[i] = (char) (((x & 0x1f) << 6) + (buff[p++] & 0x3f));
156: }
157: }
158: pos = p;
159: return new String(chars);
160: }
161:
162: public void writeString(String s) {
163: // if(UTF8) {
164: writeStringUTF8(s);
165: // } else {
166: // writeStringChar(s);
167: // }
168: }
169:
170: public String readString() {
171: // if(UTF8) {
172: return readStringUTF8();
173: // }
174: // return readStringChar();
175: }
176:
177: public int getIntLen() {
178: return 4;
179: }
180:
181: public int getLongLen(long x) {
182: return 8;
183: }
184:
185: public int getStringLen(String s) {
186: // if(UTF8) {
187: return getStringLenUTF8(s);
188: // }
189: // return getStringLenChar(s);
190: }
191:
192: public void fill(int len) {
193: if (pos > len) {
194: pos = len;
195: }
196: checkCapacity(len - pos);
197: pos = len;
198: }
199:
200: }
|