Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 081f63060951063499c34e27acee1bd809297ffc (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.core.internal.text;

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

public class CharSequenceReader extends Reader {
	private int fCurrentPosition;
	private int fMaximumReadOffset;

	private CharSequence fOriginalSource;

	/**
	 *  
	 */
	CharSequenceReader() {
		super();
	}


	public CharSequenceReader(CharSequence originalSource, int offset, int length) {
		// ISSUE: should we "fail fast" if requested length is more than there
		// is?
		fOriginalSource = originalSource;
		int startOffset = offset;
		int maxRequestedOffset = startOffset + length;
		int maxPossibleOffset = 0 + originalSource.length();
		fMaximumReadOffset = Math.min(maxRequestedOffset, maxPossibleOffset);

		fCurrentPosition = startOffset;

	}

	/**
	 * @param lock
	 */
	CharSequenceReader(Object lock) {
		super(lock);
		// for thread safety, may need to add back locking mechanism
		// in our custom constructor. This constructor left here just
		// for a reminder.
	}

	public void close() throws IOException {
		// nothing to do when we close
		// (may be to eventually "unlock" or null out some varibles
		// just for hygene.
		// or, perhaps if already closed once throw IOException? for
		// consistency?
	}

	/**
	 * @return Returns the originalSource.
	 * @deprecated - only temporarily public, should be 'default' eventually
	 *             or go away altogether.
	 */
	public CharSequence getOriginalSource() {
		return fOriginalSource;
	}

	public int read() {
		int result = -1;
		if (fCurrentPosition < fMaximumReadOffset) {
			result = fOriginalSource.charAt(fCurrentPosition++);
		}
		return result;
	}

	/**
	 * Read characters into a portion of an array. This method will block
	 * until some input is available, an I/O error occurs, or the end of the
	 * stream is reached.
	 * 
	 * @param cbuf
	 *            Destination buffer
	 * @param off
	 *            Offset at which to start storing characters
	 * @param len
	 *            Maximum number of characters to read
	 * 
	 * @return The number of characters read, or -1 if the end of the stream
	 *         has been reached
	 * 
	 * @exception IOException
	 *                If an I/O error occurs
	 */

	public int read(char[] cbuf, int off, int len) throws IOException {
		int charsToRead = -1;
		// if already over max, just return -1
		// remember, currentPosition is what is getting ready to be read
		// (that is, its already been incremented in read()).
		if (fCurrentPosition < fMaximumReadOffset) {


			int buffMaxToRead = cbuf.length - off;
			int minRequested = Math.min(buffMaxToRead, len);
			int lengthRemaining = fMaximumReadOffset - fCurrentPosition;
			charsToRead = Math.min(minRequested, lengthRemaining);


			CharSequence seq = fOriginalSource.subSequence(fCurrentPosition, fCurrentPosition + charsToRead);
			// for now, hard assumption that original is a String since source
			// is assumed to be document, or text store
			String seqString = (String) seq;
			seqString.getChars(0, seqString.length(), cbuf, off);



			fCurrentPosition = fCurrentPosition + charsToRead;


		}
		return charsToRead;
	}
}

Back to the top