KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > java > FileObjectAccessor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.editor.java;
22
23 import java.io.EOFException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import org.netbeans.editor.ext.DataAccessor;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.FileLock;
32
33 /**
34  * DataAccessor for Code Completion DB files via FileObject streams
35  *
36  * @author Martin Roskanin
37  */

38 public class FileObjectAccessor implements DataAccessor {
39
40     FileObject fo;
41     InputStream JavaDoc inputStream;
42     FileOutputStream JavaDoc fos;
43     int actOff;
44
45     public FileObjectAccessor(FileObject fo) {
46         this.fo = fo;
47     }
48     
49     /** Appends exactly <code>len</code> bytes, starting at <code>off</code> of the buffer pointer
50      * to the end of file resource.
51      * @param buffer the buffer from which the data is appended.
52      * @param off the start offset of the data in the buffer.
53      * @param len the number of bytes to append.
54      */

55     public void append(byte[] buffer, int off, int len) throws IOException JavaDoc {
56         fos = new FileOutputStream JavaDoc(FileUtil.toFile(fo).getPath(), true);
57         fos.write(buffer, off, len);
58         fos.flush();
59         fos.close();
60         fos = null;
61     }
62     
63     /**
64      * Reads exactly <code>len</code> bytes from this file resource into the byte
65      * array, starting at the current file pointer. This method reads
66      * repeatedly from the file until the requested number of bytes are
67      * read. This method blocks until the requested number of bytes are
68      * read, the end of the inputStream is detected, or an exception is thrown.
69      *
70      * @param buffer the buffer into which the data is read.
71      * @param off the start offset of the data.
72      * @param len the number of bytes to read.
73      */

74     public void read(byte[] buffer, int off, int len) throws IOException JavaDoc {
75         int n = 0;
76         off = actOff + off;
77     do {
78         int count = this.readStream(buffer, off + n, len - n);
79         if (count < 0)
80         throw new EOFException JavaDoc();
81         n += count;
82     } while (n < len);
83     }
84     
85     /** Opens DataAccessor file resource
86      * @param requestWrite if true, file is opened for read/write operation.
87      */

88     public void open(boolean requestWrite) throws IOException JavaDoc {
89         if (!fo.existsExt(fo.getExt())){
90             resetFile();
91         }
92     }
93     
94     /** Closes DataAccessor file resource */
95     public void close() throws IOException JavaDoc {
96         if (inputStream!=null){
97             inputStream.close();
98         }
99         inputStream = null;
100     }
101     
102     /**
103      * Returns the current offset in this file.
104      *
105      * @return the offset from the beginning of the file, in bytes,
106      * at which the next read or write occurs.
107      */

108     public long getFilePointer() throws IOException JavaDoc {
109         return actOff;
110     }
111     
112     /** Clears the file and sets the offset to 0 */
113     public void resetFile() throws IOException JavaDoc {
114         FileObject folder = fo.getParent();
115         String JavaDoc name = fo.getName();
116         String JavaDoc ext = fo.getExt();
117         FileLock lock = fo.lock();
118         try {
119             fo.delete(lock);
120         } finally {
121             lock.releaseLock();
122         }
123         fo = folder.createData(name, ext);
124         actOff = 0;
125     }
126     
127     /**
128      * Sets the file-pointer offset, measured from the beginning of this
129      * file, at which the next read or write occurs.
130      */

131     public void seek(long pos) throws IOException JavaDoc {
132         actOff = (int)pos;
133     }
134
135     /** Reads up to len bytes of data from this file resource into an array of bytes.
136      * @param buffer the buffer into which the data is read.
137      * @param off the start offset of the data.
138      * @param len the maximum number of bytes read.
139      */

140     private int readStream(byte[] buffer, int off, int len) throws IOException JavaDoc {
141         int read = getStream(off).read(buffer,0,len);
142         actOff += read;
143         return read;
144     }
145     
146     /** Gets InputStream prepared for reading from <code>off</code> offset position*/
147     private InputStream JavaDoc getStream(int off) throws IOException JavaDoc {
148         if(inputStream == null) {
149             inputStream = fo.getInputStream();
150             inputStream.skip(off);
151         } else {
152             if(off >= actOff) {
153                 inputStream.skip(off-actOff);
154             } else {
155                 inputStream.close();
156                 inputStream = fo.getInputStream();
157                 inputStream.skip(off);
158             }
159         }
160         actOff = off;
161         return inputStream;
162     }
163     
164     public int getFileLength() {
165         return (int)fo.getSize();
166     }
167     
168     public String JavaDoc toString() {
169         return fo.toString();
170     }
171     
172 }
173
Popular Tags