1 package com.quadcap.sql.file; 2 3 40 41 import java.io.IOException ; 42 43 49 50 public abstract class RandomAccess { 51 byte[] fmtBuf = new byte[8]; 52 55 public abstract long size(); 56 57 60 public abstract void resize(long newSize) throws IOException ; 61 62 66 public abstract void write(long pos, byte[] buf, int offset, int len) 67 throws IOException ; 68 69 73 public abstract void read(long pos, byte[] buf, int offset, int len) 74 throws IOException ; 75 76 79 public abstract void close() throws IOException ; 80 81 public abstract void flush() throws IOException ; 82 83 public void writeByte(int pos, int val) throws IOException { 84 synchronized (fmtBuf) { 85 fmtBuf[0] = (byte)(val & 0xff); 86 write(pos, fmtBuf, 0, 1); 87 } 88 } 89 90 public int readByte(int pos) throws IOException { 91 synchronized (fmtBuf) { 92 read(pos, fmtBuf, 0, 1); 93 return fmtBuf[0] & 0xff; 94 } 95 } 96 97 100 public void writeInt(long pos, int val) throws IOException { 101 synchronized (fmtBuf) { 102 ByteUtil.putInt(fmtBuf, 0, val); 103 write(pos, fmtBuf, 0, 4); 104 } 105 } 106 107 110 public int readInt(long pos) throws IOException { 111 synchronized (fmtBuf) { 112 read(pos, fmtBuf, 0, 4); 113 return ByteUtil.getInt(fmtBuf, 0); 114 } 115 } 116 117 120 public void writeLong(long pos, long val) throws IOException { 121 synchronized (fmtBuf) { 122 ByteUtil.putLong(fmtBuf, 0, val); 123 write(pos, fmtBuf, 0, 8); 124 } 125 } 126 127 130 public long readLong(long pos) throws IOException { 131 synchronized (fmtBuf) { 132 read(pos, fmtBuf, 0, 8); 133 return ByteUtil.getLong(fmtBuf, 0); 134 } 135 } 136 } 137 | Popular Tags |