KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > ios > LoggingAdapter


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */
2
3 package com.db4odoc.f1.ios;
4
5 import java.io.File JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.PrintStream JavaDoc;
8 import java.io.RandomAccessFile JavaDoc;
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 JavaDoc _delegate;
17     private PrintStream JavaDoc _out = System.out;
18      
19      public LoggingAdapter(){
20      }
21      
22     protected LoggingAdapter(String JavaDoc path, boolean lockFile, long initialLength) throws IOException JavaDoc {
23         _delegate = new RandomAccessFile JavaDoc(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 JavaDoc out){
34         _out = out;
35     }
36     
37     public void close() throws IOException JavaDoc {
38         _out.println("Closing file");
39         try {
40             Platform4.unlockFile(_delegate);
41         } catch (Exception JavaDoc e) {
42         }
43         _delegate.close();
44     }
45
46
47     public void delete(String JavaDoc path) {
48         _out.println("Deleting file " + path);
49         new File JavaDoc(path).delete();
50     }
51
52     public boolean exists(String JavaDoc path){
53         File JavaDoc existingFile = new File JavaDoc(path);
54         return existingFile.exists() && existingFile.length() > 0;
55     }
56
57     public long getLength() throws IOException JavaDoc {
58         _out.println("File length:" + _delegate.length());
59         return _delegate.length();
60     }
61
62     public IoAdapter open(String JavaDoc path, boolean lockFile, long initialLength) throws IOException JavaDoc {
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 JavaDoc {
69         _out.println("Reading " + length + " bytes");
70         return _delegate.read(bytes, 0, length);
71     }
72
73
74     public void seek(long pos) throws IOException JavaDoc {
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 JavaDoc {
85         _out.println("Synchronizing");
86         _delegate.getFD().sync();
87     }
88
89     
90     public void write(byte[] buffer, int length) throws IOException JavaDoc {
91         _out.println("Writing " + length + " bytes");
92         _delegate.write(buffer, 0, length);
93         
94     }
95
96 }
97
Popular Tags