KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > retouche > 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.retouche;
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  * This file is originally from Retouche, the Java Support
35  * infrastructure in NetBeans. I have modified the file as little
36  * as possible to make merging Retouche fixes back as simple as
37  * possible.
38  *
39  * DataAccessor for Code Completion DB files via FileObject streams
40  *
41  * @author Martin Roskanin
42  */

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

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

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

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

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

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

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