forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOContext.java
More file actions
240 lines (211 loc) · 7.88 KB
/
IOContext.java
File metadata and controls
240 lines (211 loc) · 7.88 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.codehaus.jackson.io;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.util.BufferRecycler;
import org.codehaus.jackson.util.TextBuffer;
/**
* To limit number of configuration and state objects to pass, all
* contextual objects that need to be passed by the factory to
* readers and writers are combined under this object. One instance
* is created for each reader and writer.
*/
public final class IOContext
{
/*
//////////////////////////////////////////////////////
// Configuration
//////////////////////////////////////////////////////
*/
/**
* Reference to the source object, which can be used for displaying
* location information
*/
final Object _sourceRef;
/**
* Encoding used by the underlying stream, if known.
*/
protected JsonEncoding _encoding;
/**
* Flag that indicates whether underlying input/output source/target
* object is fully managed by the owner of this context (parser or
* generator). If true, it is, and is to be closed by parser/generator;
* if false, calling application has to do closing (unless auto-closing
* feature is enabled for the parser/generator in question; in which
* case it acts like the owner).
*/
protected final boolean _managedResource;
/*
//////////////////////////////////////////////////////
// Buffer handling, recycling
//////////////////////////////////////////////////////
*/
/**
* Recycler used for actual allocation/deallocation/reuse
*/
final BufferRecycler _bufferRecycler;
/**
* Reference to the allocated I/O buffer for low-level input reading,
* if any allocated.
*/
protected byte[] _readIOBuffer = null;
/**
* Reference to the allocated I/O buffer used for low-level
* encoding-related buffering.
*/
protected byte[] _writeEncodingBuffer = null;
/**
* Reference to the buffer allocated for tokenization purposes,
* in which character input is read, and from which it can be
* further returned.
*/
protected char[] _tokenCBuffer = null;
/**
* Reference to the buffer allocated for buffering it for
* output, before being encoded: generally this means concatenating
* output, then encoding when buffer fills up.
*/
protected char[] _concatCBuffer = null;
/**
* Reference temporary buffer Parser instances need if calling
* app decides it wants to access name via 'getTextCharacters' method.
* Regular text buffer can not be used as it may contain textual
* representation of the value token.
*/
protected char[] _nameCopyBuffer = null;
/*
//////////////////////////////////////////////////////
// Life-cycle
//////////////////////////////////////////////////////
*/
public IOContext(BufferRecycler br, Object sourceRef, boolean managedResource)
{
_bufferRecycler = br;
_sourceRef = sourceRef;
_managedResource = managedResource;
}
public void setEncoding(JsonEncoding enc)
{
_encoding = enc;
}
/*
//////////////////////////////////////////////////////
// Public API, accessors
//////////////////////////////////////////////////////
*/
public Object getSourceReference() { return _sourceRef; }
public JsonEncoding getEncoding() { return _encoding; }
public boolean isResourceManaged() { return _managedResource; }
/*
//////////////////////////////////////////////////////
// Public API, buffer management
//////////////////////////////////////////////////////
*/
public TextBuffer constructTextBuffer()
{
return new TextBuffer(_bufferRecycler);
}
/**
*<p>
* Note: the method can only be called once during its life cycle.
* This is to protect against accidental sharing.
*/
public byte[] allocReadIOBuffer()
{
if (_readIOBuffer != null) {
throw new IllegalStateException("Trying to call allocReadIOBuffer() second time");
}
_readIOBuffer = _bufferRecycler.allocByteBuffer(BufferRecycler.ByteBufferType.READ_IO_BUFFER);
return _readIOBuffer;
}
public byte[] allocWriteEncodingBuffer()
{
if (_writeEncodingBuffer != null) {
throw new IllegalStateException("Trying to call allocWriteEncodingBuffer() second time");
}
_writeEncodingBuffer = _bufferRecycler.allocByteBuffer(BufferRecycler.ByteBufferType.WRITE_ENCODING_BUFFER);
return _writeEncodingBuffer;
}
public char[] allocTokenBuffer()
{
if (_tokenCBuffer != null) {
throw new IllegalStateException("Trying to call allocTokenBuffer() second time");
}
_tokenCBuffer = _bufferRecycler.allocCharBuffer(BufferRecycler.CharBufferType.TOKEN_BUFFER);
return _tokenCBuffer;
}
public char[] allocConcatBuffer()
{
if (_concatCBuffer != null) {
throw new IllegalStateException("Trying to call allocConcatBuffer() second time");
}
_concatCBuffer = _bufferRecycler.allocCharBuffer(BufferRecycler.CharBufferType.CONCAT_BUFFER);
return _concatCBuffer;
}
public char[] allocNameCopyBuffer(int minSize)
{
if (_nameCopyBuffer != null) {
throw new IllegalStateException("Trying to call allocNameCopyBuffer() second time");
}
_nameCopyBuffer = _bufferRecycler.allocCharBuffer(BufferRecycler.CharBufferType.NAME_COPY_BUFFER, minSize);
return _nameCopyBuffer;
}
/**
* Method to call when all the processing buffers can be safely
* recycled.
*/
public void releaseReadIOBuffer(byte[] buf)
{
if (buf != null) {
/* Let's do sanity checks to ensure once-and-only-once release,
* as well as avoiding trying to release buffers not owned
*/
if (buf != _readIOBuffer) {
throw new IllegalArgumentException("Trying to release buffer not owned by the context");
}
_readIOBuffer = null;
_bufferRecycler.releaseByteBuffer(BufferRecycler.ByteBufferType.READ_IO_BUFFER, buf);
}
}
public void releaseWriteEncodingBuffer(byte[] buf)
{
if (buf != null) {
/* Let's do sanity checks to ensure once-and-only-once release,
* as well as avoiding trying to release buffers not owned
*/
if (buf != _writeEncodingBuffer) {
throw new IllegalArgumentException("Trying to release buffer not owned by the context");
}
_writeEncodingBuffer = null;
_bufferRecycler.releaseByteBuffer(BufferRecycler.ByteBufferType.WRITE_ENCODING_BUFFER, buf);
}
}
public void releaseTokenBuffer(char[] buf)
{
if (buf != null) {
if (buf != _tokenCBuffer) {
throw new IllegalArgumentException("Trying to release buffer not owned by the context");
}
_tokenCBuffer = null;
_bufferRecycler.releaseCharBuffer(BufferRecycler.CharBufferType.TOKEN_BUFFER, buf);
}
}
public void releaseConcatBuffer(char[] buf)
{
if (buf != null) {
if (buf != _concatCBuffer) {
throw new IllegalArgumentException("Trying to release buffer not owned by the context");
}
_concatCBuffer = null;
_bufferRecycler.releaseCharBuffer(BufferRecycler.CharBufferType.CONCAT_BUFFER, buf);
}
}
public void releaseNameCopyBuffer(char[] buf)
{
if (buf != null) {
if (buf != _nameCopyBuffer) {
throw new IllegalArgumentException("Trying to release buffer not owned by the context");
}
_nameCopyBuffer = null;
_bufferRecycler.releaseCharBuffer(BufferRecycler.CharBufferType.NAME_COPY_BUFFER, buf);
}
}
}