Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public static ByteChannel create( String host, int port, Config config, Logger l
}
case NONE:
{
channel = new AllOrNothingChannel( soChannel );
channel = soChannel;
break;
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SocketProtocolV1( ByteChannel channel ) throws IOException
messageFormat = new PackStreamMessageFormatV1();

ChunkedOutput output = new ChunkedOutput( channel );
ChunkedInput input = new ChunkedInput( channel );
BufferingChunkedInput input = new BufferingChunkedInput( channel );

this.writer = new PackStreamMessageFormatV1.Writer( output, output.messageBoundaryHook() );
this.reader = new PackStreamMessageFormatV1.Reader( input, input.messageBoundaryHook() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.neo4j.driver.v1.exceptions.DatabaseException;
import org.neo4j.driver.v1.exceptions.Neo4jException;
import org.neo4j.driver.v1.exceptions.TransientException;
import org.neo4j.driver.v1.summary.Notification;
import org.neo4j.driver.v1.summary.StatementType;
import org.neo4j.driver.v1.util.Function;

public class SocketResponseHandler implements MessageHandler
{
Expand Down Expand Up @@ -95,8 +97,9 @@ private void collectNotifications( StreamCollector collector, Value notification
{
if ( notifications != null )
{
collector.notifications( notifications.asList( InternalNotification
.VALUE_TO_NOTIFICATION ) );
Function<Value,Notification> notification = InternalNotification
.VALUE_TO_NOTIFICATION;
collector.notifications( notifications.asList( notification ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class TLSSocketChannel implements ByteChannel
private ByteBuffer plainIn;
private ByteBuffer plainOut;

private static final ByteBuffer DUMMY_BUFFER = ByteBuffer.allocateDirect( 0 );

public TLSSocketChannel( String host, int port, SocketChannel channel, Logger logger,
TrustStrategy trustStrategy )
throws GeneralSecurityException, IOException
Expand Down Expand Up @@ -123,7 +125,7 @@ private void runHandshake() throws IOException
break;
case NEED_UNWRAP:
// Unwrap the ssl packet to value ssl handshake information
handshakeStatus = unwrap( null );
handshakeStatus = unwrap( DUMMY_BUFFER );
plainIn.clear();
break;
case NEED_WRAP:
Expand Down Expand Up @@ -329,17 +331,17 @@ private HandshakeStatus wrap( ByteBuffer buffer ) throws IOException
*/
static int bufferCopy( ByteBuffer from, ByteBuffer to )
{
if ( from == null || to == null )
{
return 0;
}
int maxTransfer = Math.min( to.remaining(), from.remaining() );

int i;
for ( i = 0; to.remaining() > 0 && from.remaining() > 0; i++ )
{
to.put( from.get() );
}
return i;
//use a temp buffer and move all data in one go
ByteBuffer temporaryBuffer = from.duplicate();
temporaryBuffer.limit( temporaryBuffer.position() + maxTransfer );
to.put( temporaryBuffer );

//move postion so it appears as if we read the buffer
from.position( from.position() + maxTransfer );

return maxTransfer;
}

/**
Expand Down Expand Up @@ -391,23 +393,18 @@ public int read( ByteBuffer dst ) throws IOException
*/
int toRead = dst.remaining();
plainIn.flip();
if ( plainIn.remaining() >= toRead )
if ( plainIn.hasRemaining() )
{
bufferCopy( plainIn, dst );
plainIn.compact();
}
else
{
dst.put( plainIn ); // Copy whatever left in the plainIn to dst
do
{
plainIn.clear(); // Clear plainIn
unwrap( dst ); // Read more data from the underline channel and save the data read into dst
}
while ( dst.remaining() > 0 ); // If enough bytes read then return otherwise continue reading from channel
plainIn.clear(); // Clear plainIn
unwrap( dst ); // Read more data from the underline channel and save the data read into dst
}

return toRead;
return toRead - dst.remaining();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.neo4j.driver.internal.InternalNode;
import org.neo4j.driver.internal.InternalPath;
import org.neo4j.driver.internal.InternalRelationship;
import org.neo4j.driver.internal.connector.socket.ChunkedInput;
import org.neo4j.driver.internal.connector.socket.BufferingChunkedInput;
import org.neo4j.driver.internal.connector.socket.ChunkedOutput;
import org.neo4j.driver.internal.packstream.PackInput;
import org.neo4j.driver.internal.packstream.PackOutput;
Expand All @@ -44,12 +44,12 @@
import org.neo4j.driver.internal.value.NodeValue;
import org.neo4j.driver.internal.value.PathValue;
import org.neo4j.driver.internal.value.RelationshipValue;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.types.Entity;
import org.neo4j.driver.v1.types.Node;
import org.neo4j.driver.v1.types.Path;
import org.neo4j.driver.v1.types.Relationship;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;

import static org.neo4j.driver.v1.Values.value;

Expand Down Expand Up @@ -87,7 +87,7 @@ public MessageFormat.Writer newWriter( WritableByteChannel ch )
@Override
public MessageFormat.Reader newReader( ReadableByteChannel ch )
{
ChunkedInput input = new ChunkedInput( ch );
BufferingChunkedInput input = new BufferingChunkedInput( ch );
return new Reader( input, input.messageBoundaryHook() );
}

Expand Down Expand Up @@ -534,7 +534,8 @@ private Value unpackValue() throws IOException
{
case NODE:
ensureCorrectStructSize( "NODE", NODE_FIELDS, size );
return new NodeValue( unpackNode() );
InternalNode adapted = unpackNode();
return new NodeValue( adapted );
case RELATIONSHIP:
ensureCorrectStructSize( "RELATIONSHIP", 5, size );
return unpackRelationship();
Expand All @@ -555,7 +556,8 @@ private Value unpackRelationship() throws IOException
String relType = unpacker.unpackString();
Map<String,Value> props = unpackMap();

return new RelationshipValue( new InternalRelationship( urn, startUrn, endUrn, relType, props ) );
InternalRelationship adapted = new InternalRelationship( urn, startUrn, endUrn, relType, props );
return new RelationshipValue( adapted );
}

private InternalNode unpackNode() throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ public class BufferedChannelInput implements PackInput
{
private final ByteBuffer buffer;
private ReadableByteChannel channel;
private static final int DEFAULT_BUFFER_CAPACITY = 8192;

public BufferedChannelInput( int bufferCapacity )
public BufferedChannelInput(ReadableByteChannel ch )
{
this( bufferCapacity, null );
this( DEFAULT_BUFFER_CAPACITY, ch );
}

public BufferedChannelInput( int bufferCapacity, ReadableByteChannel ch )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.neo4j.driver.internal.packstream;

import java.io.IOException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.util.List;
Expand Down Expand Up @@ -147,8 +146,6 @@ public class PackStream
private static final String EMPTY_STRING = "";
private static final Charset UTF_8 = Charset.forName( "UTF-8" );

private static final int DEFAULT_BUFFER_CAPACITY = 8192;

private PackStream() {}

public static class Packer
Expand Down Expand Up @@ -428,29 +425,11 @@ public static class Unpacker
{
private PackInput in;

public Unpacker( ReadableByteChannel channel )
{
this( DEFAULT_BUFFER_CAPACITY );
reset( channel );
}

public Unpacker( int bufferCapacity )
{
assert bufferCapacity >= 8 : "Buffer must be at least 8 bytes.";
this.in = new BufferedChannelInput( bufferCapacity );
}

public Unpacker( PackInput in )
{
this.in = in;
}

public Unpacker reset( ReadableByteChannel ch )
{
((BufferedChannelInput)in).reset( ch );
return this;
}

public boolean hasNext() throws IOException
{
return in.hasMoreData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
public class Functions
{
@SuppressWarnings( "unchecked" )
public static <T> Function<T,T> identity()
{
return IDENTITY;
Expand Down
Loading