-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrap byte arr/stream in BinaryData input stream wrapper so its not a …
…pain to work with parsing JSON and binary data coming over the wire
- Loading branch information
1 parent
1937ef6
commit f015860
Showing
12 changed files
with
309 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
hawkular-feed-comm/feed-comm-api/src/main/java/org/hawkular/feedcomm/api/BinaryData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.hawkular.feedcomm.api; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* This is a "stream" that includes some in-memory data as well as another stream. The in-memory data (or not null | ||
* or empty) will be read first. Once the in-memory data has been exhausted, any additional reads will read data | ||
* from the input stream. | ||
* | ||
* This is used in the use-case that an input stream contained a JSON command (e.g. BasicMessage) followed by | ||
* additional binary data. The JSON parser may have read extra data over and beyond the actual JSON message data. | ||
* In that case, the extra data the JSON parser read will be found in an in-memory byte array. That additional | ||
* in-memory data byte array combined with the input stream that may contain even more data will both be stored | ||
* in this object so both pieces of data can be accessed using the normal input stream API. | ||
*/ | ||
public class BinaryData extends InputStream { | ||
|
||
private byte[] inMemoryData; | ||
private final InputStream streamData; | ||
private int inMemoryDataPointer; | ||
|
||
public BinaryData(byte[] inMemoryData, InputStream streamData) { | ||
this.inMemoryData = (inMemoryData != null) ? inMemoryData : new byte[0]; | ||
this.streamData = streamData; | ||
this.inMemoryDataPointer = 0; | ||
} | ||
|
||
public int read() throws IOException { | ||
if (unreadInMemoryDataExists()) { | ||
return (int) inMemoryData[inMemoryDataPointer++]; | ||
} else { | ||
return streamData.read(); | ||
} | ||
} | ||
|
||
public int read(byte[] b) throws IOException { | ||
if (unreadInMemoryDataExists()) { | ||
return super.read(b); // the superclass implementation is all we need | ||
} else { | ||
return this.streamData.read(b); // delegate directly to the stream | ||
} | ||
} | ||
|
||
public int read(byte[] b, int off, int len) throws IOException { | ||
if (unreadInMemoryDataExists()) { | ||
return super.read(b, off, len); // the superclass implementation is all we need | ||
} else { | ||
return this.streamData.read(b, off, len); // delegate directly to the stream | ||
} | ||
} | ||
|
||
public long skip(long n) throws IOException { | ||
if (unreadInMemoryDataExists()) { | ||
return super.skip(n); // the superclass implementation is all we need | ||
} else { | ||
return this.streamData.skip(n); // delegate directly to the stream | ||
} | ||
} | ||
|
||
public int available() throws IOException { | ||
return (inMemoryData.length - inMemoryDataPointer) + streamData.available(); | ||
} | ||
|
||
public void close() throws IOException { | ||
// force nothing more to be read from the in-memory data, let the garbage collected free up its memory, | ||
// but avoid NPEs by setting inMemoryData to a small zero-byte array. | ||
inMemoryData = new byte[0]; | ||
inMemoryDataPointer = 0; | ||
streamData.close(); | ||
} | ||
|
||
public void mark(int readlimit) { | ||
super.mark(readlimit); // superclass doesn't support this, and neither to we | ||
} | ||
|
||
public void reset() throws IOException { | ||
super.reset(); // superclass doesn't support this, and neither do we | ||
} | ||
|
||
public boolean markSupported() { | ||
return super.markSupported(); // superclass doesn't support this, and neither to we | ||
} | ||
|
||
private boolean unreadInMemoryDataExists() { | ||
return (inMemoryDataPointer < inMemoryData.length); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
hawkular-feed-comm/feed-comm-api/src/test/java/org/hawkular/feedcomm/api/BinaryDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Copyright 2015 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.hawkular.feedcomm.api; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Arrays; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
@SuppressWarnings("resource") | ||
public class BinaryDataTest { | ||
|
||
@Test | ||
public void testEmptyBinaryData() throws Exception { | ||
BinaryData binaryData; | ||
|
||
binaryData = new BinaryData(null, buildInputStream("")); | ||
Assert.assertEquals(0, binaryData.available()); | ||
Assert.assertEquals(-1, binaryData.read()); | ||
binaryData = new BinaryData(buildByteArray(""), buildInputStream("")); | ||
Assert.assertEquals(0, binaryData.available()); | ||
Assert.assertEquals(-1, binaryData.read()); | ||
} | ||
|
||
@Test | ||
public void testClose() throws Exception { | ||
// because our tests use ByteArrayInputStream, close has no effect on it. | ||
// But close does have an effect on our in-memory data - closing the stream closes reads to the in-memory data | ||
BinaryData binaryData = new BinaryData(buildByteArray("123"), buildInputStream("")); | ||
|
||
// show we can start reading | ||
Assert.assertEquals(3, binaryData.available()); | ||
Assert.assertEquals('1', binaryData.read()); | ||
Assert.assertEquals(2, binaryData.available()); | ||
|
||
// show that close worked | ||
binaryData.close(); | ||
Assert.assertEquals(0, binaryData.available()); | ||
Assert.assertEquals(-1, binaryData.read()); | ||
} | ||
|
||
@Test | ||
public void testUnsupportedApis() { | ||
BinaryData binaryData = new BinaryData(buildByteArray(""), buildInputStream("123")); | ||
Assert.assertFalse(binaryData.markSupported()); | ||
try { | ||
binaryData.reset(); | ||
Assert.fail("Mark is not supported - an exception should have been thrown"); | ||
} catch (IOException expected) { | ||
} | ||
} | ||
|
||
@Test | ||
public void testOnlyStreamData() throws Exception { | ||
actualTests("", "1234567890"); | ||
} | ||
|
||
@Test | ||
public void testOnlyInMemoryData() throws Exception { | ||
actualTests("1234567890", ""); | ||
} | ||
|
||
@Test | ||
public void testBothInMemoryDataAndStreamData() throws Exception { | ||
actualTests("12345", "67890"); | ||
} | ||
|
||
// callers MUST assure that the concatenation of byteArray and inputStream will always be "1234567890" | ||
private void actualTests(String byteArrayString, String inputStreamString) throws Exception { | ||
BinaryData binaryData; | ||
byte[] bytes; | ||
|
||
// available() | ||
binaryData = new BinaryData(buildByteArray(byteArrayString), buildInputStream(inputStreamString)); | ||
Assert.assertEquals(10, binaryData.available()); | ||
Assert.assertEquals('1', binaryData.read()); | ||
Assert.assertEquals(9, binaryData.available()); | ||
Assert.assertEquals('2', binaryData.read()); | ||
Assert.assertEquals(8, binaryData.available()); | ||
Assert.assertEquals('3', binaryData.read()); | ||
Assert.assertEquals(7, binaryData.available()); | ||
Assert.assertEquals('4', binaryData.read()); | ||
Assert.assertEquals(6, binaryData.available()); | ||
Assert.assertEquals('5', binaryData.read()); | ||
Assert.assertEquals(5, binaryData.available()); | ||
Assert.assertEquals('6', binaryData.read()); | ||
Assert.assertEquals(4, binaryData.available()); | ||
Assert.assertEquals('7', binaryData.read()); | ||
Assert.assertEquals(3, binaryData.available()); | ||
Assert.assertEquals('8', binaryData.read()); | ||
Assert.assertEquals(2, binaryData.available()); | ||
Assert.assertEquals('9', binaryData.read()); | ||
Assert.assertEquals(1, binaryData.available()); | ||
Assert.assertEquals('0', binaryData.read()); | ||
Assert.assertEquals(0, binaryData.available()); | ||
Assert.assertEquals(-1, binaryData.read()); | ||
|
||
// read(byte[]) | ||
binaryData = new BinaryData(buildByteArray(byteArrayString), buildInputStream(inputStreamString)); | ||
bytes = new byte[0]; // according to javadocs, an array of length of zero always returns 0 from read | ||
Assert.assertEquals(0, binaryData.read(bytes)); | ||
Assert.assertEquals("", new String(bytes, "UTF-8")); | ||
|
||
binaryData = new BinaryData(buildByteArray(byteArrayString), buildInputStream(inputStreamString)); | ||
bytes = new byte[5]; | ||
Assert.assertEquals(5, binaryData.read(bytes)); | ||
Assert.assertEquals("12345", new String(bytes, "UTF-8")); | ||
Assert.assertEquals(5, binaryData.read(bytes)); | ||
Assert.assertEquals("67890", new String(bytes, "UTF-8")); | ||
Assert.assertEquals(-1, binaryData.read(bytes)); | ||
|
||
binaryData = new BinaryData(buildByteArray(byteArrayString), buildInputStream(inputStreamString)); | ||
bytes = new byte[10]; | ||
Assert.assertEquals(10, binaryData.read(bytes)); | ||
Assert.assertEquals("1234567890", new String(bytes, "UTF-8")); | ||
Assert.assertEquals(-1, binaryData.read(bytes)); | ||
|
||
binaryData = new BinaryData(buildByteArray(byteArrayString), buildInputStream(inputStreamString)); | ||
bytes = new byte[15]; | ||
Assert.assertEquals(10, binaryData.read(bytes)); | ||
Assert.assertEquals("1234567890\0\0\0\0\0", new String(bytes, "UTF-8")); | ||
Assert.assertEquals(-1, binaryData.read(bytes)); | ||
|
||
// read(byte[], int, int) | ||
binaryData = new BinaryData(buildByteArray(byteArrayString), buildInputStream(inputStreamString)); | ||
bytes = new byte[0]; // according to javadocs, an array of length of zero always returns 0 from read | ||
Assert.assertEquals(0, binaryData.read(bytes, 0, 0)); | ||
Assert.assertEquals("", new String(bytes, "UTF-8")); | ||
|
||
binaryData = new BinaryData(buildByteArray(byteArrayString), buildInputStream(inputStreamString)); | ||
bytes = new byte[10]; | ||
Assert.assertEquals(3, binaryData.read(bytes, 0, 3)); | ||
Assert.assertEquals(4, binaryData.read(bytes, 3, 4)); | ||
Assert.assertEquals(3, binaryData.read(bytes, 7, 3)); | ||
Assert.assertEquals("1234567890", new String(bytes, "UTF-8")); | ||
Assert.assertEquals(-1, binaryData.read(bytes)); | ||
|
||
// skip(int) | ||
binaryData = new BinaryData(buildByteArray(byteArrayString), buildInputStream(inputStreamString)); | ||
Assert.assertEquals(3, binaryData.skip(3)); | ||
Assert.assertEquals('4', binaryData.read()); | ||
Assert.assertEquals(4, binaryData.skip(4)); | ||
Assert.assertEquals('9', binaryData.read()); | ||
Assert.assertEquals('0', binaryData.read()); | ||
Assert.assertEquals(0, binaryData.skip(111)); | ||
Assert.assertEquals(-1, binaryData.read()); | ||
} | ||
|
||
private byte[] buildByteArray(String str) { | ||
return Arrays.copyOf(str.getBytes(), str.length()); | ||
} | ||
|
||
private InputStream buildInputStream(String str) { | ||
return new ByteArrayInputStream(str.getBytes()); | ||
} | ||
} |
Oops, something went wrong.