Skip to content

Commit e34df59

Browse files
committed
Make buffer size of StreamBuffer{Input,Output} and ChannelBuffer{Input,Output}
1 parent 5039f16 commit e34df59

1 file changed

Lines changed: 41 additions & 7 deletions

File tree

msgpack-core/src/main/java/org/msgpack/core/MessagePack.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ public static class PackerConfig
239239

240240
private int bufferFlushThreshold = 8192;
241241

242+
private int bufferSize = 8192;
243+
242244
/**
243245
* Create a packer that outputs the packed data to a given output
244246
*
@@ -258,7 +260,7 @@ public MessagePacker newPacker(MessageBufferOutput out)
258260
*/
259261
public MessagePacker newPacker(OutputStream out)
260262
{
261-
return newPacker(new OutputStreamBufferOutput(out));
263+
return newPacker(new OutputStreamBufferOutput(out, bufferSize));
262264
}
263265

264266
/**
@@ -269,7 +271,7 @@ public MessagePacker newPacker(OutputStream out)
269271
*/
270272
public MessagePacker newPacker(WritableByteChannel channel)
271273
{
272-
return newPacker(new ChannelBufferOutput(channel));
274+
return newPacker(new ChannelBufferOutput(channel, bufferSize));
273275
}
274276

275277
/**
@@ -299,7 +301,7 @@ public int getSmallStringOptimizationThreshold()
299301

300302
/**
301303
* When the next payload size exceeds this threshold, MessagePacker will call MessageBufferOutput.flush() before
302-
* packing the data.
304+
* packing the data (default: 8192).
303305
*/
304306
public PackerConfig setBufferFlushThreshold(int bytes)
305307
{
@@ -311,6 +313,21 @@ public int getBufferFlushThreshold()
311313
{
312314
return bufferFlushThreshold;
313315
}
316+
317+
/**
318+
* When a packer is created with newPacker(OutputStream) or newPacker(WritableByteChannel), the stream will be
319+
* buffered with this size of buffer (default: 8192).
320+
*/
321+
public PackerConfig setBufferSize(int bytes)
322+
{
323+
this.bufferSize = bytes;
324+
return this;
325+
}
326+
327+
public int getBufferSize()
328+
{
329+
return bufferSize;
330+
}
314331
}
315332

316333
/**
@@ -328,6 +345,8 @@ public static class UnpackerConfig
328345

329346
private int stringSizeLimit = Integer.MAX_VALUE;
330347

348+
private int bufferSize = 8192;
349+
331350
/**
332351
*
333352
*/
@@ -352,7 +371,7 @@ public MessageUnpacker newUnpacker(MessageBufferInput in)
352371
*/
353372
public MessageUnpacker newUnpacker(InputStream in)
354373
{
355-
return newUnpacker(new InputStreamBufferInput(in));
374+
return newUnpacker(new InputStreamBufferInput(in, bufferSize));
356375
}
357376

358377
/**
@@ -363,7 +382,7 @@ public MessageUnpacker newUnpacker(InputStream in)
363382
*/
364383
public MessageUnpacker newUnpacker(ReadableByteChannel channel)
365384
{
366-
return newUnpacker(new ChannelBufferInput(channel));
385+
return newUnpacker(new ChannelBufferInput(channel, bufferSize));
367386
}
368387

369388
/**
@@ -389,7 +408,7 @@ public MessageUnpacker newUnpacker(byte[] contents, int offset, int length)
389408
}
390409

391410
/**
392-
* Allow unpackBinaryHeader to read str format family (default:true)
411+
* Allow unpackBinaryHeader to read str format family (default: true)
393412
*/
394413
public UnpackerConfig setAllowReadingStringAsBinary(boolean enable)
395414
{
@@ -445,7 +464,7 @@ public CodingErrorAction getActionOnUnmappableString()
445464
}
446465

447466
/**
448-
* unpackString size limit. (default: Integer.MAX_VALUE)
467+
* unpackString size limit (default: Integer.MAX_VALUE).
449468
*/
450469
public UnpackerConfig setStringSizeLimit(int bytes)
451470
{
@@ -471,5 +490,20 @@ public int getStringDecoderBufferSize()
471490
{
472491
return stringDecoderBufferSize;
473492
}
493+
494+
/**
495+
* When a packer is created with newUnpacker(OutputStream) or newUnpacker(WritableByteChannel), the stream will be
496+
* buffered with this size of buffer (default: 8192).
497+
*/
498+
public UnpackerConfig setBufferSize(int bytes)
499+
{
500+
this.bufferSize = bytes;
501+
return this;
502+
}
503+
504+
public int getBufferSize()
505+
{
506+
return bufferSize;
507+
}
474508
}
475509
}

0 commit comments

Comments
 (0)