1 5 package org.h2.store; 6 7 import java.sql.SQLException ; 8 9 12 public class DataPageBinary extends DataPage { 13 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 { 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 74 83 97 private static int getStringLenUTF8(String 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 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 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 (chars); 152 } 153 154 public void writeString(String s) { 155 writeStringUTF8(s); 157 } 161 162 public String readString() { 163 return readStringUTF8(); 165 } 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 s) { 178 return getStringLenUTF8(s); 180 } 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 |