1 21 package com.db4o.io; 22 23 import java.io.*; 24 25 42 public class SymbianIoAdapter extends RandomAccessFileAdapter { 43 private byte[] _seekBytes=new byte[500]; 44 private String _path; 45 private long _pos; 46 private long _length; 47 48 protected SymbianIoAdapter(String path, boolean lockFile, long initialLength) throws IOException { 49 super(path, lockFile, initialLength); 50 _path=path; 51 _pos=0; 52 setLength(); 53 } 54 55 private void setLength() throws IOException { 56 _length=retrieveLength(); 57 } 58 59 60 private long retrieveLength() throws IOException { 61 RandomAccessFile file=new RandomAccessFile(_path,"r"); 62 try { 63 return file.length(); 64 } finally { 65 file.close(); 66 } 67 } 68 69 70 public SymbianIoAdapter() { 71 super(); 72 } 73 74 public IoAdapter open(String path, boolean lockFile, long initialLength) throws IOException { 75 return new SymbianIoAdapter(path,lockFile,initialLength); 76 } 77 78 public long getLength() throws IOException { 79 setLength(); 80 return _length; 81 } 82 83 public int read(byte[] bytes, int length) throws IOException { 84 int ret=super.read(bytes, length); 85 _pos+=ret; 86 return ret; 87 } 88 89 public void write(byte[] buffer, int length) throws IOException { 90 super.write(buffer, length); 91 _pos+=length; 92 if(_pos>_length) { 93 setLength(); 94 } 95 } 96 97 public void seek(long pos) throws IOException { 98 if (pos > _length) { 99 setLength(); 100 } 101 if (pos > _length) { 102 int len = (int) (pos - _length); 103 super.seek(_length); 104 _pos=_length; 105 if (len < 500) { 106 write(_seekBytes, len); 107 } else { 108 write(new byte[len]); 109 } 110 } 111 super.seek(pos); 112 _pos=pos; 113 } 114 } | Popular Tags |