diff -r daf94f0b9e06 utilities/src/org/netbeans/modules/search/BasicSearchCriteria.java --- a/utilities/src/org/netbeans/modules/search/BasicSearchCriteria.java Tue Feb 01 18:55:43 2011 +0100 +++ b/utilities/src/org/netbeans/modules/search/BasicSearchCriteria.java Wed Feb 02 14:44:02 2011 +0300 @@ -44,9 +44,9 @@ package org.netbeans.modules.search; +import java.io.InputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.FileInputStream; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; @@ -62,7 +62,6 @@ import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import org.netbeans.api.queries.FileEncodingQuery; -import org.netbeans.modules.search.TextDetail.DetailNode; import org.openide.filesystems.FileObject; import org.openide.loaders.DataObject; import org.openide.loaders.DataObjectNotFoundException; @@ -748,13 +747,13 @@ lastCharset = FileEncodingQuery.getEncoding(fo); SearchPattern sp = createSearchPattern(); BufferedCharSequence bcs = null; - try { - FileInputStream fis = (FileInputStream)fo.getInputStream(); - bcs = new BufferedCharSequence(fis, lastCharset); + try { + InputStream stream = fo.getInputStream(); + bcs = new BufferedCharSequence(stream, lastCharset, fo.getSize()); ArrayList txtDetails = getTextDetails(bcs, fo, sp); - if (txtDetails.isEmpty()){ + if (txtDetails.isEmpty()){ return false; - } + } getDetailsMap().put(fo, txtDetails); freeDataObject(); return true; diff -r daf94f0b9e06 utilities/src/org/netbeans/modules/search/BufferedCharSequence.java --- a/utilities/src/org/netbeans/modules/search/BufferedCharSequence.java Tue Feb 01 18:55:43 2011 +0100 +++ b/utilities/src/org/netbeans/modules/search/BufferedCharSequence.java Wed Feb 02 14:44:02 2011 +0300 @@ -42,16 +42,15 @@ package org.netbeans.modules.search; -import java.io.FileInputStream; +import java.io.BufferedInputStream; import java.io.IOException; +import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.CharBuffer; -import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CoderResult; import java.nio.charset.CodingErrorAction; -import java.util.logging.Level; import java.util.logging.Logger; import sun.nio.cs.ThreadLocalCoders; @@ -138,43 +137,27 @@ /** * Creates {@code BufferedCharSequence} for the specified {@code stream}. - * @param stream is a stream that will be buffered and represented as a + * @param stream is a stream that will be buffered and represented as a * {@code CharSequence}. * @param charset is a named mapping that will be used to decode a sequence * of bytes from the {@code stream}. + * @param size is the size of the file. */ - public BufferedCharSequence(final FileInputStream stream, Charset charset) { - this(stream.getChannel(), charset); - } - - /** - * Creates {@code BufferedCharSequence} for the specified {@code channel}. - * @param channel is a channel that will be buffered and represented as a - * {@code CharSequence}. - * @param charset is a named mapping that will be used to decode a sequence - * of bytes from the {@code channel}. - */ - public BufferedCharSequence(final FileChannel channel, Charset charset) { + public BufferedCharSequence(final InputStream stream, Charset charset, long size) { // TODO charset.name() is used instead of charset due to a bug in the // org.netbeans.api.queries.FileEncodingQuery.ProxyCharset.ProxyDecoder // The IllegalStateException may be thrown after correct actions. // See #169804 - this(channel, + this(stream, ThreadLocalCoders.decoderFor(charset.name()) .onMalformedInput(CodingErrorAction.REPLACE) - .onUnmappableCharacter(CodingErrorAction.REPLACE)); + .onUnmappableCharacter(CodingErrorAction.REPLACE), size); } - /** - * Creates {@code BufferedCharSequence} for the specified {@code channel}. - * @param channel is a channel that will be buffered and represented as a - * {@code CharSequence}. - * @param decoder is a decoder that will be used to decode a sequence - * of bytes from the {@code channel}. - */ - public BufferedCharSequence(final FileChannel channel, - CharsetDecoder decoder) { - this.source = new Source(channel); + + public BufferedCharSequence(final InputStream stream, + CharsetDecoder decoder, long size) { + this.source = new Source(stream, size); this.decoder = decoder; this.sink = new Sink(this.source); LOG.finer(" " + this.source + "; decoder = " + this.decoder + @@ -232,7 +215,7 @@ * @return the underlying instance of this class. * @throws SourceIOException */ - public BufferedCharSequence reset() throws SourceIOException { + public BufferedCharSequence reset() throws SourceIOException { source.reset(); sink.reset(); decoder.reset(); @@ -274,7 +257,7 @@ reset(); int length = 0; while(sink.next()) { } - length = sink.buffer.scope.end; + length = sink.buffer.scope.end; return length; } @@ -409,13 +392,13 @@ */ public String getLineText(int start) { int oldPosition = changePosition(start); - String text = nextLineText(); + String text = nextLineText(); changePosition(oldPosition); return text; } - private char getCharAt(int index) throws IndexOutOfBoundsException { - if(sink.buffer.scope.isBefore(index)) { + private char getCharAt(int index) throws IndexOutOfBoundsException { + if(sink.buffer.scope.isBefore(index)) { reset(); } while(!sink.buffer.scope.isInside(index)) { @@ -424,7 +407,7 @@ throw new IndexOutOfBoundsException("index is " + index + " > lenght"); // NOI18N } - } + } return sink.charAt(index); } @@ -466,70 +449,68 @@ /** * The source buffer. */ - private class Source { + private class Source { private int maxBufferSize = MAX_SOURCE_BUFFER_SIZE; private ByteBuffer buffer; - private FileChannel channel; + private BufferedInputStream bstream; + private int bufferSize; - public Source(FileChannel channel) { - this.channel = channel; + public Source(InputStream inputStream, long bufferSize) { + this.bstream = new BufferedInputStream(inputStream); + this.bstream.mark(Integer.MAX_VALUE); + this.bufferSize = getBufferSize(bufferSize); buffer = newBuffer(); } @Override public String toString() { - return "source=[channel = "+channel+", buffer = "+buffer+"]"; + return "source=[stream = " + bstream.toString() + ", buffer = " + buffer + "]"; } private ByteBuffer newBuffer() { - return ByteBuffer.allocate(getBufferSize()); + return ByteBuffer.allocate(bufferSize); } - public void reset() { - try { - channel.position(0); + public void reset() { + try { + bstream.reset(); } catch (IOException ex) { throw new SourceIOException(ex); - } + } buffer.clear(); } /** - * Reads a sequence of bytes from the source channel. Bytes are read - * starting at the channel's current file position, and then the + * Reads a sequence of bytes from the source stream. Bytes are read + * starting at the stream's current position, and then the * position is updated with the number of bytes actually read. - * - * @return The number of bytes read, possibly zero, or -1 if the channel - * has reached end-of-stream - * + * + * @return The number of bytes read, possibly zero, or -1 if + * the end-of-stream is reached + * * @throws ProcessException If some other I/O error occurs */ private int read() { try { - return channel.read(buffer); + if(buffer.hasArray()) { + int res = bstream.read(buffer.array()); + if(res > 0) { + buffer.position(res); + } + return res; + } + throw new IOException("No byte array"); } catch (IOException ex) { throw new SourceIOException(ex); } } - public int position() throws IOException { - return (int) channel.position(); + public void close() throws IOException { + bstream.close(); } - public void close() throws IOException { - channel.close(); - } - - public int size() { - try { - return getSize(channel.size()); - } catch (IOException ioe) { - throw new SourceIOException(ioe); - } - } - - private int getSize(long size) { + public int getSize(long size) { if (size > Integer.MAX_VALUE) { LOG.warning("File size is " + size + "bytes. " + "Only first " + MAX_FILE_SIZE + @@ -539,8 +520,8 @@ return (int) size; } - private int getBufferSize() { - int size = Math.min(size(), maxBufferSize); + private int getBufferSize(long bufferSize) { + int size = Math.min(getSize(bufferSize), maxBufferSize); return size; } @@ -548,10 +529,10 @@ * * @return {@code true} if EOF, otherwise {@code false}. */ - public boolean readNext() { - buffer.clear(); - int status = read(); - buffer.flip(); + public boolean readNext() { + buffer.clear(); + int status = read(); + buffer.flip(); return status == -1; } @@ -578,7 +559,7 @@ return "sink = [" + buffer + "]"; } - public void reset() { + public void reset() { buffer.reset(); } @@ -595,7 +576,7 @@ * @return {@code true} is successful, otherwise {@code false}. */ private boolean next() { - CharBuffer out = buffer.clear(); + CharBuffer out = buffer.clear(); boolean endOfInput = false; if(coderResult == CoderResult.UNDERFLOW) { endOfInput = source.readNext(); @@ -603,7 +584,7 @@ while((coderResult = decoder.decode(source.buffer, out, endOfInput)) != CoderResult.UNDERFLOW) { - out = buffer.growBuffer(); + out = buffer.growBuffer(); } if(endOfInput) { while((coderResult = decoder.flush(out)) @@ -676,7 +657,7 @@ return c; } - private void reset() { + private void reset() { scope.reset(); charBuffer.clear(); } @@ -690,10 +671,10 @@ return charBuffer; } - private void adjustScope() { - scope.start = scope.end == -1 ? 0 : scope.end; - flip(); - scope.end = scope.start + charBuffer.limit(); + private void adjustScope() { + scope.start = scope.end == -1 ? 0 : scope.end; + flip(); + scope.end = scope.start + charBuffer.limit(); } /** diff -r daf94f0b9e06 utilities/src/org/netbeans/modules/search/ContextView.java --- a/utilities/src/org/netbeans/modules/search/ContextView.java Tue Feb 01 18:55:43 2011 +0100 +++ b/utilities/src/org/netbeans/modules/search/ContextView.java Wed Feb 02 14:44:02 2011 +0300 @@ -313,7 +313,7 @@ = matchingObj.checkValidity(); if (invalidityStatus != null) { displayMessage(invalidityStatus.getDescription( - matchingObj.getFile().getPath())); + matchingObj.getFileObject().getPath())); return; } diff -r daf94f0b9e06 utilities/src/org/netbeans/modules/search/MatchingObject.java --- a/utilities/src/org/netbeans/modules/search/MatchingObject.java Tue Feb 01 18:55:43 2011 +0100 +++ b/utilities/src/org/netbeans/modules/search/MatchingObject.java Wed Feb 02 14:44:02 2011 +0300 @@ -44,12 +44,9 @@ package org.netbeans.modules.search; -import org.openide.util.Exceptions; import java.awt.EventQueue; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; @@ -61,7 +58,6 @@ import java.util.regex.Matcher; import org.openide.filesystems.FileLock; import org.openide.filesystems.FileObject; -import org.openide.filesystems.FileUtil; import org.openide.loaders.DataObject; import org.openide.util.NbBundle; import static java.util.logging.Level.FINER; @@ -84,7 +80,7 @@ /** */ private final ResultModel resultModel; /** */ - private final File file; + private final FileObject fileObject; /** */ private final long timestamp; @@ -137,7 +133,7 @@ /** */ private boolean valid = true; /** */ - private StringBuilder text; + private String text; /** * Creates a new {@code MatchingObject} with a reference to the found @@ -162,9 +158,8 @@ this.object = object; this.charset = charset; - FileObject fileObject = getFileObject(); - file = FileUtil.toFile(fileObject); - timestamp = (file != null) ? file.lastModified() : 0L; + fileObject = fileObject(); + timestamp = fileObject.lastModified().getTime(); valid = (timestamp != 0L); setUpDataObjValidityChecking(); @@ -213,11 +208,17 @@ return valid && data != null ? data.isValid() : false; // #190819 } - FileObject getFileObject() { + private FileObject fileObject() { return object instanceof FileObject ? (FileObject) object : ((DataObject) getDataObject()).getPrimaryFile(); } + + /** + */ + FileObject getFileObject() { + return fileObject; + } /** */ @@ -378,15 +379,10 @@ return expanded; } - /** - */ - File getFile() { - return file; - } /** Get the name (not the path) of the file */ - String getName() { - return getFile().getName(); + String getName() { + return getFileObject().getName(); } String getHtmlDisplayName() { @@ -410,19 +406,14 @@ /** */ - String getDescription() { - return getFile().getParent(); + String getDescription() { + return getFileObject().getParent().getPath(); } /** */ String getText() throws IOException { - StringBuilder txt = text(); - if (txt != null) { - return txt.toString(); - } else { - return null; - } + return text(false); } /** @@ -437,17 +428,14 @@ * @author TimBoudreau * @author Marian Petras */ - private StringBuilder text() throws IOException { - return text(false); - } - - private StringBuilder text(boolean refreshCache) throws IOException { + + private String text(boolean refreshCache) throws IOException { assert !EventQueue.isDispatchThread(); - if (refreshCache || (text == null)) { - text = new StringBuilder(Utils.getCharSequence(new FileInputStream(getFile()), charset)); + if (refreshCache || (text == null)) { + text = getFileObject().asText(); } - return text == null ? new StringBuilder() : text; + return text; } @Override @@ -470,7 +458,7 @@ } throw new IOException("Unknown object in search: " + object);// NOI18N - } catch (IOException ex) { + } catch (IOException ex) { valid = false; return null; } @@ -549,7 +537,7 @@ InvalidityStatus status = getInvalidityStatus(); if (status != null) { - descr = status.getDescription(getFile().getPath()); + descr = status.getDescription(getFileObject().getPath()); } else { descr = null; } @@ -566,18 +554,17 @@ */ private InvalidityStatus getInvalidityStatus() { log(FINER, "getInvalidityStatus()"); //NOI18N - File f = getFile(); - if (!f.exists()) { - log(FINEST, " - DELETED"); + FileObject f = getFileObject(); + if (!f.isValid()) { + log(FINEST, " - DELETED"); return InvalidityStatus.DELETED; } - - if (f.isDirectory()) { - log(FINEST, " - BECAME_DIR"); + if (f.isFolder()) { + log(FINEST, " - BECAME_DIR"); return InvalidityStatus.BECAME_DIR; } - long stamp = f.lastModified(); + long stamp = f.lastModified().getTime(); if (stamp > resultModel.getCreationTime()) { log(SEVERE, "file's timestamp changed since start of the search"); if (LOG.isLoggable(FINEST)) { @@ -586,15 +573,15 @@ log(FINEST, " - file stamp: " + stamp + " (" + cal.getTime() + ')'); cal.setTimeInMillis(resultModel.getCreationTime()); log(FINEST, " - result model created: " + resultModel.getCreationTime() + " (" + cal.getTime() + ')'); - } + } return InvalidityStatus.CHANGED; } - if (f.length() > Integer.MAX_VALUE) { + if (f.getSize() > Integer.MAX_VALUE) { return InvalidityStatus.TOO_BIG; } - if (!f.canRead()) { + if (!f.canRead()) { return InvalidityStatus.CANT_READ; } @@ -619,8 +606,8 @@ if (shouldReplaceNone) { return null; } - - StringBuilder content = text(true); //refresh the cache, reads the file + + StringBuilder content = new StringBuilder(text(true)); //refresh the cache, reads the file List textMatches = resultModel.basicCriteria.getTextDetails(getFileObject()); @@ -667,8 +654,7 @@ throw new IllegalStateException("Buffer is gone"); //NOI18N } - if (REALLY_WRITE) { - final FileObject fileObject = getFileObject(); + if (REALLY_WRITE) { Writer writer = null; try { writer = new OutputStreamWriter( @@ -681,7 +667,7 @@ } } } else { - System.err.println("Would write to " + getFile().getPath());//NOI18N + System.err.println("Would write to " + getFileObject().getPath());//NOI18N System.err.println(text); } } @@ -689,7 +675,7 @@ /** */ private String makeStringToWrite() { - return makeStringToWrite(text); + return text; } /** @@ -726,7 +712,7 @@ return false; } final MatchingObject other = (MatchingObject) obj; - if (this.file != other.file && (this.file == null || !this.file.equals(other.file))) { + if (this.fileObject != other.fileObject && (this.fileObject == null || !this.fileObject.equals(other.fileObject))) { return false; } return true; @@ -735,7 +721,7 @@ @Override public int hashCode() { int hash = 3; - hash = 73 * hash + (this.file != null ? this.file.hashCode() : 0); + hash = 73 * hash + (this.fileObject != null ? this.fileObject.hashCode() : 0); return hash; } diff -r daf94f0b9e06 utilities/src/org/netbeans/modules/search/NodeListener.java --- a/utilities/src/org/netbeans/modules/search/NodeListener.java Tue Feb 01 18:55:43 2011 +0100 +++ b/utilities/src/org/netbeans/modules/search/NodeListener.java Wed Feb 02 14:44:02 2011 +0300 @@ -56,6 +56,7 @@ import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -75,6 +76,8 @@ import javax.swing.tree.TreeSelectionModel; import org.openide.actions.EditAction; import org.openide.cookies.EditCookie; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; import org.openide.nodes.Node; import org.openide.util.NbBundle; import org.openide.util.SharedClassObject; @@ -629,7 +632,10 @@ private CopyTextAction newCopyTextAction(TreePath path) { try { MatchingObject mo = getMatchingObject(path); - String filePath = mo.getFile().getCanonicalPath(); // NPE, IOE + + FileObject fileObject = mo.getFileObject(); + File file = FileUtil.toFile(fileObject); + String filePath = ( file != null )? file.getCanonicalPath() : fileObject.getPath(); String ctaName = NbBundle.getBundle(this.getClass()). getString("LBL_CopyFilePathAction"); // NOI18N CopyTextAction cta = new CopyTextAction(ctaName); diff -r daf94f0b9e06 utilities/src/org/netbeans/modules/search/ReplaceTask.java --- a/utilities/src/org/netbeans/modules/search/ReplaceTask.java Tue Feb 01 18:55:43 2011 +0100 +++ b/utilities/src/org/netbeans/modules/search/ReplaceTask.java Wed Feb 02 14:44:02 2011 +0300 @@ -135,7 +135,7 @@ for(MatchingObject mo: matchingObjects) { InvalidityStatus status = mo.checkValidity(); if (status != null) { - problems.add(status.getDescription(mo.getFile().getPath())); + problems.add(status.getDescription(mo.getFileObject().getPath())); if (++errorsCount > MAX_ERRORS_CHECKED) { break; } @@ -177,7 +177,7 @@ if (status == null) { obj.write(fileLock); } else { - errMessage = status.getDescription(obj.getFile().getPath()); + errMessage = status.getDescription(obj.getFileObject().getPath()); } } catch (FileAlreadyLockedException ex) { errMessage = createMsgFileLocked(obj); diff -r daf94f0b9e06 utilities/src/org/netbeans/modules/search/TextFetcher.java --- a/utilities/src/org/netbeans/modules/search/TextFetcher.java Tue Feb 01 18:55:43 2011 +0100 +++ b/utilities/src/org/netbeans/modules/search/TextFetcher.java Wed Feb 02 14:44:02 2011 +0300 @@ -100,8 +100,7 @@ return; } - FileObject fob = FileUtil.toFileObject( - source.matchingObj.getFile()); + FileObject fob = source.matchingObj.getFileObject(); String mimeType = fob.getMIMEType(); //We don't want the swing html editor kit, and even if we //do get it, it will frequently throw a random NPE @@ -166,8 +165,8 @@ return false; } - boolean result = source.matchingObj.getFile() - .equals(item.matchingObj.getFile()); + boolean result = source.matchingObj.getFileObject() + .equals(item.matchingObj.getFileObject()); if (result) { setLocation(item.getLocation()); task.schedule(50);