Skip to content

Commit 5039f16

Browse files
committed
Support method-chain syntax to create packer and unpacker with config
1 parent bdb2c2a commit 5039f16

File tree

5 files changed

+139
-48
lines changed

5 files changed

+139
-48
lines changed

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

Lines changed: 122 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,9 @@ public static MessageUnpacker newDefaultUnpacker(byte[] contents, int offset, in
235235
*/
236236
public static class PackerConfig
237237
{
238-
/**
239-
* Use String.getBytes() for converting Java Strings that are smaller than this threshold into UTF8.
240-
* Note that this parameter is subject to change.
241-
*/
242-
public int smallStringOptimizationThreshold = 512;
238+
private int smallStringOptimizationThreshold = 512;
243239

244-
/**
245-
* When the next payload size exceeds this threshold, MessagePacker will call MessageBufferOutput.flush() before
246-
* packing the data.
247-
*/
248-
public int bufferFlushThreshold = 8192;
240+
private int bufferFlushThreshold = 8192;
249241

250242
/**
251243
* Create a packer that outputs the packed data to a given output
@@ -289,42 +281,57 @@ public MessageBufferPacker newBufferPacker()
289281
{
290282
return new MessageBufferPacker(this);
291283
}
284+
285+
/**
286+
* Use String.getBytes() for converting Java Strings that are smaller than this threshold into UTF8.
287+
* Note that this parameter is subject to change.
288+
*/
289+
public PackerConfig setSmallStringOptimizationThreshold(int bytes)
290+
{
291+
this.smallStringOptimizationThreshold = bytes;
292+
return this;
293+
}
294+
295+
public int getSmallStringOptimizationThreshold()
296+
{
297+
return smallStringOptimizationThreshold;
298+
}
299+
300+
/**
301+
* When the next payload size exceeds this threshold, MessagePacker will call MessageBufferOutput.flush() before
302+
* packing the data.
303+
*/
304+
public PackerConfig setBufferFlushThreshold(int bytes)
305+
{
306+
this.bufferFlushThreshold = bytes;
307+
return this;
308+
}
309+
310+
public int getBufferFlushThreshold()
311+
{
312+
return bufferFlushThreshold;
313+
}
292314
}
293315

294316
/**
295317
* MessageUnpacker configuration.
296318
*/
297319
public static class UnpackerConfig
298320
{
299-
/**
300-
* Allow unpackBinaryHeader to read str format family (default:true)
301-
*/
302-
public boolean allowReadingStringAsBinary = true;
321+
private boolean allowReadingStringAsBinary = true;
303322

304-
/**
305-
* Allow unpackRawStringHeader and unpackString to read bin format family (default: true)
306-
*/
307-
public boolean allowReadingBinaryAsString = true;
323+
private boolean allowReadingBinaryAsString = true;
308324

309-
/**
310-
* Action when encountered a malformed input
311-
*/
312-
public CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE;
325+
private CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE;
313326

314-
/**
315-
* Action when an unmappable character is found
316-
*/
317-
public CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE;
327+
private CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE;
318328

319-
/**
320-
* unpackString size limit. (default: Integer.MAX_VALUE)
321-
*/
322-
public int stringSizeLimit = Integer.MAX_VALUE;
329+
private int stringSizeLimit = Integer.MAX_VALUE;
323330

324331
/**
325332
*
326333
*/
327-
public int stringDecoderBufferSize = 8192;
334+
private int stringDecoderBufferSize = 8192;
328335

329336
/**
330337
* Create an unpacker that reads the data from a given input
@@ -380,5 +387,89 @@ public MessageUnpacker newUnpacker(byte[] contents, int offset, int length)
380387
{
381388
return newUnpacker(new ArrayBufferInput(contents, offset, length));
382389
}
390+
391+
/**
392+
* Allow unpackBinaryHeader to read str format family (default:true)
393+
*/
394+
public UnpackerConfig setAllowReadingStringAsBinary(boolean enable)
395+
{
396+
this.allowReadingStringAsBinary = enable;
397+
return this;
398+
}
399+
400+
public boolean getAllowReadingStringAsBinary()
401+
{
402+
return allowReadingStringAsBinary;
403+
}
404+
405+
/**
406+
* Allow unpackString and unpackRawStringHeader and unpackString to read bin format family (default: true)
407+
*/
408+
public UnpackerConfig setAllowReadingBinaryAsString(boolean enable)
409+
{
410+
this.allowReadingBinaryAsString = enable;
411+
return this;
412+
}
413+
414+
public boolean getAllowReadingBinaryAsString()
415+
{
416+
return allowReadingBinaryAsString;
417+
}
418+
419+
/**
420+
* Action when encountered a malformed input (default: REPLACE)
421+
*/
422+
public UnpackerConfig setActionOnMalformedString(CodingErrorAction action)
423+
{
424+
this.actionOnMalformedString = action;
425+
return this;
426+
}
427+
428+
public CodingErrorAction getActionOnMalformedString()
429+
{
430+
return actionOnMalformedString;
431+
}
432+
433+
/**
434+
* Action when an unmappable character is found (default: REPLACE)
435+
*/
436+
public UnpackerConfig setActionOnUnmappableString(CodingErrorAction action)
437+
{
438+
this.actionOnUnmappableString = action;
439+
return this;
440+
}
441+
442+
public CodingErrorAction getActionOnUnmappableString()
443+
{
444+
return actionOnUnmappableString;
445+
}
446+
447+
/**
448+
* unpackString size limit. (default: Integer.MAX_VALUE)
449+
*/
450+
public UnpackerConfig setStringSizeLimit(int bytes)
451+
{
452+
this.stringSizeLimit = bytes;
453+
return this;
454+
}
455+
456+
public int getStringSizeLimit()
457+
{
458+
return stringSizeLimit;
459+
}
460+
461+
/**
462+
*
463+
*/
464+
public UnpackerConfig setStringDecoderBufferSize(int bytes)
465+
{
466+
this.stringDecoderBufferSize = bytes;
467+
return this;
468+
}
469+
470+
public int getStringDecoderBufferSize()
471+
{
472+
return stringDecoderBufferSize;
473+
}
383474
}
384475
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public MessagePacker(MessageBufferOutput out, MessagePack.PackerConfig config)
114114
{
115115
this.out = checkNotNull(out, "MessageBufferOutput is null");
116116
// We must copy the configuration parameters here since the config object is mutable
117-
this.smallStringOptimizationThreshold = config.smallStringOptimizationThreshold;
118-
this.bufferFlushThreshold = config.bufferFlushThreshold;
117+
this.smallStringOptimizationThreshold = config.getSmallStringOptimizationThreshold();
118+
this.bufferFlushThreshold = config.getBufferFlushThreshold();
119119
this.position = 0;
120120
this.totalFlushBytes = 0;
121121
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ public MessageUnpacker(MessageBufferInput in, MessagePack.UnpackerConfig config)
130130
{
131131
this.in = checkNotNull(in, "MessageBufferInput is null");
132132
// We need to copy the configuration parameters since the config object is mutable
133-
this.allowReadingStringAsBinary = config.allowReadingStringAsBinary;
134-
this.allowReadingBinaryAsString = config.allowReadingBinaryAsString;
135-
this.actionOnMalformedString = config.actionOnMalformedString;
136-
this.actionOnUnmappableString = config.actionOnUnmappableString;
137-
this.stringSizeLimit = config.stringSizeLimit;
138-
this.stringDecoderBufferSize = config.stringDecoderBufferSize;
133+
this.allowReadingStringAsBinary = config.getAllowReadingStringAsBinary();
134+
this.allowReadingBinaryAsString = config.getAllowReadingBinaryAsString();
135+
this.actionOnMalformedString = config.getActionOnMalformedString();
136+
this.actionOnUnmappableString = config.getActionOnUnmappableString();
137+
this.stringSizeLimit = config.getStringSizeLimit();
138+
this.stringDecoderBufferSize = config.getStringDecoderBufferSize();
139139
}
140140

141141
/**

msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,19 @@ public static void configuration()
246246
throws IOException
247247
{
248248
ByteArrayOutputStream out = new ByteArrayOutputStream();
249-
PackerConfig packerConfig = new PackerConfig();
250-
packerConfig.smallStringOptimizationThreshold = 256; // String
251-
MessagePacker packer = packerConfig.newPacker(out);
249+
MessagePacker packer = new PackerConfig()
250+
.setSmallStringOptimizationThreshold(256) // String
251+
.newPacker(out);
252252

253253
packer.packInt(10);
254254
packer.packBoolean(true);
255255
packer.close();
256256

257257
// Unpack data
258-
UnpackerConfig unpackerConfig = new UnpackerConfig();
259-
unpackerConfig.stringDecoderBufferSize = 16 * 1024; // If your data contains many large strings (the default is 8k)
260-
261258
byte[] packedData = out.toByteArray();
262-
MessageUnpacker unpacker = unpackerConfig.newUnpacker(packedData);
259+
MessageUnpacker unpacker = new UnpackerConfig()
260+
.setStringDecoderBufferSize(16 * 1024) // If your data contains many large strings (the default is 8k)
261+
.newUnpacker(packedData);
263262
int i = unpacker.unpackInt(); // 10
264263
boolean b = unpacker.unpackBoolean(); // true
265264
unpacker.close();

msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,9 @@ class MessagePackTest extends MessagePackSpec {
337337

338338
// Report error on unmappable character
339339
val unpackerConfig = new UnpackerConfig()
340-
unpackerConfig.actionOnMalformedString = CodingErrorAction.REPORT
341-
unpackerConfig.actionOnUnmappableString = CodingErrorAction.REPORT
340+
unpackerConfig
341+
.setActionOnMalformedString(CodingErrorAction.REPORT)
342+
.setActionOnUnmappableString(CodingErrorAction.REPORT)
342343

343344
for (bytes <- Seq(unmappable)) {
344345
When("unpacking")

0 commit comments

Comments
 (0)