1 19 20 21 package org.netbeans.modules.editor.java; 22 23 import java.io.EOFException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 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 38 public class FileObjectAccessor implements DataAccessor { 39 40 FileObject fo; 41 InputStream inputStream; 42 FileOutputStream fos; 43 int actOff; 44 45 public FileObjectAccessor(FileObject fo) { 46 this.fo = fo; 47 } 48 49 55 public void append(byte[] buffer, int off, int len) throws IOException { 56 fos = new FileOutputStream (FileUtil.toFile(fo).getPath(), true); 57 fos.write(buffer, off, len); 58 fos.flush(); 59 fos.close(); 60 fos = null; 61 } 62 63 74 public void read(byte[] buffer, int off, int len) throws IOException { 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 (); 81 n += count; 82 } while (n < len); 83 } 84 85 88 public void open(boolean requestWrite) throws IOException { 89 if (!fo.existsExt(fo.getExt())){ 90 resetFile(); 91 } 92 } 93 94 95 public void close() throws IOException { 96 if (inputStream!=null){ 97 inputStream.close(); 98 } 99 inputStream = null; 100 } 101 102 108 public long getFilePointer() throws IOException { 109 return actOff; 110 } 111 112 113 public void resetFile() throws IOException { 114 FileObject folder = fo.getParent(); 115 String name = fo.getName(); 116 String 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 131 public void seek(long pos) throws IOException { 132 actOff = (int)pos; 133 } 134 135 140 private int readStream(byte[] buffer, int off, int len) throws IOException { 141 int read = getStream(off).read(buffer,0,len); 142 actOff += read; 143 return read; 144 } 145 146 147 private InputStream getStream(int off) throws IOException { 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 toString() { 169 return fo.toString(); 170 } 171 172 } 173 | Popular Tags |