org.eclipse.search_3.7.0.v20110928-1504

16:47:40.148 INFO  jd.cli.Main - Decompiling org.eclipse.search_3.7.0.v20110928-1504.jar
package org.eclipse.search.core.text;

import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.search.internal.core.text.TextSearchVisitor;

class TextSearchEngine$1
  extends TextSearchEngine
{
  public IStatus search(TextSearchScope scope, TextSearchRequestor requestor, Pattern searchPattern, IProgressMonitor monitor)
  {
    return new TextSearchVisitor(requestor, searchPattern).search(scope, monitor);
  }
  
  public IStatus search(IFile[] scope, TextSearchRequestor requestor, Pattern searchPattern, IProgressMonitor monitor)
  {
    return new TextSearchVisitor(requestor, searchPattern).search(scope, monitor);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.search.core.text.TextSearchEngine.1
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.core.text;

import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.search.internal.core.text.TextSearchEngineRegistry;
import org.eclipse.search.internal.core.text.TextSearchVisitor;
import org.eclipse.search.internal.ui.SearchPlugin;

public abstract class TextSearchEngine
{
  public static TextSearchEngine create()
  {
    return SearchPlugin.getDefault().getTextSearchEngineRegistry().getPreferred();
  }
  
  public static TextSearchEngine createDefault()
  {
    new TextSearchEngine()
    {
      public IStatus search(TextSearchScope scope, TextSearchRequestor requestor, Pattern searchPattern, IProgressMonitor monitor)
      {
        return new TextSearchVisitor(requestor, searchPattern).search(scope, monitor);
      }
      
      public IStatus search(IFile[] scope, TextSearchRequestor requestor, Pattern searchPattern, IProgressMonitor monitor)
      {
        return new TextSearchVisitor(requestor, searchPattern).search(scope, monitor);
      }
    };
  }
  
  public abstract IStatus search(TextSearchScope paramTextSearchScope, TextSearchRequestor paramTextSearchRequestor, Pattern paramPattern, IProgressMonitor paramIProgressMonitor);
  
  public abstract IStatus search(IFile[] paramArrayOfIFile, TextSearchRequestor paramTextSearchRequestor, Pattern paramPattern, IProgressMonitor paramIProgressMonitor);
}

/* Location:
 * Qualified Name:     org.eclipse.search.core.text.TextSearchEngine
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.core.text;

import org.eclipse.core.resources.IFile;

public abstract class TextSearchMatchAccess
{
  public abstract IFile getFile();
  
  public abstract int getMatchOffset();
  
  public abstract int getMatchLength();
  
  public abstract int getFileContentLength();
  
  public abstract char getFileContentChar(int paramInt);
  
  public abstract String getFileContent(int paramInt1, int paramInt2);
}

/* Location:
 * Qualified Name:     org.eclipse.search.core.text.TextSearchMatchAccess
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.core.text;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

public abstract class TextSearchRequestor
{
  public void beginReporting() {}
  
  public void endReporting() {}
  
  public boolean acceptFile(IFile file)
    throws CoreException
  {
    return true;
  }
  
  public boolean reportBinaryFile(IFile file)
  {
    return false;
  }
  
  public boolean acceptPatternMatch(TextSearchMatchAccess matchAccess)
    throws CoreException
  {
    return true;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.search.core.text.TextSearchRequestor
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.core.text;

import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.search.internal.core.text.FileNamePatternSearchScope;
import org.eclipse.search.internal.core.text.FilesOfScopeCalculator;

public abstract class TextSearchScope
{
  public static TextSearchScope newSearchScope(IResource[] rootResources, Pattern fileNamePattern, boolean visitDerivedResources)
  {
    FileNamePatternSearchScope scope = FileNamePatternSearchScope.newSearchScope(new String(), rootResources, visitDerivedResources);
    scope.setFileNamePattern(fileNamePattern);
    return scope;
  }
  
  public IResource[] getRoots()
  {
    return new IResource[] { ResourcesPlugin.getWorkspace().getRoot() };
  }
  
  public abstract boolean contains(IResourceProxy paramIResourceProxy);
  
  public IFile[] evaluateFilesInScope(MultiStatus status)
  {
    return new FilesOfScopeCalculator(this, status).process();
  }
}

/* Location:
 * Qualified Name:     org.eclipse.search.core.text.TextSearchScope
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.internal.core.text;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;

public class DocumentCharSequence
  implements CharSequence
{
  private final IDocument fDocument;
  
  public DocumentCharSequence(IDocument document)
  {
    fDocument = document;
  }
  
  public int length()
  {
    return fDocument.getLength();
  }
  
  public char charAt(int index)
  {
    try
    {
      return fDocument.getChar(index);
    }
    catch (BadLocationException localBadLocationException)
    {
      throw new IndexOutOfBoundsException();
    }
  }
  
  public CharSequence subSequence(int start, int end)
  {
    try
    {
      return fDocument.get(start, end - start);
    }
    catch (BadLocationException localBadLocationException)
    {
      throw new IndexOutOfBoundsException();
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.search.internal.core.text.DocumentCharSequence
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.internal.core.text;

import java.io.IOException;
import java.io.Reader;

final class FileCharSequenceProvider$Buffer
{
  private final char[] fBuf;
  private int fOffset;
  private int fLength;
  private Buffer fNext;
  private Buffer fPrevious;
  
  public FileCharSequenceProvider$Buffer()
  {
    fBuf = new char[FileCharSequenceProvider.BUFFER_SIZE];
    reset();
    fNext = this;
    fPrevious = this;
  }
  
  public boolean contains(int pos)
  {
    int offset = fOffset;
    return (offset <= pos) && (pos < offset + fLength);
  }
  
  public boolean fill(Reader reader, int pos)
    throws IOException
  {
    int res = reader.read(fBuf);
    if (res == -1)
    {
      fOffset = pos;
      fLength = 0;
      return true;
    }
    int charsRead = res;
    while (charsRead < FileCharSequenceProvider.BUFFER_SIZE)
    {
      res = reader.read(fBuf, charsRead, FileCharSequenceProvider.BUFFER_SIZE - charsRead);
      if (res == -1)
      {
        fOffset = pos;
        fLength = charsRead;
        return true;
      }
      charsRead += res;
    }
    fOffset = pos;
    fLength = FileCharSequenceProvider.BUFFER_SIZE;
    return false;
  }
  
  public char get(int pos)
  {
    return fBuf[(pos - fOffset)];
  }
  
  public StringBuffer append(StringBuffer buf, int start, int length)
  {
    return buf.append(fBuf, start - fOffset, length);
  }
  
  public StringBuffer appendAll(StringBuffer buf)
  {
    return buf.append(fBuf, 0, fLength);
  }
  
  public int getEndOffset()
  {
    return fOffset + fLength;
  }
  
  public void removeFromChain()
  {
    fPrevious.fNext = fNext;
    fNext.fPrevious = fPrevious;
    
    fNext = this;
    fPrevious = this;
  }
  
  public void insertBefore(Buffer other)
  {
    fNext = other;
    fPrevious = fPrevious;
    fPrevious.fNext = this;
    fPrevious = this;
  }
  
  public Buffer getNext()
  {
    return fNext;
  }
  
  public Buffer getPrevious()
  {
    return fPrevious;
  }
  
  public void reset()
  {
    fOffset = -1;
    fLength = 0;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.search.internal.core.text.FileCharSequenceProvider.Buffer
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.internal.core.text;

import java.io.IOException;
import org.eclipse.core.runtime.CoreException;

final class FileCharSequenceProvider$CharSubSequence
  implements CharSequence
{
  private final int fSequenceOffset;
  private final int fSequenceLength;
  private final FileCharSequenceProvider.FileCharSequence fParent;
  
  public FileCharSequenceProvider$CharSubSequence(FileCharSequenceProvider.FileCharSequence parent, int offset, int length)
  {
    fParent = parent;
    fSequenceOffset = offset;
    fSequenceLength = length;
  }
  
  public int length()
  {
    return fSequenceLength;
  }
  
  public char charAt(int index)
  {
    if (index < 0) {
      throw new IndexOutOfBoundsException("index must be larger than 0");
    }
    if (index >= fSequenceLength) {
      throw new IndexOutOfBoundsException("index must be smaller than length");
    }
    return fParent.charAt(fSequenceOffset + index);
  }
  
  public CharSequence subSequence(int start, int end)
  {
    if (end < start) {
      throw new IndexOutOfBoundsException("end cannot be smaller than start");
    }
    if (start < 0) {
      throw new IndexOutOfBoundsException("start must be larger than 0");
    }
    if (end > fSequenceLength) {
      throw new IndexOutOfBoundsException("end must be smaller or equal than length");
    }
    return fParent.subSequence(fSequenceOffset + start, fSequenceOffset + end);
  }
  
  public String toString()
  {
    try
    {
      return fParent.getSubstring(fSequenceOffset, fSequenceLength);
    }
    catch (IOException e)
    {
      throw new FileCharSequenceProvider.FileCharSequenceException(e);
    }
    catch (CoreException e)
    {
      throw new FileCharSequenceProvider.FileCharSequenceException(e);
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.search.internal.core.text.FileCharSequenceProvider.CharSubSequence
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.internal.core.text;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

final class FileCharSequenceProvider$FileCharSequence
  implements CharSequence
{
  private static final String CHARSET_UTF_8 = "UTF-8";
  private Reader fReader;
  private int fReaderPos;
  private Integer fLength;
  private FileCharSequenceProvider.Buffer fMostCurrentBuffer;
  private int fNumberOfBuffers;
  private IFile fFile;
  final FileCharSequenceProvider this$0;
  
  public FileCharSequenceProvider$FileCharSequence(FileCharSequenceProvider paramFileCharSequenceProvider, IFile file)
    throws CoreException, IOException
  {
    this$0 = paramFileCharSequenceProvider;
    fNumberOfBuffers = 0;
    reset(file);
  }
  
  public void reset(IFile file)
    throws CoreException, IOException
  {
    fFile = file;
    fLength = null;
    
    FileCharSequenceProvider.Buffer curr = fMostCurrentBuffer;
    if (curr != null) {
      do
      {
        curr.reset();
        curr = curr.getNext();
      } while (curr != fMostCurrentBuffer);
    }
    initializeReader();
  }
  
  private void initializeReader()
    throws CoreException, IOException
  {
    if (fReader != null) {
      fReader.close();
    }
    String charset = fFile.getCharset();
    fReader = new InputStreamReader(getInputStream(charset), charset);
    fReaderPos = 0;
  }
  
  /* Error */
  private java.io.InputStream getInputStream(String charset)
    throws CoreException, IOException
  {
    // Byte code:
    //   0: iconst_0
    //   1: istore_2
    //   2: aload_0
    //   3: getfield 217	org/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence:fFile	Lorg/eclipse/core/resources/IFile;
    //   6: invokeinterface 257 1 0
    //   11: astore_3
    //   12: ldc 2
    //   14: aload_1
    //   15: invokevirtual 229	java/lang/String:equals	(Ljava/lang/Object;)Z
    //   18: ifeq +115 -> 133
    //   21: aload_0
    //   22: getfield 217	org/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence:fFile	Lorg/eclipse/core/resources/IFile;
    //   25: invokeinterface 259 1 0
    //   30: astore 4
    //   32: aload 4
    //   34: ifnull +99 -> 133
    //   37: aload 4
    //   39: getstatic 212	org/eclipse/core/runtime/content/IContentDescription:BYTE_ORDER_MARK	Lorg/eclipse/core/runtime/QualifiedName;
    //   42: invokeinterface 260 2 0
    //   47: ifnull +86 -> 133
    //   50: getstatic 211	org/eclipse/core/runtime/content/IContentDescription:BOM_UTF_8	[B
    //   53: arraylength
    //   54: istore 5
    //   56: iload 5
    //   58: newarray <illegal type>
    //   60: astore 6
    //   62: iconst_0
    //   63: istore 7
    //   65: aload_3
    //   66: aload 6
    //   68: iload 7
    //   70: iload 5
    //   72: iload 7
    //   74: isub
    //   75: invokevirtual 222	java/io/InputStream:read	([BII)I
    //   78: istore 8
    //   80: iload 8
    //   82: iconst_m1
    //   83: if_icmpne +11 -> 94
    //   86: new 111	java/io/IOException
    //   89: dup
    //   90: invokespecial 220	java/io/IOException:<init>	()V
    //   93: athrow
    //   94: iload 7
    //   96: iload 8
    //   98: iadd
    //   99: istore 7
    //   101: iload 7
    //   103: iload 5
    //   105: if_icmplt -40 -> 65
    //   108: aload 6
    //   110: getstatic 211	org/eclipse/core/runtime/content/IContentDescription:BOM_UTF_8	[B
    //   113: invokestatic 233	java/util/Arrays:equals	([B[B)Z
    //   116: ifne +17 -> 133
    //   119: aload_3
    //   120: invokevirtual 221	java/io/InputStream:close	()V
    //   123: aload_0
    //   124: getfield 217	org/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence:fFile	Lorg/eclipse/core/resources/IFile;
    //   127: invokeinterface 257 1 0
    //   132: astore_3
    //   133: iconst_1
    //   134: istore_2
    //   135: goto +24 -> 159
    //   138: astore 9
    //   140: iload_2
    //   141: ifne +15 -> 156
    //   144: aload_3
    //   145: ifnull +11 -> 156
    //   148: aload_3
    //   149: invokevirtual 221	java/io/InputStream:close	()V
    //   152: goto +4 -> 156
    //   155: pop
    //   156: aload 9
    //   158: athrow
    //   159: iload_2
    //   160: ifne +15 -> 175
    //   163: aload_3
    //   164: ifnull +11 -> 175
    //   167: aload_3
    //   168: invokevirtual 221	java/io/InputStream:close	()V
    //   171: goto +4 -> 175
    //   174: pop
    //   175: aload_3
    //   176: areturn
    // Line number table:
    //   Java source line #282	-> byte code offset #0
    //   Java source line #283	-> byte code offset #2
    //   Java source line #285	-> byte code offset #12
    //   Java source line #291	-> byte code offset #21
    //   Java source line #292	-> byte code offset #32
    //   Java source line #293	-> byte code offset #50
    //   Java source line #294	-> byte code offset #56
    //   Java source line #295	-> byte code offset #62
    //   Java source line #297	-> byte code offset #65
    //   Java source line #298	-> byte code offset #80
    //   Java source line #299	-> byte code offset #86
    //   Java source line #300	-> byte code offset #94
    //   Java source line #301	-> byte code offset #101
    //   Java source line #303	-> byte code offset #108
    //   Java source line #305	-> byte code offset #119
    //   Java source line #306	-> byte code offset #123
    //   Java source line #310	-> byte code offset #133
    //   Java source line #311	-> byte code offset #138
    //   Java source line #312	-> byte code offset #140
    //   Java source line #314	-> byte code offset #148
    //   Java source line #315	-> byte code offset #155
    //   Java source line #318	-> byte code offset #156
    //   Java source line #312	-> byte code offset #159
    //   Java source line #314	-> byte code offset #167
    //   Java source line #315	-> byte code offset #174
    //   Java source line #319	-> byte code offset #175
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	177	0	this	FileCharSequence
    //   0	177	1	charset	String
    //   1	159	2	ok	boolean
    //   11	165	3	contents	java.io.InputStream
    //   30	8	4	description	org.eclipse.core.runtime.content.IContentDescription
    //   54	50	5	bomLength	int
    //   60	49	6	bomStore	byte[]
    //   63	39	7	bytesRead	int
    //   78	19	8	bytes	int
    //   138	19	9	localObject	Object
    //   155	1	10	localIOException1	IOException
    //   174	1	11	localIOException2	IOException
    // Exception table:
    //   from	to	target	type
    //   12	138	138	finally
    //   148	152	155	java/io/IOException
    //   167	171	174	java/io/IOException
  }
  
  private void clearReader()
    throws IOException
  {
    if (fReader != null) {
      fReader.close();
    }
    fReader = null;
    fReaderPos = Integer.MAX_VALUE;
  }
  
  public int length()
  {
    if (fLength == null) {
      try
      {
        getBuffer(Integer.MAX_VALUE);
      }
      catch (IOException e)
      {
        throw new FileCharSequenceProvider.FileCharSequenceException(e);
      }
      catch (CoreException e)
      {
        throw new FileCharSequenceProvider.FileCharSequenceException(e);
      }
    }
    return fLength.intValue();
  }
  
  private FileCharSequenceProvider.Buffer getBuffer(int pos)
    throws IOException, CoreException
  {
    FileCharSequenceProvider.Buffer curr = fMostCurrentBuffer;
    if (curr != null) {
      do
      {
        if (curr.contains(pos)) {
          return curr;
        }
        curr = curr.getNext();
      } while (curr != fMostCurrentBuffer);
    }
    FileCharSequenceProvider.Buffer buf = findBufferToUse();
    fillBuffer(buf, pos);
    if (buf.contains(pos)) {
      return buf;
    }
    return null;
  }
  
  private FileCharSequenceProvider.Buffer findBufferToUse()
  {
    if (fNumberOfBuffers < FileCharSequenceProvider.access$0())
    {
      fNumberOfBuffers += 1;
      FileCharSequenceProvider.Buffer newBuffer = new FileCharSequenceProvider.Buffer();
      if (fMostCurrentBuffer == null)
      {
        fMostCurrentBuffer = newBuffer;
        return newBuffer;
      }
      newBuffer.insertBefore(fMostCurrentBuffer);
      return newBuffer;
    }
    return fMostCurrentBuffer.getPrevious();
  }
  
  private boolean fillBuffer(FileCharSequenceProvider.Buffer buffer, int pos)
    throws CoreException, IOException
  {
    if (fReaderPos > pos) {
      initializeReader();
    }
    do
    {
      boolean endReached = buffer.fill(fReader, fReaderPos);
      fReaderPos = buffer.getEndOffset();
      if (endReached)
      {
        fLength = new Integer(fReaderPos);
        fReaderPos = Integer.MAX_VALUE;
        return true;
      }
    } while (fReaderPos <= pos);
    return true;
  }
  
  public char charAt(int index)
  {
    FileCharSequenceProvider.Buffer current = fMostCurrentBuffer;
    if ((current != null) && (current.contains(index))) {
      return current.get(index);
    }
    if (index < 0) {
      throw new IndexOutOfBoundsException("index must be larger than 0");
    }
    if ((fLength != null) && (index >= fLength.intValue())) {
      throw new IndexOutOfBoundsException("index must be smaller than length");
    }
    try
    {
      FileCharSequenceProvider.Buffer buffer = getBuffer(index);
      if (buffer == null) {
        throw new IndexOutOfBoundsException("index must be smaller than length");
      }
      if (buffer != fMostCurrentBuffer)
      {
        if (buffer.getNext() != fMostCurrentBuffer)
        {
          buffer.removeFromChain();
          buffer.insertBefore(fMostCurrentBuffer);
        }
        fMostCurrentBuffer = buffer;
      }
      return buffer.get(index);
    }
    catch (IOException e)
    {
      throw new FileCharSequenceProvider.FileCharSequenceException(e);
    }
    catch (CoreException e)
    {
      throw new FileCharSequenceProvider.FileCharSequenceException(e);
    }
  }
  
  public String getSubstring(int start, int length)
    throws IOException, CoreException
  {
    int pos = start;
    int endPos = start + length;
    if ((fLength != null) && (endPos > fLength.intValue())) {
      throw new IndexOutOfBoundsException("end must be smaller than length");
    }
    StringBuffer res = new StringBuffer(length);
    
    FileCharSequenceProvider.Buffer buffer = getBuffer(pos);
    while ((pos < endPos) && (buffer != null))
    {
      int bufEnd = buffer.getEndOffset();
      if (bufEnd >= endPos) {
        return buffer.append(res, pos, endPos - pos).toString();
      }
      buffer.append(res, pos, bufEnd - pos);
      pos = bufEnd;
      buffer = getBuffer(pos);
    }
    return res.toString();
  }
  
  public CharSequence subSequence(int start, int end)
  {
    if (end < start) {
      throw new IndexOutOfBoundsException("end cannot be smaller than start");
    }
    if (start < 0) {
      throw new IndexOutOfBoundsException("start must be larger than 0");
    }
    if ((fLength != null) && (end > fLength.intValue())) {
      throw new IndexOutOfBoundsException("end must be smaller than length");
    }
    return new FileCharSequenceProvider.CharSubSequence(this, start, end - start);
  }
  
  public void close()
    throws IOException
  {
    clearReader();
  }
  
  public String toString()
  {
    int len = fLength != null ? fLength.intValue() : 4000;
    StringBuffer res = new StringBuffer(len);
    try
    {
      FileCharSequenceProvider.Buffer buffer = getBuffer(0);
      while (buffer != null)
      {
        buffer.appendAll(res);
        buffer = getBuffer(res.length());
      }
      return res.toString();
    }
    catch (IOException e)
    {
      throw new FileCharSequenceProvider.FileCharSequenceException(e);
    }
    catch (CoreException e)
    {
      throw new FileCharSequenceProvider.FileCharSequenceException(e);
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.search.internal.core.text.FileCharSequenceProvider.FileCharSequence
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.internal.core.text;

import java.io.IOException;
import org.eclipse.core.runtime.CoreException;

public class FileCharSequenceProvider$FileCharSequenceException
  extends RuntimeException
{
  private static final long serialVersionUID = 1L;
  
  FileCharSequenceProvider$FileCharSequenceException(IOException e)
  {
    super(e);
  }
  
  FileCharSequenceProvider$FileCharSequenceException(CoreException e)
  {
    super(e);
  }
  
  public void throwWrappedException()
    throws CoreException, IOException
  {
    Throwable wrapped = getCause();
    if ((wrapped instanceof CoreException)) {
      throw ((CoreException)wrapped);
    }
    if ((wrapped instanceof IOException)) {
      throw ((IOException)wrapped);
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.search.internal.core.text.FileCharSequenceProvider.FileCharSequenceException
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.search.internal.core.text;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

public class FileCharSequenceProvider
{
  private static int NUMBER_OF_BUFFERS = 3;
  public static int BUFFER_SIZE = 524288;
  private FileCharSequence fReused = null;
  
  public CharSequence newCharSequence(IFile file)
    throws CoreException, IOException
  {
    if (fReused == null) {
      return new FileCharSequence(file);
    }
    FileCharSequence curr = fReused;
    fReused = null;
    curr.reset(file);
    return curr;
  }
  
  /* Error */
  public void releaseCharSequence(CharSequence seq)
    throws IOException
  {
    // Byte code:
    //   0: aload_1
    //   1: instanceof 34
    //   4: ifeq +42 -> 46
    //   7: aload_1
    //   8: checkcast 34	org/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence
    //   11: astore_2
    //   12: aload_2
    //   13: invokevirtual 55	org/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence:close	()V
    //   16: goto +18 -> 34
    //   19: astore_3
    //   20: aload_0
    //   21: getfield 53	org/eclipse/search/internal/core/text/FileCharSequenceProvider:fReused	Lorg/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence;
    //   24: ifnonnull +8 -> 32
    //   27: aload_0
    //   28: aload_2
    //   29: putfield 53	org/eclipse/search/internal/core/text/FileCharSequenceProvider:fReused	Lorg/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence;
    //   32: aload_3
    //   33: athrow
    //   34: aload_0
    //   35: getfield 53	org/eclipse/search/internal/core/text/FileCharSequenceProvider:fReused	Lorg/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence;
    //   38: ifnonnull +8 -> 46
    //   41: aload_0
    //   42: aload_2
    //   43: putfield 53	org/eclipse/search/internal/core/text/FileCharSequenceProvider:fReused	Lorg/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence;
    //   46: return
    // Line number table:
    //   Java source line #45	-> byte code offset #0
    //   Java source line #46	-> byte code offset #7
    //   Java source line #48	-> byte code offset #12
    //   Java source line #49	-> byte code offset #19
    //   Java source line #50	-> byte code offset #20
    //   Java source line #51	-> byte code offset #27
    //   Java source line #53	-> byte code offset #32
    //   Java source line #50	-> byte code offset #34
    //   Java source line #51	-> byte code offset #41
    //   Java source line #55	-> byte code offset #46
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	47	0	this	FileCharSequenceProvider
    //   0	47	1	seq	CharSequence
    //   11	32	2	curr	FileCharSequence
    //   19	14	3	localObject	Object
    // Exception table:
    //   from	to	target	type
    //   12	19	19	finally
  }
  
  public static class FileCharSequenceException
    extends RuntimeException
  {
    private static final long serialVersionUID = 1L;
    
    FileCharSequenceException(IOException e)
    {
      super();
    }
    
    FileCharSequenceException(CoreException e)
    {
      super();
    }
    
    public void throwWrappedException()
      throws CoreException, IOException
    {
      Throwable wrapped = getCause();
      if ((wrapped instanceof CoreException)) {
        throw ((CoreException)wrapped);
      }
      if ((wrapped instanceof IOException)) {
        throw ((IOException)wrapped);
      }
    }
  }
  
  private static final class CharSubSequence
    implements CharSequence
  {
    private final int fSequenceOffset;
    private final int fSequenceLength;
    private final FileCharSequenceProvider.FileCharSequence fParent;
    
    public CharSubSequence(FileCharSequenceProvider.FileCharSequence parent, int offset, int length)
    {
      fParent = parent;
      fSequenceOffset = offset;
      fSequenceLength = length;
    }
    
    public int length()
    {
      return fSequenceLength;
    }
    
    public char charAt(int index)
    {
      if (index < 0) {
        throw new IndexOutOfBoundsException("index must be larger than 0");
      }
      if (index >= fSequenceLength) {
        throw new IndexOutOfBoundsException("index must be smaller than length");
      }
      return fParent.charAt(fSequenceOffset + index);
    }
    
    public CharSequence subSequence(int start, int end)
    {
      if (end < start) {
        throw new IndexOutOfBoundsException("end cannot be smaller than start");
      }
      if (start < 0) {
        throw new IndexOutOfBoundsException("start must be larger than 0");
      }
      if (end > fSequenceLength) {
        throw new IndexOutOfBoundsException("end must be smaller or equal than length");
      }
      return fParent.subSequence(fSequenceOffset + start, fSequenceOffset + end);
    }
    
    public String toString()
    {
      try
      {
        return fParent.getSubstring(fSequenceOffset, fSequenceLength);
      }
      catch (IOException e)
      {
        throw new FileCharSequenceProvider.FileCharSequenceException(e);
      }
      catch (CoreException e)
      {
        throw new FileCharSequenceProvider.FileCharSequenceException(e);
      }
    }
  }
  
  private static final class Buffer
  {
    private final char[] fBuf;
    private int fOffset;
    private int fLength;
    private Buffer fNext;
    private Buffer fPrevious;
    
    public Buffer()
    {
      fBuf = new char[FileCharSequenceProvider.BUFFER_SIZE];
      reset();
      fNext = this;
      fPrevious = this;
    }
    
    public boolean contains(int pos)
    {
      int offset = fOffset;
      return (offset <= pos) && (pos < offset + fLength);
    }
    
    public boolean fill(Reader reader, int pos)
      throws IOException
    {
      int res = reader.read(fBuf);
      if (res == -1)
      {
        fOffset = pos;
        fLength = 0;
        return true;
      }
      int charsRead = res;
      while (charsRead < FileCharSequenceProvider.BUFFER_SIZE)
      {
        res = reader.read(fBuf, charsRead, FileCharSequenceProvider.BUFFER_SIZE - charsRead);
        if (res == -1)
        {
          fOffset = pos;
          fLength = charsRead;
          return true;
        }
        charsRead += res;
      }
      fOffset = pos;
      fLength = FileCharSequenceProvider.BUFFER_SIZE;
      return false;
    }
    
    public char get(int pos)
    {
      return fBuf[(pos - fOffset)];
    }
    
    public StringBuffer append(StringBuffer buf, int start, int length)
    {
      return buf.append(fBuf, start - fOffset, length);
    }
    
    public StringBuffer appendAll(StringBuffer buf)
    {
      return buf.append(fBuf, 0, fLength);
    }
    
    public int getEndOffset()
    {
      return fOffset + fLength;
    }
    
    public void removeFromChain()
    {
      fPrevious.fNext = fNext;
      fNext.fPrevious = fPrevious;
      
      fNext = this;
      fPrevious = this;
    }
    
    public void insertBefore(Buffer other)
    {
      fNext = other;
      fPrevious = fPrevious;
      fPrevious.fNext = this;
      fPrevious = this;
    }
    
    public Buffer getNext()
    {
      return fNext;
    }
    
    public Buffer getPrevious()
    {
      return fPrevious;
    }
    
    public void reset()
    {
      fOffset = -1;
      fLength = 0;
    }
  }
  
  private final class FileCharSequence
    implements CharSequence
  {
    private static final String CHARSET_UTF_8 = "UTF-8";
    private Reader fReader;
    private int fReaderPos;
    private Integer fLength;
    private FileCharSequenceProvider.Buffer fMostCurrentBuffer;
    private int fNumberOfBuffers;
    private IFile fFile;
    
    public FileCharSequence(IFile file)
      throws CoreException, IOException
    {
      fNumberOfBuffers = 0;
      reset(file);
    }
    
    public void reset(IFile file)
      throws CoreException, IOException
    {
      fFile = file;
      fLength = null;
      
      FileCharSequenceProvider.Buffer curr = fMostCurrentBuffer;
      if (curr != null) {
        do
        {
          curr.reset();
          curr = curr.getNext();
        } while (curr != fMostCurrentBuffer);
      }
      initializeReader();
    }
    
    private void initializeReader()
      throws CoreException, IOException
    {
      if (fReader != null) {
        fReader.close();
      }
      String charset = fFile.getCharset();
      fReader = new InputStreamReader(getInputStream(charset), charset);
      fReaderPos = 0;
    }
    
    /* Error */
    private java.io.InputStream getInputStream(String charset)
      throws CoreException, IOException
    {
      // Byte code:
      //   0: iconst_0
      //   1: istore_2
      //   2: aload_0
      //   3: getfield 217	org/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence:fFile	Lorg/eclipse/core/resources/IFile;
      //   6: invokeinterface 257 1 0
      //   11: astore_3
      //   12: ldc 2
      //   14: aload_1
      //   15: invokevirtual 229	java/lang/String:equals	(Ljava/lang/Object;)Z
      //   18: ifeq +115 -> 133
      //   21: aload_0
      //   22: getfield 217	org/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence:fFile	Lorg/eclipse/core/resources/IFile;
      //   25: invokeinterface 259 1 0
      //   30: astore 4
      //   32: aload 4
      //   34: ifnull +99 -> 133
      //   37: aload 4
      //   39: getstatic 212	org/eclipse/core/runtime/content/IContentDescription:BYTE_ORDER_MARK	Lorg/eclipse/core/runtime/QualifiedName;
      //   42: invokeinterface 260 2 0
      //   47: ifnull +86 -> 133
      //   50: getstatic 211	org/eclipse/core/runtime/content/IContentDescription:BOM_UTF_8	[B
      //   53: arraylength
      //   54: istore 5
      //   56: iload 5
      //   58: newarray <illegal type>
      //   60: astore 6
      //   62: iconst_0
      //   63: istore 7
      //   65: aload_3
      //   66: aload 6
      //   68: iload 7
      //   70: iload 5
      //   72: iload 7
      //   74: isub
      //   75: invokevirtual 222	java/io/InputStream:read	([BII)I
      //   78: istore 8
      //   80: iload 8
      //   82: iconst_m1
      //   83: if_icmpne +11 -> 94
      //   86: new 111	java/io/IOException
      //   89: dup
      //   90: invokespecial 220	java/io/IOException:<init>	()V
      //   93: athrow
      //   94: iload 7
      //   96: iload 8
      //   98: iadd
      //   99: istore 7
      //   101: iload 7
      //   103: iload 5
      //   105: if_icmplt -40 -> 65
      //   108: aload 6
      //   110: getstatic 211	org/eclipse/core/runtime/content/IContentDescription:BOM_UTF_8	[B
      //   113: invokestatic 233	java/util/Arrays:equals	([B[B)Z
      //   116: ifne +17 -> 133
      //   119: aload_3
      //   120: invokevirtual 221	java/io/InputStream:close	()V
      //   123: aload_0
      //   124: getfield 217	org/eclipse/search/internal/core/text/FileCharSequenceProvider$FileCharSequence:fFile	Lorg/eclipse/core/resources/IFile;
      //   127: invokeinterface 257 1 0
      //   132: astore_3
      //   133: iconst_1
      //   134: istore_2
      //   135: goto +24 -> 159
      //   138: astore 9
      //   140: iload_2
      //   141: ifne +15 -> 156
      //   144: aload_3
      //   145: ifnull +11 -> 156
      //   148: aload_3
      //   149: invokevirtual 221	java/io/InputStream:close	()V
      //   152: goto +4 -> 156
      //   155: pop
      //   156: aload 9
      //   158: athrow
      //   159: iload_2
      //   160: ifne +15 -> 175
      //   163: aload_3
      //   164: ifnull +11 -> 175
      //   167: aload_3
      //   168: invokevirtual 221	java/io/InputStream:close	()V
      //   171: goto +4 -> 175
      //   174: pop
      //   175: aload_3
      //   176: areturn
      // Line number table:
      //   Java source line #282	-> byte code offset #0
      //   Java source line #283	-> byte code offset #2
      //   Java source line #285	-> byte code offset #12
      //   Java source line #291	-> byte code offset #21
      //   Java source line #292	-> byte code offset #32
      //   Java source line #293	-> byte code offset #50
      //   Java source line #294	-> byte code offset #56
      //   Java source line #295	-> byte code offset #62
      //   Java source line #297	-> byte code offset #65
      //   Java source line #298	-> byte code offset #80
      //   Java source line #299	-> byte code offset #86
      //   Java source line #300	-> byte code offset #94
      //   Java source line #301	-> byte code offset #101
      //   Java source line #303	-> byte code offset #108
      //   Java source line #305	-> byte code offset #119
      //   Java source line #306	-> byte code offset #123
      //   Java source line #310	-> byte code offset #133
      //   Java source line #311	-> byte code offset #138
      //   Java source line #312	-> byte code offset #140
      //   Java source line #314	-> byte code offset #148
      //   Java source line #315	-> byte code offset #155
      //   Java source line #318	-> byte code offset #156
      //   Java source line #312	-> byte code offset #159
      //   Java source line #314	-> byte code offset #167
      //   Java source line #315	-> byte code offset #174
      //   Java source line #319	-> byte code offset #175
      // Local variable table:
      //   start	length	slot	name	signature
      //   0	177	0	this	FileCharSequence
      //   0	177	1	charset	String
      //   1	159	2	ok	boolean
      //   11	165	3	contents	java.io.InputStream
      //   30	8	4	description	org.eclipse.core.runtime.content.IContentDescription
      //   54	50	5	bomLength	int
      //   60	49	6	bomStore	byte[]
      //   63	39	7	bytesRead	int
      //   78	19	8	bytes	int
      //   138	19	9	localObject	Object
      //   155	1	10	localIOException1	IOException
      //   174	1	11	localIOException2	IOException
      // Exception table:
      //   from	to	target	type
      //   12	138	138	finally
      //   148	152	155	java/io/IOException
      //   167	171	174	java/io/IOException
    }
    
    private void clearReader()
      throws IOException
    {
      if (fReader != null) {
        fReader.close();
      }
      fReader = null;
      fReaderPos = Integer.MAX_VALUE;
    }
    
    public int length()
    {
      if (fLength == null) {
        try
        {
          getBuffer(Integer.MAX_VALUE);
        }
        catch (IOException e)
        {
          throw new FileCharSequenceProvider.FileCharSequenceException(e);
        }
        catch (CoreException e)
        {
          throw new FileCharSequenceProvider.FileCharSequenceException(e);
        }
      }
      return fLength.intValue();
    }
    
    private FileCharSequenceProvider.Buffer getBuffer(int pos)
      throws IOException, CoreException
    {
      FileCharSequenceProvider.Buffer curr = fMostCurrentBuffer;
      if (curr != null) {
        do
        {
          if (curr.contains(pos)) {
            return curr;
          }
          curr = curr.getNext();
        } while (curr != fMostCurrentBuffer);
      }
      FileCharSequenceProvider.Buffer buf = findBufferToUse();
      fillBuffer(buf, pos);
      if (buf.contains(pos)) {
        return buf;
      }
      return null;
    }
    
    private FileCharSequenceProvider.Buffer findBufferToUse()
    {
      if (fNumberOfBuffers < FileCharSequenceProvider.NUMBER_OF_BUFFERS)
      {
        fNumberOfBuffers += 1;
        FileCharSequenceProvider.Buffer newBuffer = new FileCharSequenceProvider.Buffer();
        if (fMostCurrentBuffer == null)
        {
          fMostCurrentBuffer = newBuffer;
          return newBuffer;
        }
        newBuffer.insertBefore(fMostCurrentBuffer);
        return newBuffer;
      }
      return fMostCurrentBuffer.getPrevious();
    }
    
    private boolean fillBuffer(FileCharSequenceProvider.Buffer buffer, int pos)
      throws CoreException, IOException
    {
      if (fReaderPos > pos) {
        initializeReader();
      }
      do
      {
        boolean endReached = buffer.fill(fReader, fReaderPos);
        fReaderPos = buffer.getEndOffset();
        if (endReached)
        {
          fLength = new Integer(fReaderPos);
          fReaderPos = Integer.MAX_VALUE;
          return true;
        }
      } while (fReaderPos <= pos);
      return true;
    }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd