View | Details | Raw Unified | Return to bug 159516
Collapse All | Expand All

(-)src/org/eclipse/core/internal/filebuffers/ResourceTextFileBuffer.java (-11 / +83 lines)
Lines 10-26 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.core.internal.filebuffers;
11
package org.eclipse.core.internal.filebuffers;
12
12
13
import java.io.BufferedReader;
13
import java.io.BufferedInputStream;
14
import java.io.ByteArrayInputStream;
14
import java.io.ByteArrayInputStream;
15
import java.io.IOException;
15
import java.io.IOException;
16
import java.io.InputStream;
16
import java.io.InputStream;
17
import java.io.InputStreamReader;
18
import java.io.Reader;
17
import java.io.Reader;
19
import java.io.SequenceInputStream;
18
import java.io.SequenceInputStream;
20
import java.nio.ByteBuffer;
19
import java.nio.ByteBuffer;
21
import java.nio.CharBuffer;
20
import java.nio.CharBuffer;
22
import java.nio.charset.CharacterCodingException;
21
import java.nio.charset.CharacterCodingException;
23
import java.nio.charset.Charset;
22
import java.nio.charset.Charset;
23
import java.nio.charset.CharsetDecoder;
24
import java.nio.charset.CharsetEncoder;
24
import java.nio.charset.CharsetEncoder;
25
import java.nio.charset.CodingErrorAction;
25
import java.nio.charset.CodingErrorAction;
26
import java.nio.charset.IllegalCharsetNameException;
26
import java.nio.charset.IllegalCharsetNameException;
Lines 512-518 Link Here
512
	 */
512
	 */
513
	private void setDocumentContent(IDocument document, IFile file, String encoding) throws CoreException {
513
	private void setDocumentContent(IDocument document, IFile file, String encoding) throws CoreException {
514
		InputStream contentStream= file.getContents();
514
		InputStream contentStream= file.getContents();
515
		Reader in= null;
515
		InputStream in= null;
516
		try {
516
		try {
517
517
518
			if (encoding == null)
518
			if (encoding == null)
Lines 533-546 Link Here
533
				} while (n < IContentDescription.BOM_UTF_8.length);
533
				} while (n < IContentDescription.BOM_UTF_8.length);
534
			}
534
			}
535
535
536
			in= new BufferedReader(new InputStreamReader(contentStream, encoding), BUFFER_SIZE);
536
			StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
537
			StringBuffer buffer= new StringBuffer(BUFFER_SIZE);
537
			in = new BufferedInputStream(contentStream);
538
			char[] readBuffer= new char[READER_CHUNK_SIZE];
538
			buffer.append(getInputStreamAsCharArray(in, -1, encoding));
539
			int n= in.read(readBuffer);
540
			while (n > 0) {
541
				buffer.append(readBuffer, 0, n);
542
				n= in.read(readBuffer);
543
			}
544
539
545
			if (document instanceof IDocumentExtension4)
540
			if (document instanceof IDocumentExtension4)
546
				((IDocumentExtension4)document).set(buffer.toString(), fFile.getModificationStamp());
541
				((IDocumentExtension4)document).set(buffer.toString(), fFile.getModificationStamp());
Lines 561-564 Link Here
561
			}
556
			}
562
		}
557
		}
563
	}
558
	}
559
	
560
	public char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding) throws IOException {
561
		Charset charset = null;
562
		try {
563
			charset = Charset.forName(encoding);
564
		} catch (IllegalCharsetNameException e) {
565
			// ignore
566
		} catch(UnsupportedCharsetException e) {
567
			// ignore
568
		}
569
		if (charset == null) {
570
			String defaultEncoding = System.getProperty("file.encoding"); //$NON-NLS-1$
571
			if (defaultEncoding == null) {
572
				defaultEncoding = "UTF-8"; //$NON-NLS-1$
573
			}
574
			charset = Charset.forName(defaultEncoding);
575
		}		
576
		CharsetDecoder charsetDecoder = charset.newDecoder();
577
		charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
578
		byte[] contents = getInputStreamAsByteArray(stream, length);
579
		ByteBuffer byteBuffer = ByteBuffer.allocate(contents.length);
580
		byteBuffer.put(contents);
581
		byteBuffer.flip();
582
		return charsetDecoder.decode(byteBuffer).array();
583
	}
584
	
585
	public byte[] getInputStreamAsByteArray(InputStream stream, int length) throws IOException {
586
		byte[] contents;
587
		if (length == -1) {
588
			contents = new byte[0];
589
			int contentsLength = 0;
590
			int amountRead = -1;
591
			do {
592
				int amountRequested = Math.max(stream.available(), READER_CHUNK_SIZE);  // read at least 8K
593
594
				// resize contents if needed
595
				if (contentsLength + amountRequested > contents.length) {
596
					System.arraycopy(
597
							contents,
598
							0,
599
							contents = new byte[contentsLength + amountRequested],
600
							0,
601
							contentsLength);
602
				}
603
604
				// read as many bytes as possible
605
				amountRead = stream.read(contents, contentsLength, amountRequested);
606
607
				if (amountRead > 0) {
608
					// remember length of contents
609
					contentsLength += amountRead;
610
				}
611
			} while (amountRead != -1); 
612
613
			// resize contents if necessary
614
			if (contentsLength < contents.length) {
615
				System.arraycopy(
616
						contents,
617
						0,
618
						contents = new byte[contentsLength],
619
						0,
620
						contentsLength);
621
			}
622
		} else {
623
			contents = new byte[length];
624
			int len = 0;
625
			int readSize = 0;
626
			while ((readSize != -1) && (len != length)) {
627
				// See PR 1FMS89U
628
				// We record first the read size. In this case len is the actual read size.
629
				len += readSize;
630
				readSize = stream.read(contents, len, length - len);
631
			}
632
		}
633
634
		return contents;
635
	}
564
}
636
}

Return to bug 159516