Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 97457dae8de7f37de0ce51f82ec1fb1a22ea9008 (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
/*****************************************************************************
 * Copyright (c) 2013, 2017 CEA LIST.
 *
 * 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.ui.customization.properties.storage;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

import org.eclipse.emf.cdo.common.lob.CDOClob;


/**
 * An input stream for reading {@link CDOClob}s.
 */
public class ClobInputStream extends InputStream {

	private static final int BUFFER_SIZE_IN_CHARS = 2048;

	private static final int MASK = 0xFF;

	private static final int EOF = -1;

	private final Charset charset;

	private Reader reader;

	private CharBuffer chars = CharBuffer.allocate(BUFFER_SIZE_IN_CHARS);

	private ByteBuffer buffer;

	public ClobInputStream(CDOClob clob, String encoding) throws IOException {
		this.reader = clob.getContents();
		this.charset = Charset.forName((encoding == null) ? "UTF-8" : encoding); //$NON-NLS-1$
	}

	@Override
	public void close() throws IOException {
		if (reader != null) {
			reader.close();
			reader = null;
			buffer = null;
			chars = null;
		}
	}

	@Override
	public int read() throws IOException {
		checkClosed();
		checkBuffer();

		if (buffer != null) {
			return buffer.get() & MASK;
		}

		return EOF;
	}

	private void checkBuffer() throws IOException {
		if ((buffer == null) || !buffer.hasRemaining()) {
			buffer = null;
			createBuffer();
		}
	}

	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		checkClosed();

		if (len == 0) {
			return 0;
		}

		checkBuffer();

		if (buffer == null) {
			return EOF;
		}

		int count = 0;
		do {
			checkBuffer();

			if (buffer == null) {
				// no more to read
				break;
			}

			int toRead = Math.min(buffer.remaining(), len - count);
			buffer.get(b, off + count, toRead);
			count = count + toRead;
		} while (count < len);

		return count;
	}

	@Override
	public int available() throws IOException {
		checkClosed();
		checkBuffer();

		if (buffer != null) {
			return buffer.remaining();
		}

		return 0;
	}

	protected final void checkClosed() throws IOException {
		if (reader == null) {
			throw new IOException("CLOB reader is closed"); //$NON-NLS-1$
		}
	}

	private void createBuffer() throws IOException {
		checkClosed();

		chars.rewind();
		int count = reader.read(chars);
		if (count > 0) {
			chars.flip();
			buffer = charset.encode(chars);
		}
	}
}

Back to the top