1 29 30 package com.caucho.db.store; 31 32 import com.caucho.vfs.TempBuffer; 33 34 import java.io.IOException ; 35 import java.io.Writer ; 36 37 class ClobWriter extends Writer { 38 private Store _store; 39 40 private StoreTransaction _xa; 41 42 private TempBuffer _tempBuffer; 43 private byte []_buffer; 44 private int _offset; 45 private int _bufferEnd; 46 47 private long _length; 48 49 private Inode _inode; 50 51 private byte []_inodeBuffer; 52 private int _inodeOffset; 53 54 59 ClobWriter(StoreTransaction xa, Store store, byte []inode, int inodeOffset) 60 { 61 init(xa, store, inode, inodeOffset); 62 } 63 64 69 ClobWriter(Inode inode) 70 { 71 init(null, inode.getStore(), inode.getBuffer(), 0); 72 73 _inode = inode; 74 } 75 76 79 public void init(StoreTransaction xa, Store store, 80 byte []inode, int inodeOffset) 81 { 82 if (xa == null) 83 xa = RawTransaction.create(); 84 85 _xa = xa; 86 87 _store = store; 88 89 _length = 0; 90 91 _inodeBuffer = inode; 92 _inodeOffset = inodeOffset; 93 94 Inode.clear(_inodeBuffer, _inodeOffset); 95 96 _offset = 0; 97 98 _tempBuffer = TempBuffer.allocate(); 99 _buffer = _tempBuffer.getBuffer(); 100 _bufferEnd = _buffer.length; 101 } 102 103 106 public void write(int v) 107 throws IOException 108 { 109 if (_bufferEnd <= _offset) { 110 flushBlock(); 111 } 112 113 _buffer[_offset++] = (byte) (v >> 8); 114 _buffer[_offset++] = (byte) (v); 115 116 _length += 2; 117 } 118 119 122 public void write(char []buffer, int offset, int length) 123 throws IOException 124 { 125 byte []byteBuffer = _buffer; 126 int byteOffset = _offset; 127 128 while (length > 0) { 129 if (_bufferEnd <= byteOffset) { 130 _offset = byteOffset; 131 flushBlock(); 132 byteOffset = _offset; 133 } 134 135 int sublen = (_bufferEnd - byteOffset) >> 1; 136 if (length < sublen) 137 sublen = length; 138 139 for (int i = 0; i < sublen; i++) { 140 char ch = buffer[offset + i]; 141 142 byteBuffer[byteOffset++] = (byte) (ch >> 8); 143 byteBuffer[byteOffset++] = (byte) (ch); 144 } 145 146 offset += sublen; 147 148 length -= sublen; 149 _length += 2 * sublen; 150 } 151 152 _offset = byteOffset; 153 } 154 155 158 private void flushBlock() 159 throws IOException 160 { 161 int length = _offset; 162 _offset = 0; 163 164 Inode.append(_inodeBuffer, _inodeOffset, _store, _xa, _buffer, 0, length); 165 } 166 167 170 public void flush() 171 { 172 } 173 174 177 public void close() 178 throws IOException 179 { 180 try { 181 flushBlock(); 182 183 TempBuffer.free(_tempBuffer); 184 } finally { 185 if (_inode != null) 186 _inode.closeOutputStream(); 187 } 188 } 189 190 193 private static void writeLong(byte []buffer, int offset, long v) 194 { 195 buffer[offset + 0] = (byte) (v >> 56); 196 buffer[offset + 1] = (byte) (v >> 48); 197 buffer[offset + 2] = (byte) (v >> 40); 198 buffer[offset + 3] = (byte) (v >> 32); 199 200 buffer[offset + 4] = (byte) (v >> 24); 201 buffer[offset + 5] = (byte) (v >> 16); 202 buffer[offset + 6] = (byte) (v >> 8); 203 buffer[offset + 7] = (byte) (v); 204 } 205 206 209 private static long readLong(byte []buffer, int offset) 210 { 211 return (((buffer[offset + 0] & 0xffL) << 56) | 212 ((buffer[offset + 1] & 0xffL) << 48) | 213 ((buffer[offset + 2] & 0xffL) << 40) | 214 ((buffer[offset + 3] & 0xffL) << 32) | 215 216 ((buffer[offset + 4] & 0xffL) << 24) | 217 ((buffer[offset + 4] & 0xffL) << 16) | 218 ((buffer[offset + 4] & 0xffL) << 8) | 219 ((buffer[offset + 4] & 0xffL))); 220 } 221 222 225 private static void writeShort(byte []buffer, int offset, short v) 226 { 227 buffer[offset + 0] = (byte) (v >> 8); 228 buffer[offset + 1] = (byte) (v); 229 } 230 231 234 private static int readShort(byte []buffer, int offset) 235 { 236 return (((buffer[offset + 0] & 0xff) << 8) | 237 ((buffer[offset + 1] & 0xff))); 238 } 239 } 240 | Popular Tags |