1 11 package org.eclipse.ui.editors.text; 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 import org.eclipse.ui.internal.editors.text.NLSUtility; 22 23 24 35 class DocumentInputStream extends InputStream { 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 DocumentInputStream(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 int read() throws IOException { 137 try { 138 return fOffset < fLength ? fCharSequence.charAt(fOffset++) : -1; 139 } catch (NullPointerException x) { 140 throw new IOException (TextEditorMessages.DocumentInputStream_error_streamClosed); 141 } catch (IndexOutOfBoundsException x) { 142 throw new IOException (NLSUtility.format(TextEditorMessages.DocumentInputStream_error_read, x.getLocalizedMessage())); 143 } 144 } 145 146 149 public void close() throws IOException { 150 synchronized (this) { 151 fCharSequence= null; 152 } 153 releaseDocument(); 154 } 155 156 159 private void handleDocumentAboutToBeChanged() { 160 IDocument document= fDocument; 161 if (fCharSequence == null || document == null) 162 return; 163 String content= document.get(); 164 synchronized (this) { 165 if (fCharSequence == null) 166 return; 167 fCharSequence= content; 168 } 169 releaseDocument(); 170 } 171 172 175 private synchronized void releaseDocument() { 176 if (fDocument != null) 177 fDocument.removeDocumentListener(fDocumentListener); 178 fDocument= null; 179 fDocumentListener= null; 180 } 181 } 182 | Popular Tags |