Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0bc54f2af6cc4ce8a32801a5be697c15485b85a2 (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
/*******************************************************************************
 * Copyright (c) 2017 Andreas Loth and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Andreas Loth - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.console;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;


/**
 * @since 3.7
 */
public class StreamDecoder {

	static private final int BUFFER_SIZE = 4096;

	private final CharsetDecoder decoder;
	private final ByteBuffer inputBuffer;
	private final CharBuffer outputBuffer;
	private boolean finished;

	public StreamDecoder(Charset charset) {
		this.decoder = charset.newDecoder();
		this.decoder.onMalformedInput(CodingErrorAction.REPLACE);
		this.decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
		this.inputBuffer = ByteBuffer.allocate(StreamDecoder.BUFFER_SIZE);
		this.inputBuffer.flip();
		this.outputBuffer = CharBuffer.allocate(StreamDecoder.BUFFER_SIZE);
		this.finished = false;
	}

	private void consume(StringBuilder consumer) {
		this.outputBuffer.flip();
		consumer.append(this.outputBuffer);
		this.outputBuffer.clear();
	}

	private void internalDecode(StringBuilder consumer, byte[] buffer, int offset, int length) {
		assert (offset >= 0);
		assert (length >= 0);
		int position = offset;
		int end = offset + length;
		assert (end <= buffer.length);
		boolean finishedReading = false;
		do {
			CoderResult result = this.decoder.decode(this.inputBuffer, this.outputBuffer, false);
			if (result.isOverflow()) {
				this.consume(consumer);
			} else if (result.isUnderflow()) {
				this.inputBuffer.compact();
				int remaining = this.inputBuffer.remaining();
				assert (remaining > 0);
				int read = Math.min(remaining, end - position);
				if (read > 0) {
					this.inputBuffer.put(buffer, position, read);
					position += read;
				} else {
					finishedReading = true;
				}
				this.inputBuffer.flip();
			} else {
				assert false;
			}
		} while (!finishedReading);
	}

	public void decode(StringBuilder consumer, byte[] buffer, int offset, int length) {
		this.internalDecode(consumer, buffer, offset, length);
		this.consume(consumer);
	}

	public void finish(StringBuilder consumer) {
		if (this.finished) {
			return;
		}
		this.finished = true;
		CoderResult result;
		result = this.decoder.decode(this.inputBuffer, this.outputBuffer, true);
		assert (result.isOverflow() || result.isUnderflow());
		do {
			result = this.decoder.flush(this.outputBuffer);
			if (result.isOverflow()) {
				this.consume(consumer);
			} else {
				assert result.isUnderflow();
			}
		} while (!result.isUnderflow());
		this.consume(consumer);
	}

}

Back to the top