Skip to content

Commit 65fd3cd

Browse files
committed
Merge pull request msgpack#294 from xerial/simplified-config
Simplify the config object
2 parents 5e989d9 + a839b88 commit 65fd3cd

File tree

3 files changed

+52
-98
lines changed

3 files changed

+52
-98
lines changed

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

Lines changed: 33 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,41 @@ public class MessagePack
4242
*/
4343
public static class Config
4444
{
45-
private final boolean readStringAsBinary;
46-
private final boolean readBinaryAsString;
47-
private final CodingErrorAction onMalFormedInput;
48-
private final CodingErrorAction onUnmappableCharacter;
49-
private final int maxUnpackStringSize;
50-
private final int stringEncoderBufferSize;
51-
private final int stringDecoderBufferSize;
52-
private final int packerBufferSize;
53-
private final int packerSmallStringOptimizationThreshold; // This parameter is subject to change
54-
private final int packerRawDataCopyingThreshold;
45+
/**
46+
* allow unpackBinaryHeader to read str format family (default:true)
47+
*/
48+
public final boolean readStringAsBinary;
49+
/**
50+
* allow unpackRawStringHeader and unpackString to read bin format family (default: true)
51+
*/
52+
public final boolean readBinaryAsString;
53+
/**
54+
* Action when encountered a malformed input
55+
*/
56+
public final CodingErrorAction actionOnMalFormedInput;
57+
/**
58+
* Action when an unmappable character is found
59+
*/
60+
public final CodingErrorAction actionOnUnmappableCharacter;
61+
/**
62+
* unpackString size limit. (default: Integer.MAX_VALUE)
63+
*/
64+
public final int maxUnpackStringSize;
65+
public final int stringEncoderBufferSize;
66+
public final int stringDecoderBufferSize;
67+
public final int packerBufferSize;
68+
public final int packerRawDataCopyingThreshold;
69+
/**
70+
* Use String.getBytes() for strings smaller than this threshold.
71+
* Note that this parameter is subject to change.
72+
*/
73+
public final int packerSmallStringOptimizationThreshold;
5574

5675
public Config(
5776
boolean readStringAsBinary,
5877
boolean readBinaryAsString,
59-
CodingErrorAction onMalFormedInput,
60-
CodingErrorAction onUnmappableCharacter,
78+
CodingErrorAction actionOnMalFormedInput,
79+
CodingErrorAction actionOnUnmappableCharacter,
6180
int maxUnpackStringSize,
6281
int stringEncoderBufferSize,
6382
int stringDecoderBufferSize,
@@ -71,80 +90,15 @@ public Config(
7190

7291
this.readStringAsBinary = readStringAsBinary;
7392
this.readBinaryAsString = readBinaryAsString;
74-
this.onMalFormedInput = onMalFormedInput;
75-
this.onUnmappableCharacter = onUnmappableCharacter;
93+
this.actionOnMalFormedInput = actionOnMalFormedInput;
94+
this.actionOnUnmappableCharacter = actionOnUnmappableCharacter;
7695
this.maxUnpackStringSize = maxUnpackStringSize;
7796
this.stringEncoderBufferSize = stringEncoderBufferSize;
7897
this.stringDecoderBufferSize = stringDecoderBufferSize;
7998
this.packerBufferSize = packerBufferSize;
8099
this.packerSmallStringOptimizationThreshold = packerSmallStringOptimizationThreshold;
81100
this.packerRawDataCopyingThreshold = packerRawDataCopyingThreshold;
82101
}
83-
84-
/**
85-
* allow unpackBinaryHeader to read str format family (default:true)
86-
*/
87-
public boolean isReadStringAsBinary()
88-
{
89-
return readStringAsBinary;
90-
}
91-
92-
/**
93-
* allow unpackRawStringHeader and unpackString to read bin format family (default: true)
94-
*/
95-
public boolean isReadBinaryAsString()
96-
{
97-
return readBinaryAsString;
98-
}
99-
100-
/**
101-
* Action when encountered a malformed input
102-
*/
103-
public CodingErrorAction getActionOnMalFormedInput()
104-
{
105-
return onMalFormedInput;
106-
}
107-
108-
/**
109-
* Action when an unmappable character is found
110-
*/
111-
public CodingErrorAction getActionOnUnmappableCharacter()
112-
{
113-
return onUnmappableCharacter;
114-
}
115-
116-
/**
117-
* unpackString size limit. (default: Integer.MAX_VALUE)
118-
*/
119-
public int getMaxUnpackStringSize()
120-
{
121-
return maxUnpackStringSize;
122-
}
123-
124-
public int getStringEncoderBufferSize()
125-
{
126-
return stringEncoderBufferSize;
127-
}
128-
129-
public int getStringDecoderBufferSize()
130-
{
131-
return stringDecoderBufferSize;
132-
}
133-
134-
public int getPackerBufferSize()
135-
{
136-
return packerBufferSize;
137-
}
138-
139-
public int getPackerSmallStringOptimizationThreshold()
140-
{
141-
return packerSmallStringOptimizationThreshold;
142-
}
143-
144-
public int getPackerRawDataCopyingThreshold()
145-
{
146-
return packerRawDataCopyingThreshold;
147-
}
148102
}
149103

150104
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ public long getTotalWrittenBytes()
150150
private void prepareEncoder()
151151
{
152152
if (encoder == null) {
153-
this.encoder = MessagePack.UTF8.newEncoder().onMalformedInput(config.getActionOnMalFormedInput()).onUnmappableCharacter(config.getActionOnMalFormedInput());
153+
this.encoder = MessagePack.UTF8.newEncoder().onMalformedInput(config.actionOnMalFormedInput).onUnmappableCharacter(config.actionOnMalFormedInput);
154154
}
155155
}
156156

157157
private void prepareBuffer()
158158
throws IOException
159159
{
160160
if (buffer == null) {
161-
buffer = out.next(config.getPackerBufferSize());
161+
buffer = out.next(config.packerBufferSize);
162162
}
163163
}
164164

@@ -196,7 +196,7 @@ private void ensureCapacity(int numBytesToWrite)
196196
{
197197
if (buffer == null || position + numBytesToWrite >= buffer.size()) {
198198
flush();
199-
buffer = out.next(Math.max(config.getPackerBufferSize(), numBytesToWrite));
199+
buffer = out.next(Math.max(config.packerBufferSize, numBytesToWrite));
200200
}
201201
}
202202

@@ -465,7 +465,7 @@ public MessagePacker packString(String s)
465465
return this;
466466
}
467467

