| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
* |
| 6 |
* The variable size number format code is a port from SQLite, |
| 7 |
* but stored in reverse order (least significant bits in the first byte). |
| 8 |
*/ |
| 9 |
package org.h2.store; |
| 10 |
|
| 11 |
import java.io.IOException; |
| 12 |
import java.io.OutputStream; |
| 13 |
import java.io.Reader; |
| 14 |
|
| 15 |
import org.h2.engine.Constants; |
| 16 |
import org.h2.util.Bits; |
| 17 |
import org.h2.util.MathUtils; |
| 18 |
import org.h2.util.Utils; |
| 19 |
|
| 20 |
/** |
| 21 |
* This class represents a byte buffer that contains persistent data of a page. |
| 22 |
* |
| 23 |
* @author Thomas Mueller |
| 24 |
* @author Noel Grandin |
| 25 |
* @author Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888 |
| 26 |
*/ |
| 27 |
public class Data { |
| 28 |
|
| 29 |
/** |
| 30 |
* The data itself. |
| 31 |
*/ |
| 32 |
private byte[] data; |
| 33 |
|
| 34 |
/** |
| 35 |
* The current write or read position. |
| 36 |
*/ |
| 37 |
private int pos; |
| 38 |
|
| 39 |
private Data(byte[] data) { |
| 40 |
this.data = data; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Write an integer at the current position. |
| 45 |
* The current position is incremented. |
| 46 |
* |
| 47 |
* @param x the value |
| 48 |
*/ |
| 49 |
public void writeInt(int x) { |
| 50 |
Bits.writeInt(data, pos, x); |
| 51 |
pos += 4; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Read an integer at the current position. |
| 56 |
* The current position is incremented. |
| 57 |
* |
| 58 |
* @return the value |
| 59 |
*/ |
| 60 |
public int readInt() { |
| 61 |
int x = Bits.readInt(data, pos); |
| 62 |
pos += 4; |
| 63 |
return x; |
| 64 |
} |
| 65 |
|
| 66 |
private void writeStringWithoutLength(char[] chars, int len) { |
| 67 |
int p = pos; |
| 68 |
byte[] buff = data; |
| 69 |
for (int i = 0; i < len; i++) { |
| 70 |
int c = chars[i]; |
| 71 |
if (c < 0x80) { |
| 72 |
buff[p++] = (byte) c; |
| 73 |
} else if (c >= 0x800) { |
| 74 |
buff[p++] = (byte) (0xe0 | (c >> 12)); |
| 75 |
buff[p++] = (byte) ((c >> 6) & 0x3f); |
| 76 |
buff[p++] = (byte) (c & 0x3f); |
| 77 |
} else { |
| 78 |
buff[p++] = (byte) (0xc0 | (c >> 6)); |
| 79 |
buff[p++] = (byte) (c & 0x3f); |
| 80 |
} |
| 81 |
} |
| 82 |
pos = p; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Create a new buffer. |
| 87 |
* |
| 88 |
* @param capacity the initial capacity of the buffer |
| 89 |
* @return the buffer |
| 90 |
*/ |
| 91 |
public static Data create(int capacity) { |
| 92 |
return new Data(new byte[capacity]); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the current write position of this buffer, which is the current |
| 97 |
* length. |
| 98 |
* |
| 99 |
* @return the length |
| 100 |
*/ |
| 101 |
public int length() { |
| 102 |
return pos; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get the byte array used for this page. |
| 107 |
* |
| 108 |
* @return the byte array |
| 109 |
*/ |
| 110 |
public byte[] getBytes() { |
| 111 |
return data; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Set the position to 0. |
| 116 |
*/ |
| 117 |
public void reset() { |
| 118 |
pos = 0; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Append a number of bytes to this buffer. |
| 123 |
* |
| 124 |
* @param buff the data |
| 125 |
* @param off the offset in the data |
| 126 |
* @param len the length in bytes |
| 127 |
*/ |
| 128 |
public void write(byte[] buff, int off, int len) { |
| 129 |
System.arraycopy(buff, off, data, pos, len); |
| 130 |
pos += len; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Copy a number of bytes to the given buffer from the current position. The |
| 135 |
* current position is incremented accordingly. |
| 136 |
* |
| 137 |
* @param buff the output buffer |
| 138 |
* @param off the offset in the output buffer |
| 139 |
* @param len the number of bytes to copy |
| 140 |
*/ |
| 141 |
public void read(byte[] buff, int off, int len) { |
| 142 |
System.arraycopy(data, pos, buff, off, len); |
| 143 |
pos += len; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Set the current read / write position. |
| 148 |
* |
| 149 |
* @param pos the new position |
| 150 |
*/ |
| 151 |
public void setPos(int pos) { |
| 152 |
this.pos = pos; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Read one single byte. |
| 157 |
* |
| 158 |
* @return the value |
| 159 |
*/ |
| 160 |
public byte readByte() { |
| 161 |
return data[pos++]; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Check if there is still enough capacity in the buffer. |
| 166 |
* This method extends the buffer if required. |
| 167 |
* |
| 168 |
* @param plus the number of additional bytes required |
| 169 |
*/ |
| 170 |
public void checkCapacity(int plus) { |
| 171 |
if (pos + plus >= data.length) { |
| 172 |
// a separate method to simplify inlining |
| 173 |
expand(plus); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private void expand(int plus) { |
| 178 |
// must copy everything, because pos could be 0 and data may be |
| 179 |
// still required |
| 180 |
data = Utils.copyBytes(data, (data.length + plus) * 2); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Fill up the buffer with empty space and an (initially empty) checksum |
| 185 |
* until the size is a multiple of Constants.FILE_BLOCK_SIZE. |
| 186 |
*/ |
| 187 |
public void fillAligned() { |
| 188 |
// 0..6 > 8, 7..14 > 16, 15..22 > 24, ... |
| 189 |
int len = MathUtils.roundUpInt(pos + 2, Constants.FILE_BLOCK_SIZE); |
| 190 |
pos = len; |
| 191 |
if (data.length < len) { |
| 192 |
checkCapacity(len - data.length); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Copy a String from a reader to an output stream. |
| 198 |
* |
| 199 |
* @param source the reader |
| 200 |
* @param target the output stream |
| 201 |
* @throws IOException on failure |
| 202 |
*/ |
| 203 |
public static void copyString(Reader source, OutputStream target) |
| 204 |
throws IOException { |
| 205 |
char[] buff = new char[Constants.IO_BUFFER_SIZE]; |
| 206 |
Data d = new Data(new byte[3 * Constants.IO_BUFFER_SIZE]); |
| 207 |
while (true) { |
| 208 |
int l = source.read(buff); |
| 209 |
if (l < 0) { |
| 210 |
break; |
| 211 |
} |
| 212 |
d.writeStringWithoutLength(buff, l); |
| 213 |
target.write(d.data, 0, d.pos); |
| 214 |
d.reset(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
} |