1 11 package org.eclipse.core.internal.filebuffers; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 16 import org.eclipse.jface.text.Assert; 17 import org.eclipse.jface.text.BadLocationException; 18 import org.eclipse.jface.text.DocumentEvent; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.IDocumentListener; 21 22 23 34 class DocumentInputStream extends InputStream { 35 36 39 private static class DocumentCharSequence implements CharSequence { 40 41 42 private IDocument fDocument; 43 44 50 public DocumentCharSequence(IDocument document) { 51 fDocument= document; 52 } 53 54 57 public int length() { 58 return fDocument.getLength(); 59 } 60 61 64 public char charAt(int index) { 65 try { 66 return fDocument.getChar(index); 67 } catch (BadLocationException x) { 68 throw new IndexOutOfBoundsException (x.getLocalizedMessage()); 69 } 70 } 71 72 75 public CharSequence subSequence(int start, int end) { 76 try { 77 return fDocument.get(start, end - start); 78 } catch (BadLocationException x) { 79 throw new IndexOutOfBoundsException (x.getLocalizedMessage()); 80 } 81 } 82 } 83 84 87 private class InternalDocumentListener implements IDocumentListener { 88 89 92 public void documentAboutToBeChanged(DocumentEvent event) { 93 handleDocumentAboutToBeChanged(); 94 } 95 96 99 public void documentChanged(DocumentEvent event) { 100 } 101 } 102 103 104 private volatile CharSequence fCharSequence; 105 106 107 private int fLength; 108 109 110 private int fOffset= 0; 111 112 113 private IDocument fDocument; 114 115 116 private IDocumentListener fDocumentListener= new InternalDocumentListener(); 117 118 124 public DocumentInputStream(IDocument document) { 125 Assert.isNotNull(document); 126 fDocument= document; 127 fCharSequence= new DocumentCharSequence(fDocument); 128 fDocument.addDocumentListener(fDocumentListener); 129 fLength= fCharSequence.length(); 130 } 131 132 135 public int read() throws IOException { 136 try { 137 return fOffset < fLength ? fCharSequence.charAt(fOffset++) : -1; 138 } catch (NullPointerException x) { 139 throw new IOException (FileBuffersMessages.DocumentInputStream_error_streamClosed); 140 } catch (IndexOutOfBoundsException x) { 141 throw new IOException (NLSUtility.format(FileBuffersMessages.DocumentInputStream_error_read, x.getLocalizedMessage())); 142 } 143 } 144 145 148 public void close() throws IOException { 149 synchronized (this) { 150 fCharSequence= null; 151 } 152 releaseDocument(); 153 } 154 155 158 private void handleDocumentAboutToBeChanged() { 159 IDocument document= fDocument; 160 if (fCharSequence == null || document == null) 161 return; 162 String content= document.get(); 163 synchronized (this) { 164 if (fCharSequence == null) 165 return; 166 fCharSequence= content; 167 } 168 releaseDocument(); 169 } 170 171 174 private synchronized void releaseDocument() { 175 if (fDocument != null) 176 fDocument.removeDocumentListener(fDocumentListener); 177 fDocument= null; 178 fDocumentListener= null; 179 } 180 } 181 | Popular Tags |