-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from cakofony/stream_decoder_reuse_safety
Avoid re-reading data from a cached buffer
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 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
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,42 @@ | ||
package ser; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.nustaq.serialization.FSTConfiguration; | ||
import org.nustaq.serialization.FSTObjectInput; | ||
import org.nustaq.serialization.FSTObjectOutput; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
|
||
public class BasicReuseTest { | ||
|
||
@Rule | ||
public ExpectedException expectedException = ExpectedException.none(); | ||
|
||
@Test | ||
public void testStreamReuse() throws Exception { | ||
FSTConfiguration configuration = FSTConfiguration.createDefaultConfiguration(); | ||
|
||
String expected = "Hello, World!"; | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
FSTObjectOutput fstObjectOutput = configuration.getObjectOutput(baos); | ||
try { | ||
fstObjectOutput.writeObject(expected); | ||
} finally { | ||
fstObjectOutput.flush(); | ||
} | ||
byte[] serializedData = baos.toByteArray(); | ||
FSTObjectInput input = configuration.getObjectInput(new ByteArrayInputStream(serializedData)); | ||
Object read = input.readObject(); | ||
Assert.assertEquals(expected, read); | ||
|
||
FSTObjectInput secondInput = configuration.getObjectInput(new ByteArrayInputStream(new byte[0])); | ||
expectedException.expect(IOException.class); | ||
expectedException.expectMessage("Failed to read"); | ||
secondInput.readObject(); | ||
} | ||
} |