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 java.io.IOException;
00042
00043
import com.quadcap.util.Debug;
00044
00045
00046
00047
00048
00049
00050 public class BufferedRandomAccess extends RandomAccess {
00051 static final int BUFSIZE = 16384;
00052
00053 class Buffer {
00054 byte[]
buf =
new byte[BUFSIZE];
00055 long adr = -1;
00056 boolean dty =
false;
00057
00058
00059 public String
toString() {
00060
return "Buffer[" +
adr +
"]" + (
dty?
"*":
"");
00061 }
00062
00063 }
00064 Buffer b1 =
new Buffer();
00065 Buffer
b2 =
new Buffer();
00066 Buffer
b = null;
00067
00068 RandomAccess ra;
00069 long size;
00070
00071 public BufferedRandomAccess(
RandomAccess ra) {
00072
this.ra = ra;
00073
size = ra.
size();
00074 }
00075
00076
00077
00078
00079 public long size() {
00080
return size;
00081 }
00082
00083
00084
00085
00086 public void resize(
long newSize)
throws IOException {
00087 size = newSize;
00088
ra.
resize(newSize);
00089 }
00090
00091
00092
00093
00094
00095 public void write(
long pos, byte[] buf,
int offset,
int len)
00096
throws IOException
00097 {
00098
do {
00099
long pb = pos & ~(
BUFSIZE-1);
00100
long pe = pb +
BUFSIZE-1;
00101
if (pe >= size) pe = size-1;
00102
int xlen = (
int)(pe - pos + 1);
00103
if (xlen > len) xlen = len;
00104
if (xlen > 0) {
00105
getBuffer(pb);
00106 System.arraycopy(buf, offset,
b.
buf, (
int)(pos-pb), xlen);
00107
b.
dty =
true;
00108 offset += xlen;
00109 pos += xlen;
00110 len -= xlen;
00111 }
else {
00112 len = 0;
00113 }
00114 }
while (len > 0);
00115
00116 }
00117
00118
00119
00120
00121
00122 public void read(
long pos, byte[] buf,
int offset,
int len)
00123
throws IOException
00124 {
00125
do {
00126
long pb = pos & ~(
BUFSIZE-1);
00127
long pe = pb +
BUFSIZE-1;
00128
if (pe >= size) pe = size-1;
00129
int xlen = (
int)(pe - pos + 1);
00130
if (xlen > len) xlen = len;
00131
if (xlen > 0) {
00132
getBuffer(pb);
00133 System.arraycopy(
b.
buf, (
int)(pos-pb), buf, offset, xlen);
00134 offset += xlen;
00135 pos += xlen;
00136 len -= xlen;
00137 }
else {
00138 len = 0;
00139 }
00140 }
while (len > 0);
00141 }
00142
00143 final void getBuffer(
long addr)
throws IOException {
00144
if (addr ==
b1.
adr) {
00145
b =
b1;
00146 }
else if (addr ==
b2.
adr) {
00147
b =
b2;
00148 }
else {
00149
b = (
b ==
b1) ?
b2 :
b1;
00150
if (b.dty)
flush(b);
00151 b.adr = addr;
00152
int xlen = (
int)(
ra.
size() - addr);
00153
if (xlen >
BUFSIZE) xlen =
BUFSIZE;
00154
ra.
read(addr, b.buf, 0, xlen);
00155 }
00156 }
00157
00158
00159
00160
00161 public void close() throws IOException {
00162
flush();
00163
ra.
close();
00164 }
00165
00166 final void flushBuffers() throws IOException {
00167
flush(
b1);
00168
flush(
b2);
00169 }
00170
00171 public void flush() throws IOException {
00172
flushBuffers();
00173
ra.
flush();
00174 }
00175
00176 final void flush(Buffer b)
throws IOException {
00177
if (
b.
dty) {
00178
int xlen = (
int)(
ra.
size() -
b.
adr);
00179
if (xlen >
BUFSIZE) xlen =
BUFSIZE;
00180
ra.
write(
b.
adr,
b.
buf, 0, xlen);
00181
b.
dty =
false;
00182 }
00183 }
00184 }