Skip to content

Commit

Permalink
Updated to address pull request comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert DiFalco authored and Robert DiFalco committed Mar 21, 2014
1 parent bb3a552 commit 4856db5
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 430 deletions.
181 changes: 92 additions & 89 deletions src/com/esotericsoftware/kryo/io/KryoDataInput.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,106 @@
package com.esotericsoftware.kryo.io;

import com.esotericsoftware.kryo.KryoException;

import java.io.DataInput;
import java.io.EOFException;
import java.io.IOException;

/**
* Best attempt adapter for {@link DataInput}. Currently only {@link #readLine()} is unsupported. Other methods
* behave slightly differently. For example, {@link #readUTF()} will return a null string.
* behave slightly differently. For example, {@link #readUTF()} may return a null string.
*
* @author Robert DiFalco <[email protected]>
*/
public class KryoDataInput implements DataInput {

protected final Input input;

public KryoDataInput( Input input ) {
this.input = input;
}

public void readFully( byte[] b ) throws IOException {
readFully( b, 0, b.length );
}

public void readFully( byte[] b, int off, int len ) throws IOException {
if (len < 0)
throw new IndexOutOfBoundsException();

int n = 0;
while (n < len) {
int count = input.read(b, off + n, len - n);
if (count < 0)
throw new EOFException();

n += count;
}
}

public int skipBytes( int n ) throws IOException {
return (int)input.skip( (long)n );
}

public boolean readBoolean() throws IOException {
return input.readBoolean();
}

public byte readByte() throws IOException {
return input.readByte();
}

public int readUnsignedByte() throws IOException {
return input.readByteUnsigned();
}

public short readShort() throws IOException {
return input.readShort();
}

public int readUnsignedShort() throws IOException {
return input.readShortUnsigned();
}

public char readChar() throws IOException {
return input.readChar();
}

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

public long readLong() throws IOException {
return input.readLong();
}

public float readFloat() throws IOException {
return input.readFloat();
}

public double readDouble() throws IOException {
return input.readDouble();
}

/**
* This is not currently implemented. The method will currently throw an {@link java.lang.UnsupportedOperationException}
* whenever it is called.
* @throws UnsupportedOperationException when called.
*/
public String readLine() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}

/**
* Reads the length and string of UTF8 characters, or null. This can read strings written by
* {@link KryoDataOutput#writeUTF(String)},
* {@link com.esotericsoftware.kryo.io.Output#writeString(String)},
* {@link com.esotericsoftware.kryo.io.Output#writeString(CharSequence)}, and
* {@link com.esotericsoftware.kryo.io.Output#writeAscii(String)}.
* @return May be null.
*/
public String readUTF() throws IOException {
return input.readString();
}
protected Input input;

public KryoDataInput (Input input) {
setInput(input);
}

public void setInput (Input input) {
this.input = input;
}

public void readFully (byte[] b) throws IOException {
readFully(b, 0, b.length);
}

public void readFully (byte[] b, int off, int len) throws IOException {
try {
input.readBytes(b, off, len);
} catch (KryoException e) {
throw new EOFException(e.getMessage());
}
}

public int skipBytes (int n) throws IOException {
return (int)input.skip((long)n);
}

public boolean readBoolean () throws IOException {
return input.readBoolean();
}

public byte readByte () throws IOException {
return input.readByte();
}

public int readUnsignedByte () throws IOException {
return input.readByteUnsigned();
}

public short readShort () throws IOException {
return input.readShort();
}

public int readUnsignedShort () throws IOException {
return input.readShortUnsigned();
}

public char readChar () throws IOException {
return input.readChar();
}

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

public long readLong () throws IOException {
return input.readLong();
}

public float readFloat () throws IOException {
return input.readFloat();
}

public double readDouble () throws IOException {
return input.readDouble();
}

/**
* This is not currently implemented. The method will currently throw an {@link java.lang.UnsupportedOperationException}
* whenever it is called.
*
* @throws UnsupportedOperationException when called.
* @deprecated this method is not supported in this implementation.
*/
public String readLine () throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}

