1 11 package org.eclipse.core.internal.filebuffers; 12 13 import java.io.IOException ; 14 import java.io.Reader ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.DocumentEvent; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.jface.text.IDocumentListener; 22 23 24 35 class DocumentReader extends Reader { 36 37 40 private static class DocumentCharSequence implements CharSequence { 41 42 43 private IDocument fDocument; 44 45 51 public DocumentCharSequence(IDocument document) { 52 fDocument= document; 53 } 54 55 58 public int length() { 59 return fDocument.getLength(); 60 } 61 62 65 public char charAt(int index) { 66 try { 67 return fDocument.getChar(index); 68 } catch (BadLocationException x) { 69 throw new IndexOutOfBoundsException (x.getLocalizedMessage()); 70 } 71 } 72 73 76 public CharSequence subSequence(int start, int end) { 77 try { 78 return fDocument.get(start, end - start); 79 } catch (BadLocationException x) { 80 throw new IndexOutOfBoundsException (x.getLocalizedMessage()); 81 } 82 } 83 } 84 85 88 private class InternalDocumentListener implements IDocumentListener { 89 90 93 public void documentAboutToBeChanged(DocumentEvent event) { 94 handleDocumentAboutToBeChanged(); 95 } 96 97 100 public void documentChanged(DocumentEvent event) { 101 } 102 } 103 104 105 private volatile CharSequence fCharSequence; 106 107 108 private int fLength; 109 110 111 private int fOffset= 0; 112 113 114 private IDocument fDocument; 115 116 117 private IDocumentListener fDocumentListener= new InternalDocumentListener(); 118 119 125 public DocumentReader(IDocument document) { 126 Assert.isNotNull(document); 127 fDocument= document; 128 fCharSequence= new DocumentCharSequence(fDocument); 129 fDocument.addDocumentListener(fDocumentListener); 130 fLength= fCharSequence.length(); 131 } 132 133 136 public void close() throws IOException { 137 synchronized (this) { 138 fCharSequence= null; 139 } 140 releaseDocument(); 141 } 142 143 146 private void handleDocumentAboutToBeChanged() { 147 IDocument document= fDocument; 148 if (fCharSequence == null || document == null) 149 return; 150 String content= document.get(); 151 synchronized (this) { 152 if (fCharSequence == null) 153 return; 154 fCharSequence= content; 155 } 156 releaseDocument(); 157 } 158 159 162 private synchronized void releaseDocument() { 163 if (fDocument != null) 164 fDocument.removeDocumentListener(fDocumentListener); 165 fDocument= null; 166 fDocumentListener= null; 167 } 168 169 173 public int read(char[] cbuf, int off, int len) throws IOException { 174 int i= 0; 175 try { 176 for (; i < len && fOffset < fLength; i++) 177 cbuf[off + i]= fCharSequence.charAt(fOffset++); 178 if (i > 0) 179 return i; 180 181 return -1; 182 } catch (NullPointerException x) { 183 throw new IOException (FileBuffersMessages.DocumentInputStream_error_streamClosed); 184 } catch (IndexOutOfBoundsException x) { 185 return i-1; 186 } 187 } 188 } 189 | Popular Tags |