Skip to main content
summaryrefslogtreecommitdiffstats
blob: bc9bb766f2327506489d950742460bf30398655a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.editors.text;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.jface.text.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;


/**
 * An <code>InputStream</code> that reads from an <code>IDocument</code>.
 * The input stream ensures that its content is the same as the document
 * content when the stream was created.
 * <p>
 * Note that {@link #close()} must be called to release any acquired
 * resources.
 * </p>
 * 
 * @since 3.1
 */
class DocumentInputStream extends InputStream {
	
	/**
	 * Document based character sequence.
	 */
	private static class DocumentCharSequence implements CharSequence {

		/** Document */
		private IDocument fDocument;
		
		/**
		 * Initialize with the sequence of characters in the given
		 * document.
		 * 
		 * @param document the document
		 */
		public DocumentCharSequence(IDocument document) {
			fDocument= document;
		}
		
		/*
		 * @see java.lang.CharSequence#length()
		 */
		public int length() {
			return fDocument.getLength();
		}

		/*
		 * @see java.lang.CharSequence#charAt(int)
		 */
		public char charAt(int index) {
			try {
				return fDocument.getChar(index);
			} catch (BadLocationException x) {
				throw new IndexOutOfBoundsException(x.getLocalizedMessage());
			}
		}

		/*
		 * @see java.lang.CharSequence#subSequence(int, int)
		 */
		public CharSequence subSequence(int start, int end) {
			try {
				return fDocument.get(start, end - start);
			} catch (BadLocationException x) {
				throw new IndexOutOfBoundsException(x.getLocalizedMessage());
			}
		}
	}
	
	/**
	 * Internal document listener.
	 */
	private class InternalDocumentListener implements IDocumentListener {

		/*
		 * @see org.eclipse.jface.text.IDocumentListener#documentAboutToBeChanged(org.eclipse.jface.text.DocumentEvent)
		 */
		public void documentAboutToBeChanged(DocumentEvent event) {
			handleDocumentAboutToBeChanged();
		}

		/*
		 * @see org.eclipse.jface.text.IDocumentListener#documentChanged(org.eclipse.jface.text.DocumentEvent)
		 */
		public void documentChanged(DocumentEvent event) {
		}
	}

	/** The character sequence. */
	private volatile CharSequence fCharSequence;
	
	/** Document length. */
	private int fLength;
	
	/** The current offset. */
	private int fOffset= 0;
	
	/** The document. */
	private IDocument fDocument;
	
	/** The document listener. */
	private IDocumentListener fDocumentListener= new InternalDocumentListener();
	
	/**
	 * Creates a new document input stream and initializes
	 * the stream to read from the given document.
	 * 
	 * @param document the document
	 */
	public DocumentInputStream(IDocument document) {
		Assert.isNotNull(document);
		fDocument= document;
		fCharSequence= new DocumentCharSequence(fDocument);
		fDocument.addDocumentListener(fDocumentListener);
		fLength= fCharSequence.length();
	}

	/*
	 * @see java.io.InputStream#read()
	 */
	public int read() throws IOException {
		try {
			return fOffset < fLength ? fCharSequence.charAt(fOffset++) : -1;
		} catch (NullPointerException x) {
			throw new IOException(TextEditorMessages.getString("DocumentInputStream.error.streamClosed")); //$NON-NLS-1$
		} catch (IndexOutOfBoundsException x) {
			throw new IOException(TextEditorMessages.getFormattedString("DocumentInputStream.error.read", x.getLocalizedMessage())); //$NON-NLS-1$
		}
	}
	
	/*
	 * @see java.io.InputStream#close()
	 */
	public void close() throws IOException {
		synchronized (this) {
			fCharSequence= null;
		}
		releaseDocument();
	}

	/**
	 * Copies the document prior to modification and removes the document listener.
	 */
	private void handleDocumentAboutToBeChanged() {
		IDocument document= fDocument;
		if (fCharSequence == null || document == null)
			return;
		String content= document.get();
		synchronized (this) {
			if (fCharSequence == null)
				return;
			fCharSequence= content;
		}
		releaseDocument();
	}

	/**
	 * Removes the document listener.
	 */
	private synchronized void releaseDocument() {
		if (fDocument != null)
			fDocument.removeDocumentListener(fDocumentListener);
		fDocument= null;
		fDocumentListener= null;
	}
}

Back to the top