Skip to content

Commit

Permalink
fixed #56, merged PR #57
Browse files Browse the repository at this point in the history
  • Loading branch information
RuedigerMoeller committed Mar 1, 2015
1 parent 559ba69 commit ee9da34
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import java.util.zip.ZipFile

apply plugin: 'java'
apply plugin: 'maven'
version="2.23"
version="2.24"

targetCompatibility = 1.7
sourceCompatibility = 1.7
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.ruedigermoeller</groupId>
<artifactId>fst</artifactId>
<version>2.23</version>
<version>2.24</version>

<description>a fast java serialization drop in-replacement + some serialization based utils (Structs, OffHeap Memory)</description>
<url>https://code.google.com/p/fast-serialization/</url>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/nustaq/serialization/FSTDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface FSTDecoder {
double readFDouble() throws IOException;
float readFFloat() throws IOException;
byte readFByte() throws IOException;
int readIntByte() throws IOException;
long readFLong() throws IOException;
char readFChar() throws IOException;
short readFShort() throws IOException;
Expand All @@ -40,7 +41,7 @@ public interface FSTDecoder {
int getInputPos();
void moveTo(int position);
void setInputStream(InputStream in);
void ensureReadAhead(int bytes);
int ensureReadAhead(int bytes); // might signal eof by returning -1, depends on decoder impl though

void reset();
void resetToCopyOf(byte[] bytes, int off, int len);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/nustaq/serialization/FSTObjectInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public Object readObject() throws ClassNotFoundException, IOException {

@Override
public int read() throws IOException {
return getCodec().readFByte();
return getCodec().readIntByte();
}

@Override
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/org/nustaq/serialization/coders/FSTBytezDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ public FSTBytezDecoder(FSTConfiguration conf) {
}

byte tmp[];
public void ensureReadAhead(int bytes) {
public int ensureReadAhead(int bytes) {
if ( inputStream != null ) {
if ( pos+bytes > readUntil ) {
readNextInputChunk(bytes);
return readNextInputChunk(bytes);
}
} else if ( pos+bytes > input.length() ) {
// throw FSTBufferTooSmallException.Instance;
return -1;
}
return 0;
}

protected void readNextInputChunk(int bytes) {
protected int readNextInputChunk(int bytes) {
try {
int toRead = Math.max(Integer.MAX_VALUE, bytes);
if ( inputStream instanceof ByteArrayInputStream ) {
Expand All @@ -89,10 +90,14 @@ protected void readNextInputChunk(int bytes) {
}
input.set(pos,tmp,0,read);
readUntil = pos+read;
}
return read;
} else if ( read == -1 )
return -1;
// fixme: should loop in case read == 0
} catch (IOException e) {
throw FSTUtil.rethrow(e);
}
return 0;
}

char chBufS[];
Expand Down Expand Up @@ -237,6 +242,14 @@ public final byte readFByte() throws IOException {
return input.get(pos++);
}

@Override
public int readIntByte() throws IOException {
final int res = ensureReadAhead(1);
if ( res == -1 )
return -1;
return input.get(pos++) & 0xff;
}

@Override
public long readFLong() throws IOException {
return readPlainLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public byte readFByte() throws IOException {
return (byte) input.readInt();
}

@Override
public int readIntByte() throws IOException {
return (int) input.readInt();
}

@Override
public long readFLong() throws IOException {
return input.readInt();
Expand Down Expand Up @@ -202,7 +207,8 @@ public void setInputStream(InputStream in) {
}

@Override
public void ensureReadAhead(int bytes) {
public int ensureReadAhead(int bytes) {
return 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public FSTStreamDecoder(FSTConfiguration conf) {
}
}

public void ensureReadAhead(int bytes) {
public int ensureReadAhead(int bytes) {
input.ensureReadAhead(bytes);
return 0; // checking for eof too expensive ..
}

char chBufS[];
Expand Down Expand Up @@ -249,6 +250,14 @@ public final byte readFByte() throws IOException {
return input.buf[input.pos++];
}

@Override
public final int readIntByte() throws IOException {
input.ensureReadAhead(1);
if ( input.isFullyRead() )
return -1;
return input.buf[input.pos++] & 0xff;
}

@Override
public long readFLong() throws IOException {
input.ensureReadAhead(9);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public void initFromStream(InputStream in) {
readNextChunk(in);
}

public boolean isFullyRead() {
return fullyRead && pos >= count;
}

public void readNextChunk(InputStream in) {
int read;
try {
Expand Down
35 changes: 31 additions & 4 deletions src/test/ser/SpecialsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
import javax.security.auth.Subject;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.*;
import java.math.BigDecimal;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -85,6 +82,36 @@ public void test( FSTConfiguration conf, Serializable toTest ) {
}


@Test
public void testReadByte() throws IOException {

final ByteArrayOutputStream baos = new ByteArrayOutputStream();

final ByteArrayOutputStream fstbaos = new ByteArrayOutputStream();

ObjectOutputStream oout = new ObjectOutputStream(baos);
writebytes(baos,oout);

FSTObjectOutput fsto = new FSTObjectOutput(fstbaos);
writebytes(fstbaos,fsto);

ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
FSTObjectInput fstin = new FSTObjectInput(new ByteArrayInputStream(fstbaos.toByteArray()));
int rd;
while( (rd=oin.read()) != -1 ) {
Assert.assertTrue(rd == fstin.read() );
}
Assert.assertTrue( fstin.read() == -1 );
}

private void writebytes(ByteArrayOutputStream baos, ObjectOutput oout) throws IOException {
int written[] = { 1, -13, 13, 127, 128, 129, -127, -128, -129, -1, 99, 199, };
for (int i = 0; i < written.length; i++) {
oout.write(written[i]);
}
oout.close();
}

@Test
public void main() throws Exception {
boolean succ = true;
Expand Down

0 comments on commit ee9da34

Please sign in to comment.