1 2 3 package com.db4odoc.f1.ios; 4 5 import java.io.File ; 6 import java.io.IOException ; 7 import java.io.PrintStream ; 8 import java.io.RandomAccessFile ; 9 10 import com.db4o.DTrace; 11 import com.db4o.Platform4; 12 import com.db4o.io.IoAdapter; 13 14 public class LoggingAdapter extends IoAdapter { 15 16 private RandomAccessFile _delegate; 17 private PrintStream _out = System.out; 18 19 public LoggingAdapter(){ 20 } 21 22 protected LoggingAdapter(String path, boolean lockFile, long initialLength) throws IOException { 23 _delegate = new RandomAccessFile (path, "rw"); 24 if(initialLength>0) { 25 _delegate.seek(initialLength - 1); 26 _delegate.write(new byte[] {0}); 27 } 28 if(lockFile){ 29 Platform4.lockFile(_delegate); 30 } 31 } 32 33 public void setOut(PrintStream out){ 34 _out = out; 35 } 36 37 public void close() throws IOException { 38 _out.println("Closing file"); 39 try { 40 Platform4.unlockFile(_delegate); 41 } catch (Exception e) { 42 } 43 _delegate.close(); 44 } 45 46 47 public void delete(String path) { 48 _out.println("Deleting file " + path); 49 new File (path).delete(); 50 } 51 52 public boolean exists(String path){ 53 File existingFile = new File (path); 54 return existingFile.exists() && existingFile.length() > 0; 55 } 56 57 public long getLength() throws IOException { 58 _out.println("File length:" + _delegate.length()); 59 return _delegate.length(); 60 } 61 62 public IoAdapter open(String path, boolean lockFile, long initialLength) throws IOException { 63 _out.println("Opening file " + path); 64 return new LoggingAdapter(path, lockFile, initialLength); 65 } 66 67 68 public int read(byte[] bytes, int length) throws IOException { 69 _out.println("Reading " + length + " bytes"); 70 return _delegate.read(bytes, 0, length); 71 } 72 73 74 public void seek(long pos) throws IOException { 75 76 if(DTrace.enabled){ 77 DTrace.REGULAR_SEEK.log(pos); 78 } 79 _out.println("Setting pointer position to " + pos); 80 _delegate.seek(pos); 81 } 82 83 84 public void sync() throws IOException { 85 _out.println("Synchronizing"); 86 _delegate.getFD().sync(); 87 } 88 89 90 public void write(byte[] buffer, int length) throws IOException { 91 _out.println("Writing " + length + " bytes"); 92 _delegate.write(buffer, 0, length); 93 94 } 95 96 } 97 | Popular Tags |