Skip to content

Commit 4995dc4

Browse files
committed
Add 2 more alloc methods for IOContext
1 parent cb9f04c commit 4995dc4

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

src/main/java/com/fasterxml/jackson/core/io/IOContext.java

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
* contextual objects that need to be passed by the factory to
1010
* readers and writers are combined under this object. One instance
1111
* is created for each reader and writer.
12+
*<p>
13+
* NOTE: non-final since 2.4, to allow sub-classing.
1214
*/
13-
public final class IOContext
15+
public class IOContext
1416
{
1517
/*
1618
/**********************************************************
@@ -132,41 +134,51 @@ public TextBuffer constructTextBuffer() {
132134
* Note: the method can only be called once during its life cycle.
133135
* This is to protect against accidental sharing.
134136
*/
135-
public byte[] allocReadIOBuffer()
136-
{
137+
public byte[] allocReadIOBuffer() {
137138
_verifyAlloc(_readIOBuffer);
138139
return (_readIOBuffer = _bufferRecycler.allocByteBuffer(BufferRecycler.ByteBufferType.READ_IO_BUFFER));
139140
}
140141

141-
public byte[] allocWriteEncodingBuffer()
142-
{
142+
/**
143+
* @since 2.3.2
144+
*/
145+
public byte[] allocReadIOBuffer(int minSize) {
146+
_verifyAlloc(_readIOBuffer);
147+
return (_readIOBuffer = _bufferRecycler.allocByteBuffer(BufferRecycler.ByteBufferType.READ_IO_BUFFER, minSize));
148+
}
149+
150+
public byte[] allocWriteEncodingBuffer() {
143151
_verifyAlloc(_writeEncodingBuffer);
144152
return (_writeEncodingBuffer = _bufferRecycler.allocByteBuffer(BufferRecycler.ByteBufferType.WRITE_ENCODING_BUFFER));
145153
}
146154

155+
/**
156+
* @since 2.3.2
157+
*/
158+
public byte[] allocWriteEncodingBuffer(int minSize) {
159+
_verifyAlloc(_writeEncodingBuffer);
160+
return (_writeEncodingBuffer = _bufferRecycler.allocByteBuffer(BufferRecycler.ByteBufferType.WRITE_ENCODING_BUFFER, minSize));
161+
}
162+
147163
/**
148164
* @since 2.1
149165
*/
150-
public byte[] allocBase64Buffer()
151-
{
166+
public byte[] allocBase64Buffer() {
152167
_verifyAlloc(_base64Buffer);
153168
return (_base64Buffer = _bufferRecycler.allocByteBuffer(BufferRecycler.ByteBufferType.BASE64_CODEC_BUFFER));
154169
}
155170

156-
public char[] allocTokenBuffer()
157-
{
171+
public char[] allocTokenBuffer() {
158172
_verifyAlloc(_tokenCBuffer);
159173
return (_tokenCBuffer = _bufferRecycler.allocCharBuffer(BufferRecycler.CharBufferType.TOKEN_BUFFER));
160174
}
161175

162-
public char[] allocConcatBuffer()
163-
{
176+
public char[] allocConcatBuffer() {
164177
_verifyAlloc(_concatCBuffer);
165178
return (_concatCBuffer = _bufferRecycler.allocCharBuffer(BufferRecycler.CharBufferType.CONCAT_BUFFER));
166179
}
167180

168-
public char[] allocNameCopyBuffer(int minSize)
169-
{
181+
public char[] allocNameCopyBuffer(int minSize) {
170182
_verifyAlloc(_nameCopyBuffer);
171183
return (_nameCopyBuffer = _bufferRecycler.allocCharBuffer(BufferRecycler.CharBufferType.NAME_COPY_BUFFER, minSize));
172184
}
@@ -175,8 +187,7 @@ public char[] allocNameCopyBuffer(int minSize)
175187
* Method to call when all the processing buffers can be safely
176188
* recycled.
177189
*/
178-
public void releaseReadIOBuffer(byte[] buf)
179-
{
190+
public void releaseReadIOBuffer(byte[] buf) {
180191
if (buf != null) {
181192
/* Let's do sanity checks to ensure once-and-only-once release,
182193
* as well as avoiding trying to release buffers not owned
@@ -187,8 +198,7 @@ public void releaseReadIOBuffer(byte[] buf)
187198
}
188199
}
189200

190-
public void releaseWriteEncodingBuffer(byte[] buf)
191-
{
201+
public void releaseWriteEncodingBuffer(byte[] buf) {
192202
if (buf != null) {
193203
/* Let's do sanity checks to ensure once-and-only-once release,
194204
* as well as avoiding trying to release buffers not owned
@@ -199,36 +209,34 @@ public void releaseWriteEncodingBuffer(byte[] buf)
199209
}
200210
}
201211

202-
public void releaseBase64Buffer(byte[] buf)
203-
{
212+
public void releaseBase64Buffer(byte[] buf) {
204213
if (buf != null) { // sanity checks, release once-and-only-once, must be one owned
205214
_verifyRelease(buf, _base64Buffer);
206215
_base64Buffer = null;
207216
_bufferRecycler.releaseByteBuffer(BufferRecycler.ByteBufferType.BASE64_CODEC_BUFFER, buf);
208217
}
209218
}
210219

211-
public void releaseTokenBuffer(char[] buf)
212-
{
220+
public void releaseTokenBuffer(char[] buf) {
213221
if (buf != null) {
214222
_verifyRelease(buf, _tokenCBuffer);
215223
_tokenCBuffer = null;
216224
_bufferRecycler.releaseCharBuffer(BufferRecycler.CharBufferType.TOKEN_BUFFER, buf);
217225
}
218226
}
219227

220-
public void releaseConcatBuffer(char[] buf)
221-
{
228+
public void releaseConcatBuffer(char[] buf) {
222229
if (buf != null) {
230+
// 14-Jan-2014, tatu: Let's actually allow upgrade of the original buffer.
223231
_verifyRelease(buf, _concatCBuffer);
224232
_concatCBuffer = null;
225233
_bufferRecycler.releaseCharBuffer(BufferRecycler.CharBufferType.CONCAT_BUFFER, buf);
226234
}
227235
}
228236

229-
public void releaseNameCopyBuffer(char[] buf)
230-
{
237+
public void releaseNameCopyBuffer(char[] buf) {
231238
if (buf != null) {
239+
// 14-Jan-2014, tatu: Let's actually allow upgrade of the original buffer.
232240
_verifyRelease(buf, _nameCopyBuffer);
233241
_nameCopyBuffer = null;
234242
_bufferRecycler.releaseCharBuffer(BufferRecycler.CharBufferType.NAME_COPY_BUFFER, buf);
@@ -241,17 +249,17 @@ public void releaseNameCopyBuffer(char[] buf)
241249
/**********************************************************
242250
*/
243251

244-
private final void _verifyAlloc(Object buffer)
245-
{
246-
if (buffer != null) {
247-
throw new IllegalStateException("Trying to call same allocXxx() method second time");
248-
}
252+
protected void _verifyAlloc(Object buffer) {
253+
if (buffer != null) { throw new IllegalStateException("Trying to call same allocXxx() method second time"); }
249254
}
250-
251-
private final void _verifyRelease(Object toRelease, Object src)
252-
{
253-
if (toRelease != src) {
254-
throw new IllegalArgumentException("Trying to release buffer not owned by the context");
255-
}
255+
256+
protected void _verifyRelease(byte[] toRelease, byte[] src) {
257+
if ((toRelease != src) && (toRelease.length <= src.length)) { throw wrongBuf(); }
256258
}
259+
260+
protected void _verifyRelease(char[] toRelease, char[] src) {
261+
if ((toRelease != src) && (toRelease.length <= src.length)) { throw wrongBuf(); }
262+
}
263+
264+
private IllegalArgumentException wrongBuf() { return new IllegalArgumentException("Trying to release buffer not owned by the context"); }
257265
}

0 commit comments

Comments
 (0)