View | Details | Raw Unified | Return to bug 149028 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/model/EncodingTests.java (+45 lines)
Lines 12-17 Link Here
12
12
13
import java.io.File;
13
import java.io.File;
14
import java.io.IOException;
14
import java.io.IOException;
15
import java.io.InputStream;
15
import java.io.UnsupportedEncodingException;
16
import java.io.UnsupportedEncodingException;
16
17
17
import junit.framework.Test;
18
import junit.framework.Test;
Lines 672-677 Link Here
672
		}
673
		}
673
	}
674
	}
674
675
676
	/*
677
	 * Ensures that an encoding that a file using an encoding producing more charaters than the file size can
678
	 * be correctly read.
679
	 * (regression test for bug 149028 Limiting number of characters to read with the file size is invalid.)
680
	 */
681
	public void test034() throws CoreException, IOException {
682
		try {
683
			// Create file
684
			IFile file = createFile("/Encoding/Test34.txt", "acegikm");
685
			
686
			// Read file using a transformation where a character is read and the next alphabetical character is
687
			// automaticaly added
688
			final InputStream fileStream = file.getContents();
689
			try {
690
				InputStream in = new InputStream() {
691
					int current = -1;
692
					public int read() throws IOException {
693
						int result;
694
						if (current != -1) {
695
							result = current;
696
							current = -1;
697
						} else {
698
							result = fileStream.read();
699
							if (result == -1)
700
								return -1;
701
							current = result + 1;
702
						}
703
						return result;
704
					}
705
				};
706
				char[] result = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(in, (int) file.getLocation().toFile().length(), "UTF-8");
707
				assertSourceEquals(
708
					"Unexpected source",
709
					"abcdefghijklmn",
710
					new String(result)					
711
				);
712
			} finally {
713
				fileStream.close();
714
			}
715
		} finally {
716
			deleteFile("Encoding/Test34.txt");
717
		}
718
	}	
719
	
675
	/**
720
	/**
676
	 * Bug 66898: refactor-rename: encoding is not preserved
721
	 * Bug 66898: refactor-rename: encoding is not preserved
677
	 * @see "http://bugs.eclipse.org/bugs/show_bug.cgi?id=66898"
722
	 * @see "http://bugs.eclipse.org/bugs/show_bug.cgi?id=66898"
(-)compiler/org/eclipse/jdt/internal/compiler/util/Util.java (-64 / +44 lines)
Lines 180-187 Link Here
180
	*/
180
	*/
181
	/**
181
	/**
182
	 * Returns the given input stream's contents as a character array.
182
	 * Returns the given input stream's contents as a character array.
183
	 * If a length is specified (ie. if length != -1), only length chars
183
	 * If a length is specified (ie. if length != -1), this represents the number of bytes in the stream.
184
	 * are returned. Otherwise all chars in the stream are returned.
185
	 * Note this doesn't close the stream.
184
	 * Note this doesn't close the stream.
186
	 * @throws IOException if a problem occured reading the stream.
185
	 * @throws IOException if a problem occured reading the stream.
187
	 */
186
	 */
Lines 197-270 Link Here
197
			reader =  new InputStreamReader(stream);
196
			reader =  new InputStreamReader(stream);
198
		}
197
		}
199
		char[] contents;
198
		char[] contents;
199
		int totalRead = 0;
200
		if (length == -1) {
200
		if (length == -1) {
201
			contents = CharOperation.NO_CHAR;
201
			contents = CharOperation.NO_CHAR;
202
			int contentsLength = 0;
203
			int amountRead = -1;
204
			do {
205
				int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE);  // read at least 8K
206
207
				// resize contents if needed
208
				if (contentsLength + amountRequested > contents.length) {
209
					System.arraycopy(
210
						contents,
211
						0,
212
						contents = new char[contentsLength + amountRequested],
213
						0,
214
						contentsLength);
215
				}
216
217
				// read as many chars as possible
218
				amountRead = reader.read(contents, contentsLength, amountRequested);
219
220
				if (amountRead > 0) {
221
					// remember length of contents
222
					contentsLength += amountRead;
223
				}
224
			} while (amountRead != -1);
225
226
			// Do not keep first character for UTF-8 BOM encoding
227
			int start = 0;
228
			if (contentsLength > 0 && UTF_8.equals(encoding)) {
229
				if (contents[0] == 0xFEFF) { // if BOM char then skip
230
					contentsLength--;
231
					start = 1;
232
				}
233
			}
234
			// resize contents if necessary
235
			if (contentsLength < contents.length) {
236
				System.arraycopy(
237
					contents,
238
					start,
239
					contents = new char[contentsLength],
240
					0,
241
					contentsLength);
242
			}
243
		} else {
202
		} else {
203
			// length is a good guess when the encoding produces less or the same amount of characters than the file length
244
			contents = new char[length];
204
			contents = new char[length];
245
			int len = 0;
205
			do {
246
			int readSize = 0;
206
				int amountRead = reader.read(contents, totalRead, length - totalRead);
247
			while ((readSize != -1) && (len != length)) {
207
				if (amountRead < 0)
248
				// See PR 1FMS89U
208
					break;
249
				// We record first the read size. In this case len is the actual read size.
209
				totalRead += amountRead;
250
				len += readSize;
210
			} while (totalRead != length);
251
				readSize = reader.read(contents, len, length - len);
211
		}
252
			}
212
		
253
			// Do not keep first character for UTF-8 BOM encoding
213
		// read remaining characters
254
			int start = 0;
214
		while (true) {
255
			if (length > 0 && UTF_8.equals(encoding)) {
215
			// avoid creating a char[] if there is no more char to read
256
				if (contents[0] == 0xFEFF) { // if BOM char then skip
216
			int current = reader.read(); 
257
					len--;
217
			if (current < 0)
258
					start = 1;
218
				break;
259
				}
219
			
220
			int amountRequested = Math.max(stream.available(), DEFAULT_READING_SIZE);  // read at least 8K
221
222
			// resize contents if needed
223
			if (totalRead + 1 + amountRequested > contents.length)
224
				System.arraycopy(contents, 	0, 	contents = new char[totalRead + 1 + amountRequested], 0, totalRead);
225
			contents[totalRead++] = (char) current;
226
227
			// read as many chars as possible
228
			int amountRead = reader.read(contents, totalRead, amountRequested);
229
			if (amountRead < 0)
230
				break;
231
			totalRead += amountRead;
232
		}
233
234
		// Do not keep first character for UTF-8 BOM encoding
235
		int start = 0;
236
		if (totalRead > 0 && UTF_8.equals(encoding)) {
237
			if (contents[0] == 0xFEFF) { // if BOM char then skip
238
				totalRead--;
239
				start = 1;
260
			}
240
			}
261
			// See PR 1FMS89U
262
			// Now we need to resize in case the default encoding used more than one byte for each
263
			// character
264
			if (len != length)
265
				System.arraycopy(contents, start, (contents = new char[len]), 0, len);
266
		}
241
		}
267
242
		
243
		// resize contents if necessary
244
		if (totalRead < contents.length) {
245
			System.arraycopy(contents, start, contents = new char[totalRead], 	0, 	totalRead);
246
		}
247
		
268
		return contents;
248
		return contents;
269
	}
249
	}
270
	
250
	

Return to bug 149028