KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > FileAccessor


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.editor.ext;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.RandomAccessFile JavaDoc;
26
27
28 /**
29  * DataAccessor for Code Completion DB files via RandomAccessFile implementation
30  *
31  * @author Martin Roskanin
32  */

33 public class FileAccessor implements DataAccessor{
34
35     private File JavaDoc f;
36     private RandomAccessFile JavaDoc file;
37
38
39     /** Creates a new instance of FileAccessor */
40     public FileAccessor(File JavaDoc file) {
41         f = file;
42     }
43
44     /** Appends exactly <code>len</code> bytes, starting at <code>off</code> of the buffer pointer
45      * to the end of file resource.
46      * @param buffer the buffer from which the data is appended.
47      * @param off the start offset of the data in the buffer.
48      * @param len the number of bytes to append.
49      * @return the actual file offset before appending.
50      */

51     public void append(byte[] buffer, int off, int len) throws IOException JavaDoc {
52         file.write(buffer, off, len);
53     }
54     
55     /** Reads up to len bytes of data from this file resource into an array of bytes.
56      * @param buffer the buffer into which the data is read.
57      * @param off the start offset of the data.
58      * @param len the maximum number of bytes read.
59      */

60     
61     public void read(byte[] buffer, int off, int len) throws IOException JavaDoc {
62         file.readFully(buffer, off, len);
63     }
64     
65     /** Opens DataAccessor file resource
66      * @param requestWrite if true, file is opened for read/write operation.
67      */

68     public void open(boolean requestWrite) throws IOException JavaDoc {
69         file = new RandomAccessFile JavaDoc(f, requestWrite ? "rw" : "r"); //NOI18N
70
if (!f.exists()){
71             f.createNewFile();
72         }
73     }
74     
75     /** Closes DataAccessor file resource */
76     public void close() throws IOException JavaDoc {
77         if (file!=null){
78             file.close();
79         }
80     }
81     
82     /**
83      * Returns the current offset in this file.
84      *
85      * @return the offset from the beginning of the file, in bytes,
86      * at which the next read or write occurs.
87      */

88     public long getFilePointer() throws IOException JavaDoc {
89         return file.getFilePointer();
90     }
91
92     /** Clears the file and sets the offset to 0 */
93     public void resetFile() throws IOException JavaDoc {
94         file.setLength(0);
95     }
96     
97     public void seek(long pos) throws IOException JavaDoc {
98         file.seek(pos);
99     }
100
101     public int getFileLength() {
102         return (int)f.length();
103     }
104     
105     public String JavaDoc toString() {
106         return f.getAbsolutePath();
107     }
108     
109 }
110
Popular Tags