1 package com.quadcap.sql.file; 2 3 40 41 import com.quadcap.util.Debug; 42 import com.quadcap.util.Util; 43 44 49 public class ByteArrayRandomAccess extends RandomAccess { 50 byte[] buf; 51 int size; 52 53 public ByteArrayRandomAccess() { 54 this.size = 0; 55 setCapacity(1024); 56 } 57 58 public ByteArrayRandomAccess(int initialCap) { 59 this.size = 0; 60 setCapacity(initialCap); 61 } 62 63 public ByteArrayRandomAccess(byte[] buf) { 64 this.buf = buf; 65 this.size = buf.length; 66 } 67 68 public void reset(byte[] buf, int size) { 69 this.buf = buf; 70 this.size = size; 71 } 72 73 public byte[] getBytes() { 74 return buf; 75 } 76 77 final void setCapacity(long cap) { 78 if (buf == null || cap > buf.length) { 79 byte[] nbuf = new byte[(int)cap]; 80 if (size > 0) { 81 System.arraycopy(buf, 0, nbuf, 0, size); 82 } 83 this.buf = nbuf; 84 } 85 } 86 87 public byte[] toByteArray() { 88 byte[] ret = new byte[size]; 89 read(0, ret, 0, size); 90 return ret; 91 } 92 93 96 public long size() { 97 return size; 98 } 99 100 103 public void resize(long newSize) { 104 if (newSize > buf.length) { 105 setCapacity(newSize + (newSize >> 1)); 106 } 107 this.size = (int)newSize; 108 } 109 110 114 public void write(long pos, byte[] b, int offset, int len) { 115 if (trace) { 117 Debug.println("BAR:write(" + pos + ", " + 118 Util.hexBytes(b,offset,len) + ")"); 119 } 120 System.arraycopy(b, offset, buf, (int)pos, len); 122 } 123 124 128 public void read(long pos, byte[] b, int offset, int len) { 129 if (trace) { 131 Debug.println("BAR:read(" + pos + ", " + 132 Util.hexBytes(b,offset,len) + ")"); 133 } 134 System.arraycopy(buf, (int)pos, b, offset, len); 136 } 137 138 static final boolean trace = false; 140 142 public final void writeByte(int pos, int val) { 143 buf[pos] = (byte)(val & 0xff); 144 } 145 146 public final int readByte(int pos) { 147 return buf[pos] & 0xff; 148 } 149 150 153 public void close() { 154 } 155 156 public void flush() { 157 } 158 } 159 | Popular Tags |