00001
package com.quadcap.sql.file;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import com.quadcap.util.Debug;
00042
import com.quadcap.util.Util;
00043
00044
00045
00046
00047
00048
00049 public class ByteArrayRandomAccess extends RandomAccess {
00050 byte[]
buf;
00051 int size;
00052
00053 public ByteArrayRandomAccess() {
00054
this.size = 0;
00055
setCapacity(1024);
00056 }
00057
00058 public ByteArrayRandomAccess(
int initialCap) {
00059
this.size = 0;
00060
setCapacity(initialCap);
00061 }
00062
00063 public ByteArrayRandomAccess(byte[] buf) {
00064
this.buf = buf;
00065
this.size = buf.length;
00066 }
00067
00068 public void reset(byte[] buf,
int size) {
00069
this.buf = buf;
00070
this.size = size;
00071 }
00072
00073 public byte[]
getBytes() {
00074
return buf;
00075 }
00076
00077 final void setCapacity(
long cap) {
00078
if (
buf == null || cap >
buf.length) {
00079 byte[] nbuf =
new byte[(
int)cap];
00080
if (
size > 0) {
00081 System.arraycopy(
buf, 0, nbuf, 0,
size);
00082 }
00083
this.buf = nbuf;
00084 }
00085 }
00086
00087 public byte[]
toByteArray() {
00088 byte[] ret =
new byte[
size];
00089
read(0, ret, 0,
size);
00090
return ret;
00091 }
00092
00093
00094
00095
00096 public long size() {
00097
return size;
00098 }
00099
00100
00101
00102
00103 public void resize(
long newSize) {
00104
if (newSize >
buf.length) {
00105 setCapacity(newSize + (newSize >> 1));
00106 }
00107
this.size = (
int)newSize;
00108 }
00109
00110
00111
00112
00113
00114 public void write(
long pos, byte[] b,
int offset,
int len) {
00115
00116
if (
trace) {
00117
Debug.println(
"BAR:write(" + pos +
", " +
00118
Util.hexBytes(b,offset,len) +
")");
00119 }
00120
00121 System.arraycopy(b, offset,
buf, (
int)pos, len);
00122 }
00123
00124
00125
00126
00127
00128 public void read(
long pos, byte[] b,
int offset,
int len) {
00129
00130
if (
trace) {
00131
Debug.println(
"BAR:read(" + pos +
", " +
00132
Util.hexBytes(b,offset,len) +
")");
00133 }
00134
00135 System.arraycopy(
buf, (
int)pos, b, offset, len);
00136 }
00137
00138
00139 static final boolean trace =
false;
00140
00141
00142 public final void writeByte(
int pos,
int val) {
00143
buf[pos] = (byte)(val & 0xff);
00144 }
00145
00146 public final int readByte(
int pos) {
00147
return buf[pos] & 0xff;
00148 }
00149
00150
00151
00152
00153 public void close() {
00154 }
00155
00156 public void flush() {
00157 }
00158 }