Skip to content

Commit b371999

Browse files
committed
Refactoring
1 parent 6a4e5e6 commit b371999

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.jackson.dataformat;
17+
18+
import java.lang.reflect.Field;
19+
import java.util.function.Supplier;
20+
21+
public final class JavaInfo
22+
{
23+
static final Supplier<Boolean> STRING_VALUE_FIELD_IS_CHARS;
24+
static {
25+
boolean stringValueFieldIsChars = false;
26+
try {
27+
Field stringValueField = String.class.getDeclaredField("value");
28+
stringValueFieldIsChars = stringValueField.getType() == char[].class;
29+
}
30+
catch (NoSuchFieldException ignored) {
31+
}
32+
if (stringValueFieldIsChars) {
33+
STRING_VALUE_FIELD_IS_CHARS = () -> true;
34+
}
35+
else {
36+
STRING_VALUE_FIELD_IS_CHARS = () -> false;
37+
}
38+
}
39+
40+
private JavaInfo() {}
41+
}

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
import java.io.ByteArrayOutputStream;
3030
import java.io.IOException;
3131
import java.io.OutputStream;
32-
import java.lang.reflect.Field;
3332
import java.math.BigDecimal;
3433
import java.math.BigInteger;
3534
import java.nio.ByteBuffer;
3635
import java.nio.charset.Charset;
3736
import java.nio.charset.StandardCharsets;
3837
import java.util.ArrayList;
3938
import java.util.List;
40-
import java.util.function.Supplier;
39+
40+
import static org.msgpack.jackson.dataformat.JavaInfo.STRING_VALUE_FIELD_IS_CHARS;
4141

4242
public class MessagePackGenerator
4343
extends GeneratorBase
@@ -56,23 +56,6 @@ public class MessagePackGenerator
5656
private final List<Node> nodes;
5757
private boolean isElementsClosed = false;
5858

59-
static final Supplier<Boolean> STRING_VALUE_FIELD_IS_CHARS;
60-
static {
61-
boolean stringValueFieldIsChars = false;
62-
try {
63-
Field stringValueField = String.class.getDeclaredField("value");
64-
stringValueFieldIsChars = stringValueField.getType() == char[].class;
65-
}
66-
catch (NoSuchFieldException ignored) {
67-
}
68-
if (stringValueFieldIsChars) {
69-
STRING_VALUE_FIELD_IS_CHARS = () -> true;
70-
}
71-
else {
72-
STRING_VALUE_FIELD_IS_CHARS = () -> false;
73-
}
74-
}
75-
7659
private static final class AsciiCharString
7760
{
7861
public final byte[] bytes;
@@ -531,7 +514,7 @@ private void writeByteArrayTextKey(byte[] text, int offset, int len) throws IOEx
531514
}
532515

533516
@Override
534-
public void writeFieldName(String name) throws IOException
517+
public void writeFieldName(String name)
535518
{
536519
if (STRING_VALUE_FIELD_IS_CHARS.get()) {
537520
char[] chars = name.toCharArray();
@@ -543,7 +526,7 @@ public void writeFieldName(String name) throws IOException
543526
}
544527

545528
@Override
546-
public void writeFieldName(SerializableString name) throws IOException
529+
public void writeFieldName(SerializableString name)
547530
{
548531
if (name instanceof SerializedString) {
549532
writeFieldName(name.getValue());

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.math.BigInteger;
4242
import java.nio.charset.StandardCharsets;
4343

44+
import static org.msgpack.jackson.dataformat.JavaInfo.STRING_VALUE_FIELD_IS_CHARS;
45+
4446
public class MessagePackParser
4547
extends ParserMinimalBase
4648
{
@@ -161,7 +163,7 @@ private String unpackString(MessageUnpacker messageUnpacker) throws IOException
161163
int strLen = messageUnpacker.unpackRawStringHeader();
162164
if (strLen <= tempBytes.length) {
163165
messageUnpacker.readPayload(tempBytes, 0, strLen);
164-
if (MessagePackGenerator.STRING_VALUE_FIELD_IS_CHARS.get()) {
166+
if (STRING_VALUE_FIELD_IS_CHARS.get()) {
165167
for (int i = 0; i < strLen; i++) {
166168
byte b = tempBytes[i];
167169
if ((0x80 & b) != 0) {

0 commit comments

Comments
 (0)