1 21 package com.db4o.io; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 27 31 public abstract class IoAdapter { 32 33 private static final int COPY_SIZE = 4096; 34 35 private int _blockSize; 36 37 40 protected final long regularAddress(int blockAddress, int blockAddressOffset){ 41 if (0 == _blockSize) { 42 throw new IllegalStateException (); 43 } 44 return (long)blockAddress * _blockSize + blockAddressOffset; 45 } 46 47 50 public void blockCopy(int oldAddress, int oldAddressOffset, int newAddress, int newAddressOffset, int length) throws IOException{ 51 copy(regularAddress(oldAddress, oldAddressOffset), regularAddress(newAddress, newAddressOffset), length); 52 } 53 54 57 public void blockSeek(int address) throws IOException { 58 blockSeek(address,0); 59 } 60 61 64 public void blockSeek(int address, int offset) 65 throws IOException { 66 seek(regularAddress(address,offset)); 67 } 68 69 72 public void blockSize(int blockSize) { 73 if (blockSize < 1) { 74 throw new IllegalArgumentException (); 75 } 76 _blockSize=blockSize; 77 } 78 79 82 public abstract void close() throws IOException; 83 84 87 public void copy(long oldAddress, long newAddress, int length) throws IOException{ 88 89 if(DTrace.enabled){ 90 DTrace.IO_COPY.logLength(newAddress, length); 91 } 92 93 if(length > COPY_SIZE){ 94 byte[] buffer = new byte[COPY_SIZE]; 95 int pos = 0; 96 while(pos + COPY_SIZE < length){ 97 copy(buffer, oldAddress + pos, newAddress + pos); 98 pos+= COPY_SIZE; 99 } 100 oldAddress += pos; 101 newAddress += pos; 102 length -= pos; 103 } 104 105 copy(new byte[length], oldAddress, newAddress); 106 } 107 108 109 private void copy(byte[] buffer, long oldAddress, long newAddress) throws IOException { 110 seek(oldAddress); 111 read(buffer); 112 seek(newAddress); 113 write(buffer); 114 } 115 116 119 public abstract void delete(String path); 120 121 124 public abstract boolean exists(String path); 125 126 129 public abstract long getLength() throws IOException; 130 131 134 public abstract IoAdapter open(String path, boolean lockFile, long initialLength) throws IOException; 135 136 141 public int read(byte[] buffer) throws IOException { 142 return read(buffer,buffer.length); 143 } 144 145 148 public abstract int read(byte[] bytes, int length) throws IOException; 149 150 153 public abstract void seek(long pos) throws IOException; 154 155 158 public abstract void sync() throws IOException; 159 160 163 public void write(byte[] bytes) throws IOException { 164 write(bytes,bytes.length); 165 } 166 167 170 public abstract void write(byte[] buffer, int length) throws IOException; 171 172 175 public int blockSize() { 176 return _blockSize; 177 } 178 179 180 } | Popular Tags |