1 package com.quadcap.sql.file; 2 3 40 41 import java.io.IOException ; 42 import java.io.RandomAccessFile ; 43 44 49 public class FileRandomAccess extends RandomAccess { 50 long pos; 51 long maxSize; 52 RandomAccessFile ra; 53 54 public FileRandomAccess(RandomAccessFile ra, long maxSize) { 55 this.maxSize = maxSize; 56 this.ra = ra; 57 } 58 59 62 public long size() { 63 try { 64 return ra.length(); 65 } catch (IOException ex) { 66 return -1; 67 } 68 } 69 70 73 public void resize(long newSize) throws IOException { 74 ra.setLength(newSize); 75 } 76 77 81 public void write(long p, byte[] b, int offset, int len) 82 throws IOException 83 { 84 checkCapacity(p+len); 85 if (p != pos) { 86 ra.seek(p); 87 pos = p; 88 } 89 ra.write(b, offset, len); 90 pos += len; 91 } 92 93 97 public void read(long p, byte[] b, int offset, int len) 98 throws IOException 99 { 100 if (p != pos) { 101 ra.seek(p); 102 pos = p; 103 } 104 ra.read(b, offset, len); 105 pos += len; 106 } 107 108 111 public void close() throws IOException { 112 ra.close(); 113 } 114 115 public void flush() throws IOException { 116 try { 117 ra.getFD().sync(); 118 } catch (Throwable t) { 119 } 120 } 121 122 final void checkCapacity(long t) throws IOException { 123 if (t > ra.length()) { 124 if (t > maxSize) { 125 throw new IOException ("full"); 126 } 127 try { 128 ra.setLength(t); 129 } catch (IOException e) { 130 if (e.toString().indexOf("extended attributes") > 0) { 131 return; 132 } 133 throw e; 134 } 135 } 136 } 137 } 138 | Popular Tags |