468-
if (s.length() < config.getPackerSmallStringOptimizationThreshold()) {
468+
if (s.length() < config.packerSmallStringOptimizationThreshold) {
469469
// Write the length and payload of small string to the buffer so that it avoids an extra flush of buffer
470470
packSmallString(s);
471471
return this;
@@ -504,8 +504,8 @@ public MessagePacker packString(String s)
504504
}
505505

506506
if (cr.isError()) {
507-
if ((cr.isMalformed() && config.getActionOnMalFormedInput() == CodingErrorAction.REPORT) ||
508-
(cr.isUnmappable() && config.getActionOnUnmappableCharacter() == CodingErrorAction.REPORT)) {
507+
if ((cr.isMalformed() && config.actionOnMalFormedInput == CodingErrorAction.REPORT) ||
508+
(cr.isUnmappable() && config.actionOnUnmappableCharacter == CodingErrorAction.REPORT)) {
509509
cr.throwException();
510510
}
511511
}
@@ -663,7 +663,7 @@ public MessagePacker writePayload(ByteBuffer src)
663663
throws IOException
664664
{
665665
int len = src.remaining();
666-
if (len >= config.getPackerRawDataCopyingThreshold()) {
666+
if (len >= config.packerRawDataCopyingThreshold) {
667667
// Use the source ByteBuffer directly to avoid memory copy
668668

669669
// First, flush the current buffer contents
@@ -701,7 +701,7 @@ public MessagePacker writePayload(byte[] src)
701701
public MessagePacker writePayload(byte[] src, int off, int len)
702702
throws IOException
703703
{
704-
if (len >= config.getPackerRawDataCopyingThreshold()) {
704+
if (len >= config.packerRawDataCopyingThreshold) {
705705
// Use the input array directory to avoid memory copy
706706

707707
// Flush the current buffer contents

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ public long getTotalReadBytes()
172172
private void prepareDecoder()
173173
{
174174
if (decoder == null) {
175-
decodeBuffer = CharBuffer.allocate(config.getStringDecoderBufferSize());
175+
decodeBuffer = CharBuffer.allocate(config.stringDecoderBufferSize);
176176
decoder = MessagePack.UTF8.newDecoder()
177-
.onMalformedInput(config.getActionOnMalFormedInput())
178-
.onUnmappableCharacter(config.getActionOnUnmappableCharacter());
177+
.onMalformedInput(config.actionOnMalFormedInput)
178+
.onUnmappableCharacter(config.actionOnUnmappableCharacter);
179179
}
180180
}
181181

@@ -1011,8 +1011,8 @@ public String unpackString()
10111011
{
10121012
int strLen = unpackRawStringHeader();
10131013
if (strLen > 0) {
1014-
if (strLen > config.getMaxUnpackStringSize()) {
1015-
throw new MessageSizeException(String.format("cannot unpack a String of size larger than %,d: %,d", config.getMaxUnpackStringSize(), strLen), strLen);
1014+
if (strLen > config.maxUnpackStringSize) {
1015+
throw new MessageSizeException(String.format("cannot unpack a String of size larger than %,d: %,d", config.maxUnpackStringSize, strLen), strLen);
10161016
}
10171017

10181018
prepareDecoder();
@@ -1030,7 +1030,7 @@ public String unpackString()
10301030
int readLen = Math.min(position < buffer.size() ? buffer.size() - position : buffer.size(), strLen - cursor);
10311031
if (hasIncompleteMultiBytes) {
10321032
// Prepare enough buffer for decoding multi-bytes character right after running into incomplete one
1033-
readLen = Math.min(config.getStringDecoderBufferSize(), strLen - cursor);
1033+
readLen = Math.min(config.stringDecoderBufferSize, strLen - cursor);
10341034
}
10351035
if (!ensure(readLen)) {
10361036
throw new EOFException();
@@ -1055,7 +1055,7 @@ public String unpackString()
10551055

10561056
if (cr.isUnderflow() && bb.hasRemaining()) {
10571057
// input buffer doesn't have enough bytes for multi bytes characters
1058-
if (config.getActionOnMalFormedInput() == CodingErrorAction.REPORT) {
1058+
if (config.actionOnMalFormedInput == CodingErrorAction.REPORT) {
10591059
throw new MalformedInputException(strLen);
10601060
}
10611061
hasIncompleteMultiBytes = true;
@@ -1064,8 +1064,8 @@ public String unpackString()
10641064
}
10651065

10661066
if (cr.isError()) {
1067-
if ((cr.isMalformed() && config.getActionOnMalFormedInput() == CodingErrorAction.REPORT) ||
1068-
(cr.isUnmappable() && config.getActionOnUnmappableCharacter() == CodingErrorAction.REPORT)) {
1067+
if ((cr.isMalformed() && config.actionOnMalFormedInput == CodingErrorAction.REPORT) ||
1068+
(cr.isUnmappable() && config.actionOnUnmappableCharacter == CodingErrorAction.REPORT)) {
10691069
cr.throwException();
10701070
}
10711071
}
@@ -1204,7 +1204,7 @@ public int unpackRawStringHeader()
12041204
return len;
12051205
}
12061206

1207-
if (config.isReadBinaryAsString()) {
1207+
if (config.readBinaryAsString) {
12081208
len = readBinaryHeader(b);
12091209
if (len >= 0) {
12101210
return len;
@@ -1225,7 +1225,7 @@ public int unpackBinaryHeader()
12251225
return len;
12261226
}
12271227

1228-
if (config.isReadStringAsBinary()) {
1228+
if (config.readStringAsBinary) {
12291229
len = readStringHeader(b);
12301230
if (len >= 0) {
12311231
return len;

0 commit comments

Comments
 (0)