Skip to content

Commit 07250c8

Browse files
committed
This commit includes the following changes:
* Add VisibleForTesting, Nullable annotations * Add Preconditions class ported from Google Guava * Add license notes for Guava and sbt-extras * Add checkNotNull preconditions * Fix typos
1 parent eb42579 commit 07250c8

File tree

11 files changed

+516
-17
lines changed

11 files changed

+516
-17
lines changed

LICENCE.sbt-extras.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Generated from http://www.opensource.org/licenses/bsd-license.php
2+
Copyright (c) 2011, Paul Phillips. All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
* Neither the name of the author nor the names of its contributors
14+
may be used to endorse or promote products derived from this software
15+
without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

NOTICE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This product includes the software developed by third-party:
2+
3+
* Google Guava https://code.google.com/p/guava-libraries/ (APL2)
4+
* sbt-extras: https://github.com/paulp/sbt-extras (BSD) (LICENSE.sbt-extras.txt)
5+

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.BufferOverflowException;
99
import java.nio.ByteBuffer;
1010
import java.util.concurrent.atomic.AtomicInteger;
11+
import static org.msgpack.core.Preconditions.*;
1112

1213
import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
1314
import static sun.misc.Unsafe.ARRAY_BYTE_INDEX_SCALE;
@@ -137,11 +138,12 @@ public static MessageBuffer wrap(ByteBuffer bb) {
137138
}
138139

139140
/**
140-
* Creates a new MessageBuffer instance bakeed by ByteBuffer
141+
* Creates a new MessageBuffer instance backed by ByteBuffer
141142
* @param bb
142143
* @return
143144
*/
144145
private static MessageBuffer newMessageBuffer(ByteBuffer bb) {
146+
checkNotNull(bb);
145147
try {
146148
// We need to use reflection to create MessageBuffer instances in order to prevent TypeProfile generation for getInt method. TypeProfile will be
147149
// generated to resolve one of the method references when two or more classes overrides the method.
@@ -162,6 +164,7 @@ private static MessageBuffer newMessageBuffer(ByteBuffer bb) {
162164
* @return
163165
*/
164166
private static MessageBuffer newMessageBuffer(byte[] arr) {
167+
checkNotNull(arr);
165168
try {
166169
Constructor<?> constructor = msgBufferClass.getDeclaredConstructor(byte[].class);
167170
return (MessageBuffer) constructor.newInstance(arr);

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.io.InputStream;
55
import java.nio.ByteBuffer;
66
import java.nio.channels.ReadableByteChannel;
7+
import static org.msgpack.core.Preconditions.*;
8+
79

810
/**
911
* Provides a sequence of MessageBuffers that contains message packed data.
@@ -31,7 +33,7 @@ class ArrayMessageBufferInput implements MessageBufferInput {
3133
private boolean isRead = false;
3234

3335
ArrayMessageBufferInput(byte[] arr) {
34-
this.buffer = MessageBuffer.wrap(arr);
36+
this.buffer = MessageBuffer.wrap(checkNotNull(arr, "input array is null"));
3537
}
3638

3739
@Override
@@ -56,9 +58,7 @@ class MessageBufferInputStream implements MessageBufferInput {
5658
private byte[] buffer = new byte[8192];
5759

5860
MessageBufferInputStream(InputStream in) {
59-
if(in == null)
60-
throw new NullPointerException("MessageBufferInputStream: input is null");
61-
this.in = in;
61+
this.in = checkNotNull(in, "input is null");
6262
}
6363

6464
@Override
@@ -81,8 +81,12 @@ public MessageBuffer next() throws IOException {
8181

8282
@Override
8383
public void close() throws IOException {
84-
in.close();
85-
buffer = null;
84+
try {
85+
in.close();
86+
}
87+
finally {
88+
buffer = null;
89+
}
8690
}
8791
}
8892

@@ -91,8 +95,7 @@ class MessageBufferInputChannel implements MessageBufferInput {
9195
private final ReadableByteChannel channel;
9296

9397
MessageBufferInputChannel(ReadableByteChannel channel) {
94-
assert(channel != null);
95-
this.channel = channel;
98+
this.channel = checkNotNull(channel, "input channel is null");
9699
}
97100

98101
@Override

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.msgpack.core;
22

3-
import java.io.FileInputStream;
4-
import java.io.FileOutputStream;
53
import java.io.IOException;
64
import java.io.OutputStream;
75
import java.nio.ByteBuffer;
8-
import java.nio.channels.FileChannel;
96
import java.nio.channels.WritableByteChannel;
7+
import static org.msgpack.core.Preconditions.*;
108

119
/**
1210
* Provides a sequence of MessageBuffers for packing the input data
@@ -38,7 +36,7 @@ class MessageBufferOutputStream implements MessageBufferOutput {
3836
private final OutputStream out;
3937

4038
MessageBufferOutputStream(OutputStream out) {
41-
this.out = out;
39+
this.out = checkNotNull(out, "output is null");
4240
}
4341

4442
@Override
@@ -68,7 +66,7 @@ class MessageBufferOutputChannel implements MessageBufferOutput {
6866
private final WritableByteChannel channel;
6967

7068
MessageBufferOutputChannel(WritableByteChannel channel) {
71-
this.channel = channel;
69+
this.channel = checkNotNull(channel, "output channel is null");
7270
}
7371

7472
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.msgpack.core;
22

33
import org.msgpack.core.MessagePack.Code;
4+
import org.msgpack.core.annotations.VisibleForTesting;
45

56
import java.io.IOException;
67

@@ -322,7 +323,7 @@ public static MessageFormat valueOf(final byte b) {
322323
return formatTable[table[b & 0xFF]];
323324
}
324325

325-
326+
@VisibleForTesting
326327
static MessageFormat toMessageFormat(final byte b) {
327328
if (Code.isPosFixInt(b)) {
328329
return POSFIXINT;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.nio.ByteBuffer;
2121
import static org.msgpack.core.MessagePack.Code.*;
22+
import static org.msgpack.core.Preconditions.*;
2223

2324
/**
2425
* Writer of message packed data.
@@ -49,7 +50,7 @@ public MessagePacker(MessageBufferOutput out) {
4950
}
5051

5152
public MessagePacker(MessageBufferOutput out, int bufferSize) {
52-
assert(out != null);
53+
checkNotNull(out, "MessageBufferOutput is null");
5354
this.out = out;
5455
this.buffer = MessageBuffer.newDirectBuffer(bufferSize);
5556
this.position = 0;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.ArrayList;
2525

2626
import org.msgpack.core.MessagePack.Code;
27+
import static org.msgpack.core.Preconditions.*;
28+
2729

2830
/**
2931
* Reader of message-packed values.
@@ -70,7 +72,7 @@ public static class Options {
7072
private boolean reachedEOF = false;
7173

7274
public MessageUnpacker(MessageBufferInput in) {
73-
this.in = in;
75+
this.in = checkNotNull(in, "MessageBufferInput");
7476
}
7577

7678

0 commit comments

Comments
 (0)