Skip to content

Commit 84b2921

Browse files
committed
Add tests and javadoc for Message and ByteArray
1 parent 215a5df commit 84b2921

File tree

5 files changed

+387
-49
lines changed

5 files changed

+387
-49
lines changed

gcloud-java-core/src/main/java/com/google/cloud/ByteArray.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 Google Inc. All Rights Reserved.
2+
* Copyright 2016 Google Inc. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,8 +20,6 @@
2020
import com.google.common.base.MoreObjects.ToStringHelper;
2121
import com.google.protobuf.ByteString;
2222

23-
import java.io.BufferedInputStream;
24-
import java.io.ByteArrayOutputStream;
2523
import java.io.IOException;
2624
import java.io.InputStream;
2725
import java.io.Serializable;
@@ -35,6 +33,7 @@
3533
public class ByteArray implements Iterable<Byte>, Serializable {
3634

3735
private static final long serialVersionUID = -1908809133893782840L;
36+
3837
private final ByteString byteString;
3938

4039
protected ByteArray(ByteString byteString) {
@@ -75,91 +74,91 @@ public final boolean equals(Object obj) {
7574
}
7675

7776
/**
78-
* Returns the size of this blob.
77+
* Returns the number of bytes in this {@code ByteArray}.
7978
*/
8079
public final int length() {
8180
return byteString.size();
8281
}
8382

8483
/**
85-
* Returns a copy as byte array.
84+
* Returns a copy of this {@code ByteArray} as an array of bytes.
8685
*/
8786
public final byte[] toByteArray() {
8887
return byteString.toByteArray();
8988
}
9089

9190
/**
92-
* Returns the content as {@code UTF-8} string.
91+
* Returns a copy of this {@code ByteArray} as an {@code UTF-8} string.
9392
*/
9493
public final String toStringUtf8() {
9594
return byteString.toStringUtf8();
9695
}
9796

9897
/**
99-
* Returns a read-only {@link ByteBuffer} for this blob content.
98+
* Returns the content of this {@code ByteArray} as a read-only {@link ByteBuffer}.
10099
*/
101100
public final ByteBuffer asReadOnlyByteBuffer() {
102101
return byteString.asReadOnlyByteBuffer();
103102
}
104103

105104
/**
106-
* Returns an {@link InputStream} for this blob content.
105+
* Returns an {@link InputStream} for this {@code ByteArray} content.
107106
*/
108107
public final InputStream asInputStream() {
109-
final ByteBuffer byteBuffer = asReadOnlyByteBuffer();
110-
return new InputStream() {
111-
@Override public int read() {
112-
return !byteBuffer.hasRemaining() ? -1 : byteBuffer.get() & 0xFF;
113-
}
114-
};
108+
return byteString.newInput();
115109
}
116110

117111
protected ByteString byteString() {
118112
return byteString;
119113
}
120114

121115
/**
122-
* Copies bytes into a ByteBuffer.
116+
* Copies the content of this {@code ByteArray} into an existing {@code ByteBuffer}.
123117
*
124118
* @throws java.nio.ReadOnlyBufferException if the target is read-only
125-
* @throws java.nio.BufferOverflowException if the target's remaining() space is not large
126-
* enough to hold the data
119+
* @throws java.nio.BufferOverflowException if the target's {@link ByteBuffer#remaining()} space
120+
* is not large enough to hold the data
127121
*/
128122
public final void copyTo(ByteBuffer target) {
129123
byteString.copyTo(target);
130124
}
131125

132126
/**
133-
* Copies bytes into a buffer.
127+
* Copies the content of this {@code ByteArray} into an array of bytes.
134128
*
135-
* @throws IndexOutOfBoundsException if an offset or size is negative or too large
129+
* @throws IndexOutOfBoundsException if the target is not large enough to hold the data
136130
*/
137131
public final void copyTo(byte[] target) {
138132
byteString.copyTo(target, 0, 0, length());
139133
}
140134

141-
public static final ByteArray copyFrom(byte[] bytes) {
135+
/**
136+
* Creates a {@code ByteArray} object given an array of bytes. The bytes are copied.
137+
*/
138+
public static ByteArray copyFrom(byte[] bytes) {
142139
return new ByteArray(ByteString.copyFrom(bytes));
143140
}
144141

145142
/**
146-
* Copy the bytes using {@code UTF-8} decoding.
143+
* Creates a {@code ByteArray} object given a string. The string is encoded in {@code UTF-8}. The
144+
* bytes are copied.
147145
*/
148-
public static final ByteArray copyFrom(String string) {
146+
public static ByteArray copyFrom(String string) {
149147
return new ByteArray(ByteString.copyFrom(string, StandardCharsets.UTF_8));
150148
}
151149

152-
public static final ByteArray copyFrom(ByteBuffer bytes) {
150+
/**
151+
* Creates a {@code ByteArray} object given a {@link ByteBuffer}. The bytes are copied.
152+
*/
153+
public static ByteArray copyFrom(ByteBuffer bytes) {
153154
return new ByteArray(ByteString.copyFrom(bytes));
154155
}
155156

156-
public static final ByteArray copyFrom(InputStream input) throws IOException {
157-
BufferedInputStream bufferedInput = new BufferedInputStream(input);
158-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
159-
int value;
160-
while ((value = bufferedInput.read()) != -1) {
161-
bytes.write(value);
162-
}
163-
return copyFrom(bytes.toByteArray());
157+
/**
158+
* Creates a {@code ByteArray} object given an {@link InputStream}. The stream is read into the
159+
* created object.
160+
*/
161+
public static ByteArray copyFrom(InputStream input) throws IOException {
162+
return new ByteArray(ByteString.readFrom(input));
164163
}
165164
}

gcloud-java-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import com.google.api.client.http.HttpTransport;
2727
import com.google.api.client.http.javanet.NetHttpTransport;
2828
import com.google.auth.http.HttpCredentialsAdapter;
29+
import com.google.cloud.spi.ServiceRpcFactory;
2930
import com.google.common.collect.Iterables;
3031
import com.google.common.io.Files;
31-
import com.google.cloud.spi.ServiceRpcFactory;
3232

3333
import org.json.JSONException;
3434
import org.json.JSONObject;
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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+
17+
package com.google.cloud;
18+
19+
import static org.junit.Assert.assertArrayEquals;
20+
import static org.junit.Assert.assertEquals;
21+
22+
import com.google.common.io.ByteStreams;
23+
import com.google.protobuf.ByteString;
24+
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
28+
import java.io.ByteArrayInputStream;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.nio.ByteBuffer;
32+
import java.nio.charset.StandardCharsets;
33+
34+
public class ByteArrayTest {
35+
36+
private static final String STRING_CONTENT = "Hello, ByteArray!";
37+
private static final byte[] BYTES_CONTENT = STRING_CONTENT.getBytes(StandardCharsets.UTF_8);
38+
private static final ByteBuffer BYTE_BUFFER_CONTENT = ByteBuffer.wrap(BYTES_CONTENT);
39+
private static final InputStream STREAM_CONTENT = new ByteArrayInputStream(BYTES_CONTENT);
40+
private static final ByteArray STRING_ARRAY = ByteArray.copyFrom(STRING_CONTENT);
41+
private static final ByteArray BYTES_ARRAY = ByteArray.copyFrom(BYTES_CONTENT);
42+
private static final ByteArray BYTE_BUFFER_ARRAY = ByteArray.copyFrom(BYTE_BUFFER_CONTENT);
43+
private static final ByteArray ARRAY = new ByteArray(ByteString.copyFrom(BYTES_CONTENT));
44+
45+
private static ByteArray streamArray;
46+
47+
@BeforeClass
48+
public static void beforeClass() throws IOException {
49+
streamArray = ByteArray.copyFrom(STREAM_CONTENT);
50+
BYTE_BUFFER_CONTENT.flip();
51+
}
52+
53+
@Test
54+
public void testCopyFromString() throws IOException {
55+
assertEquals(STRING_CONTENT, STRING_ARRAY.toStringUtf8());
56+
assertArrayEquals(BYTES_CONTENT, STRING_ARRAY.toByteArray());
57+
assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), STRING_ARRAY.asReadOnlyByteBuffer());
58+
assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(STRING_ARRAY.asInputStream()));
59+
}
60+
61+
@Test
62+
public void testCopyFromByteArray() throws IOException {
63+
assertEquals(STRING_CONTENT, BYTES_ARRAY.toStringUtf8());
64+
assertArrayEquals(BYTES_CONTENT, BYTES_ARRAY.toByteArray());
65+
assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), BYTES_ARRAY.asReadOnlyByteBuffer());
66+
assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(BYTES_ARRAY.asInputStream()));
67+
}
68+
69+
@Test
70+
public void testCopyFromByteBuffer() throws IOException {
71+
assertEquals(STRING_CONTENT, BYTE_BUFFER_ARRAY.toStringUtf8());
72+
assertArrayEquals(BYTES_CONTENT, BYTE_BUFFER_ARRAY.toByteArray());
73+
assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), BYTE_BUFFER_ARRAY.asReadOnlyByteBuffer());
74+
assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(BYTE_BUFFER_ARRAY.asInputStream()));
75+
}
76+
77+
@Test
78+
public void testCopyFromStream() throws IOException {
79+
assertEquals(STRING_CONTENT, streamArray.toStringUtf8());
80+
assertArrayEquals(BYTES_CONTENT, streamArray.toByteArray());
81+
assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), streamArray.asReadOnlyByteBuffer());
82+
assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(streamArray.asInputStream()));
83+
}
84+
85+
@Test
86+
public void testLength() {
87+
assertEquals(BYTES_CONTENT.length, ARRAY.length());
88+
}
89+
90+
@Test
91+
public void testToStringUtf8() {
92+
assertEquals(STRING_CONTENT, ARRAY.toStringUtf8());
93+
}
94+
95+
@Test
96+
public void testToByteArray() {
97+
assertArrayEquals(BYTES_CONTENT, ARRAY.toByteArray());
98+
}
99+
100+
@Test
101+
public void testAsReadOnlyByteBuffer() {
102+
assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), ARRAY.asReadOnlyByteBuffer());
103+
}
104+
105+
@Test
106+
public void testAsInputStream() throws IOException {
107+
assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(ARRAY.asInputStream()));
108+
}
109+
110+
@Test
111+
public void testHashCode() {
112+
assertEquals(STRING_ARRAY.hashCode(), BYTES_ARRAY.hashCode());
113+
assertEquals(BYTES_ARRAY.hashCode(), BYTE_BUFFER_ARRAY.hashCode());
114+
assertEquals(BYTE_BUFFER_ARRAY.hashCode(), streamArray.hashCode());
115+
}
116+
}

0 commit comments

Comments
 (0)