001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.gsfret;
043:
044: import java.io.EOFException;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.io.InputStream;
048:
049: import org.netbeans.editor.ext.DataAccessor;
050: import org.openide.filesystems.FileObject;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.filesystems.FileLock;
053:
054: /**
055: * This file is originally from Retouche, the Java Support
056: * infrastructure in NetBeans. I have modified the file as little
057: * as possible to make merging Retouche fixes back as simple as
058: * possible.
059: *
060: * DataAccessor for Code Completion DB files via FileObject streams
061: *
062: * @author Martin Roskanin
063: */
064: public class FileObjectAccessor implements DataAccessor {
065:
066: FileObject fo;
067: InputStream inputStream;
068: FileOutputStream fos;
069: int actOff;
070:
071: public FileObjectAccessor(FileObject fo) {
072: this .fo = fo;
073: }
074:
075: /** Appends exactly <code>len</code> bytes, starting at <code>off</code> of the buffer pointer
076: * to the end of file resource.
077: * @param buffer the buffer from which the data is appended.
078: * @param off the start offset of the data in the buffer.
079: * @param len the number of bytes to append.
080: */
081: public void append(byte[] buffer, int off, int len)
082: throws IOException {
083: fos = new FileOutputStream(FileUtil.toFile(fo).getPath(), true);
084: fos.write(buffer, off, len);
085: fos.flush();
086: fos.close();
087: fos = null;
088: }
089:
090: /**
091: * Reads exactly <code>len</code> bytes from this file resource into the byte
092: * array, starting at the current file pointer. This method reads
093: * repeatedly from the file until the requested number of bytes are
094: * read. This method blocks until the requested number of bytes are
095: * read, the end of the inputStream is detected, or an exception is thrown.
096: *
097: * @param buffer the buffer into which the data is read.
098: * @param off the start offset of the data.
099: * @param len the number of bytes to read.
100: */
101: public void read(byte[] buffer, int off, int len)
102: throws IOException {
103: int n = 0;
104: off = actOff + off;
105: do {
106: int count = this .readStream(buffer, off + n, len - n);
107: if (count < 0)
108: throw new EOFException();
109: n += count;
110: } while (n < len);
111: }
112:
113: /** Opens DataAccessor file resource
114: * @param requestWrite if true, file is opened for read/write operation.
115: */
116: public void open(boolean requestWrite) throws IOException {
117: if (!fo.existsExt(fo.getExt())) {
118: resetFile();
119: }
120: }
121:
122: /** Closes DataAccessor file resource */
123: public void close() throws IOException {
124: if (inputStream != null) {
125: inputStream.close();
126: }
127: inputStream = null;
128: }
129:
130: /**
131: * Returns the current offset in this file.
132: *
133: * @return the offset from the beginning of the file, in bytes,
134: * at which the next read or write occurs.
135: */
136: public long getFilePointer() throws IOException {
137: return actOff;
138: }
139:
140: /** Clears the file and sets the offset to 0 */
141: public void resetFile() throws IOException {
142: FileObject folder = fo.getParent();
143: String name = fo.getName();
144: String ext = fo.getExt();
145: FileLock lock = fo.lock();
146: try {
147: fo.delete(lock);
148: } finally {
149: lock.releaseLock();
150: }
151: fo = folder.createData(name, ext);
152: actOff = 0;
153: }
154:
155: /**
156: * Sets the file-pointer offset, measured from the beginning of this
157: * file, at which the next read or write occurs.
158: */
159: public void seek(long pos) throws IOException {
160: actOff = (int) pos;
161: }
162:
163: /** Reads up to len bytes of data from this file resource into an array of bytes.
164: * @param buffer the buffer into which the data is read.
165: * @param off the start offset of the data.
166: * @param len the maximum number of bytes read.
167: */
168: private int readStream(byte[] buffer, int off, int len)
169: throws IOException {
170: int read = getStream(off).read(buffer, 0, len);
171: actOff += read;
172: return read;
173: }
174:
175: /** Gets InputStream prepared for reading from <code>off</code> offset position*/
176: private InputStream getStream(int off) throws IOException {
177: if (inputStream == null) {
178: inputStream = fo.getInputStream();
179: inputStream.skip(off);
180: } else {
181: if (off >= actOff) {
182: inputStream.skip(off - actOff);
183: } else {
184: inputStream.close();
185: inputStream = fo.getInputStream();
186: inputStream.skip(off);
187: }
188: }
189: actOff = off;
190: return inputStream;
191: }
192:
193: public int getFileLength() {
194: return (int) fo.getSize();
195: }
196:
197: public String toString() {
198: return fo.toString();
199: }
200:
201: }
|