Skip to content

Commit

Permalink
Merge branch 2.3 into 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
systay committed Feb 12, 2016
2 parents b6ac29c + 7fc2ed8 commit b2986b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static String read3bLengthAndString( ReadableChannel channel ) throws IOE
{
short lengthShort = channel.getShort();
byte lengthByte = channel.get();
int length = (lengthByte << 16) | lengthShort;
int length = (lengthByte << 16) | (lengthShort & 0xFFFF);
byte[] chars = new byte[length];
channel.get( chars, length );
return UTF8.decode( chars );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@

public class InMemoryClosableChannel implements ReadableClosablePositionAwareChannel, FlushablePositionAwareChannel
{
private static final Flushable NO_OP_FLUSHABLE = () -> { };

private final byte[] bytes;
private final ByteBuffer asWriter;
private final ByteBuffer asReader;

public InMemoryClosableChannel()
{
this( new byte[1000] );
this( 1000 );
}

public InMemoryClosableChannel( byte[] bytes )
Expand All @@ -47,6 +45,12 @@ public InMemoryClosableChannel( byte[] bytes )
this.asReader = ByteBuffer.wrap( this.bytes );
}

public InMemoryClosableChannel( int bufferSize )
{
this( new byte[bufferSize] );
}


public void reset()
{
asWriter.clear();
Expand Down Expand Up @@ -232,5 +236,5 @@ public int availableBytesToWrite()
{
return asWriter.remaining();
}

private static final Flushable NO_OP_FLUSHABLE = () -> { };
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel;
import org.neo4j.kernel.impl.util.IoPrimitiveUtils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -153,4 +154,30 @@ public void shouldFailWhenUnableToReadALogHeaderFromAFile() throws IOException
assertEquals( "Unable to read log version and last committed tx", ex.getMessage() );
}
}

@Test
public void shouldReadALongString() throws IOException
{
// given

// build a string longer than 32k
int stringSize = 32 * 1024 + 1;
StringBuilder sb = new StringBuilder();
for ( int i = 0; i < stringSize; i++ )
{
sb.append( "x" );
}
String lengthyString = sb.toString();

// we need 3 more bytes for writing the string length
InMemoryClosableChannel channel = new InMemoryClosableChannel( stringSize + 3 );

IoPrimitiveUtils.write3bLengthAndString( channel, lengthyString );

// when
String stringFromChannel = IoPrimitiveUtils.read3bLengthAndString( channel );

// then
assertEquals( lengthyString, stringFromChannel );
}
}

0 comments on commit b2986b7

Please sign in to comment.