/**
* Reads the length and string of UTF8 characters, or null. This can read strings written by
* {@link KryoDataOutput#writeUTF(String)},
* {@link com.esotericsoftware.kryo.io.Output#writeString(String)},
* {@link com.esotericsoftware.kryo.io.Output#writeString(CharSequence)}, and
* {@link com.esotericsoftware.kryo.io.Output#writeAscii(String)}.
*
* @return May be null.
*/
public String readUTF () throws IOException {
return input.readString();
}
}
145 changes: 74 additions & 71 deletions src/com/esotericsoftware/kryo/io/KryoDataOutput.java
Original file line number Diff line number Diff line change
@@ -1,83 +1,86 @@
package com.esotericsoftware.kryo.io;

import com.esotericsoftware.kryo.io.Output;

import java.io.DataOutput;
import java.io.IOException;

/**
* A kryo implementation of {@link java.io.DataOutput}.
*
* @author Robert DiFalco <[email protected]>
*/
public class KryoDataOutput implements DataOutput {

protected final Output output;

public KryoDataOutput( Output output ) {
this.output = output;
}

public void write( int b ) throws IOException {
output.write( b );
}

public void write( byte[] b ) throws IOException {
output.write( b );
}

public void write( byte[] b, int off, int len ) throws IOException {
output.write( b, off, len );
}

public void writeBoolean( boolean v ) throws IOException {
output.writeBoolean( v );
}

public void writeByte( int v ) throws IOException {
output.writeByte( v );
}

public void writeShort( int v ) throws IOException {
output.writeShort( v );
}

public void writeChar( int v ) throws IOException {
output.writeChar( (char) v );
}

public void writeInt( int v ) throws IOException {
output.writeInt( v );
}

public void writeLong( long v ) throws IOException {
output.writeLong( v );
}

public void writeFloat( float v ) throws IOException {
output.writeFloat( v );
}

public void writeDouble( double v ) throws IOException {
output.writeDouble( v );
}

public void writeBytes( String s ) throws IOException {
int len = s.length();
for (int i = 0 ; i < len ; i++) {
output.write( (byte) s.charAt( i ) );
}
}

public void writeChars( String s ) throws IOException {
int len = s.length();
for (int i = 0 ; i < len ; i++) {
int v = s.charAt(i);
output.write( ( v >>> 8 ) & 0xFF );
output.write( ( v >>> 0 ) & 0xFF );
}
}

public void writeUTF( String s ) throws IOException {
output.writeString( s );
}
protected Output output;

public KryoDataOutput (Output output) {
setOutput(output);
}

public void setOutput (Output output) {
this.output = output;
}

public void write (int b) throws IOException {
output.write(b);
}

public void write (byte[] b) throws IOException {
output.write(b);
}

public void write (byte[] b, int off, int len) throws IOException {
output.write(b, off, len);
}

public void writeBoolean (boolean v) throws IOException {
output.writeBoolean(v);
}

public void writeByte (int v) throws IOException {
output.writeByte(v);
}

public void writeShort (int v) throws IOException {
output.writeShort(v);
}

public void writeChar (int v) throws IOException {
output.writeChar((char)v);
}

public void writeInt (int v) throws IOException {
output.writeInt(v);
}

public void writeLong (long v) throws IOException {
output.writeLong(v);
}

public void writeFloat (float v) throws IOException {
output.writeFloat(v);
}

public void writeDouble (double v) throws IOException {
output.writeDouble(v);
}

public void writeBytes (String s) throws IOException {
int len = s.length();
for (int i = 0; i < len; i++) {
output.write((byte)s.charAt(i));
}
}

public void writeChars (String s) throws IOException {
int len = s.length();
for (int i = 0; i < len; i++) {
int v = s.charAt(i);
output.write((v >>> 8) & 0xFF);
output.write((v >>> 0) & 0xFF);
}
}

public void writeUTF (String s) throws IOException {
output.writeString(s);
}
}
Loading

0 comments on commit 4856db5

Please sign in to comment.