Skip to content

Commit 4f5b594

Browse files
committed
Simplify the config object
1 parent fe287ba commit 4f5b594

File tree

3 files changed

+46
-91
lines changed

3 files changed

+46
-91
lines changed

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

Lines changed: 28 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,36 @@ 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 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;
5469

5570
public Config(
5671
boolean readStringAsBinary,
5772
boolean readBinaryAsString,
58-
CodingErrorAction onMalFormedInput,
59-
CodingErrorAction onUnmappableCharacter,
73+
CodingErrorAction actionOnMalFormedInput,
74+
CodingErrorAction actionOnUnmappableCharacter,
6075
int maxUnpackStringSize,
6176
int stringEncoderBufferSize,
6277
int stringDecoderBufferSize,
@@ -69,74 +84,14 @@ public Config(
6984

7085
this.readStringAsBinary = readStringAsBinary;
7186
this.readBinaryAsString = readBinaryAsString;
72-
this.onMalFormedInput = onMalFormedInput;
73-
this.onUnmappableCharacter = onUnmappableCharacter;
87+
this.actionOnMalFormedInput = actionOnMalFormedInput;
88+
this.actionOnUnmappableCharacter = actionOnUnmappableCharacter;
7489
this.maxUnpackStringSize = maxUnpackStringSize;
7590
this.stringEncoderBufferSize = stringEncoderBufferSize;
7691
this.stringDecoderBufferSize = stringDecoderBufferSize;
7792
this.packerBufferSize = packerBufferSize;
7893
this.packerRawDataCopyingThreshold = packerRawDataCopyingThreshold;
7994
}
80-
81-
/**
82-
* allow unpackBinaryHeader to read str format family (default:true)
83-
*/
84-
public boolean isReadStringAsBinary()
85-
{
86-
return readStringAsBinary;
87-
}
88-
89-
/**
90-
* allow unpackRawStringHeader and unpackString to read bin format family (default: true)
91-
*/
92-
public boolean isReadBinaryAsString()
93-
{
94-
return readBinaryAsString;
95-
}
96-
97-
/**
98-
* Action when encountered a malformed input
99-
*/
100-
public CodingErrorAction getActionOnMalFormedInput()
101-
{
102-
return onMalFormedInput;
103-
}
104-
105-
/**
106-
* Action when an unmappable character is found
107-
*/
108-
public CodingErrorAction getActionOnUnmappableCharacter()
109-
{
110-
return onUnmappableCharacter;
111-
}
112-
113-
/**
114-
* unpackString size limit. (default: Integer.MAX_VALUE)
115-
*/
116-
public int getMaxUnpackStringSize()
117-
{
118-
return maxUnpackStringSize;
119-
}
120-
121-
public int getStringEncoderBufferSize()
122-
{
123-
return stringEncoderBufferSize;
124-
}
125-
126-
public int getStringDecoderBufferSize()
127-
{
128-
return stringDecoderBufferSize;
129-
}
130-
131-
public int getPackerBufferSize()
132-
{
133-
return packerBufferSize;
134-
}
135-
136-
public int getPackerRawDataCopyingThreshold()
137-
{
138-
return packerRawDataCopyingThreshold;
139-
}
14095
}
14196

14297
/**

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

Lines changed: 7 additions & 7 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

@@ -490,8 +490,8 @@ public MessagePacker packString(String s)
490490
}
491491

492492
if (cr.isError()) {
493-
if ((cr.isMalformed() && config.getActionOnMalFormedInput() == CodingErrorAction.REPORT) ||
494-
(cr.isUnmappable() && config.getActionOnUnmappableCharacter() == CodingErrorAction.REPORT)) {
493+
if ((cr.isMalformed() && config.actionOnMalFormedInput == CodingErrorAction.REPORT) ||
494+
(cr.isUnmappable() && config.actionOnUnmappableCharacter == CodingErrorAction.REPORT)) {
495495
cr.throwException();
496496
}
497497
}
@@ -649,7 +649,7 @@ public MessagePacker writePayload(ByteBuffer src)
649649
throws IOException
650650
{
651651
int len = src.remaining();
652-
if (len >= config.getPackerRawDataCopyingThreshold()) {
652+
if (len >= config.packerRawDataCopyingThreshold) {
653653
// Use the source ByteBuffer directly to avoid memory copy
654654

655655
// First, flush the current buffer contents
@@ -687,7 +687,7 @@ public MessagePacker writePayload(byte[] src)
687687
public MessagePacker writePayload(byte[] src, int off, int len)
688688
throws IOException
689689
{
690-
if (len >= config.getPackerRawDataCopyingThreshold()) {
690+
if (len >= config.packerRawDataCopyingThreshold) {
691691
// Use the input array directory to avoid memory copy
692692

693693
// 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)