-
Notifications
You must be signed in to change notification settings - Fork 155
Byte arrays support #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Byte arrays support #370
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ | |
*/ | ||
package org.neo4j.driver.internal.net; | ||
|
||
import java.io.IOException; | ||
import java.nio.channels.ByteChannel; | ||
|
||
import org.neo4j.driver.internal.messaging.MessageFormat; | ||
|
@@ -32,15 +31,22 @@ public class SocketProtocolV1 implements SocketProtocol | |
private final Reader reader; | ||
private final Writer writer; | ||
|
||
public SocketProtocolV1( ByteChannel channel ) throws IOException | ||
public static SocketProtocol create( ByteChannel channel ) | ||
{ | ||
messageFormat = new PackStreamMessageFormatV1(); | ||
/*by default the byte array support is enabled*/ | ||
return create( channel, true ); | ||
} | ||
|
||
ChunkedOutput output = new ChunkedOutput( channel ); | ||
BufferingChunkedInput input = new BufferingChunkedInput( channel ); | ||
public static SocketProtocol create( ByteChannel channel, boolean byteArraySupportEnabled ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this method be named |
||
{ | ||
return new SocketProtocolV1( channel, byteArraySupportEnabled ); | ||
} | ||
|
||
this.writer = new PackStreamMessageFormatV1.Writer( output, output.messageBoundaryHook() ); | ||
this.reader = new PackStreamMessageFormatV1.Reader( input, input.messageBoundaryHook() ); | ||
public SocketProtocolV1( ByteChannel channel, boolean byteArraySupportEnabled ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constructor can be private |
||
{ | ||
messageFormat = new PackStreamMessageFormatV1(); | ||
this.writer = messageFormat.newWriter( channel, byteArraySupportEnabled ); | ||
this.reader = messageFormat.newReader( channel ); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.driver.internal.packstream; | ||
|
||
import java.io.IOException; | ||
|
||
public class ByteArrayIncompatiblePacker extends PackStream.Packer | ||
{ | ||
public ByteArrayIncompatiblePacker( PackOutput out ) | ||
{ | ||
super( out ); | ||
} | ||
|
||
public void packBytesHeader( int size ) throws IOException | ||
{ | ||
throw new PackStream.UnPackable( "Packing bytes is not supported " + | ||
"as the current server this driver connected to does not support unpack bytes." ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -501,6 +501,29 @@ public double unpackDouble() throws IOException | |
throw new Unexpected( "Expected a double, but got: " + toHexString( markerByte )); | ||
} | ||
|
||
public byte[] unpackBytes() throws IOException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this method different from |
||
{ | ||
final byte markerByte = in.readByte(); | ||
switch(markerByte) | ||
{ | ||
case BYTES_8: return unpackRawBytes( unpackUINT8() ); | ||
case BYTES_16: return unpackRawBytes( unpackUINT16() ); | ||
case BYTES_32: | ||
{ | ||
long size = unpackUINT32(); | ||
if ( size <= Integer.MAX_VALUE ) | ||
{ | ||
return unpackRawBytes( (int) size ); | ||
} | ||
else | ||
{ | ||
throw new Overflow( "BYTES_32 too long for Java" ); | ||
} | ||
} | ||
default: throw new Unexpected( "Expected bytes, but got: 0x" + toHexString( markerByte & 0xFF )); | ||
} | ||
} | ||
|
||
public String unpackString() throws IOException | ||
{ | ||
final byte markerByte = in.readByte(); | ||
|
@@ -512,20 +535,20 @@ public String unpackString() throws IOException | |
return new String(unpackUtf8(markerByte), UTF_8); | ||
} | ||
|
||
public byte[] unpackBytes() throws IOException | ||
public byte[] unpackRawBytes() throws IOException | ||
{ | ||
final byte markerByte = in.readByte(); | ||
|
||
switch(markerByte) | ||
{ | ||
case BYTES_8: return unpackBytes( unpackUINT8() ); | ||
case BYTES_16: return unpackBytes( unpackUINT16() ); | ||
case BYTES_8: return unpackRawBytes( unpackUINT8() ); | ||
case BYTES_16: return unpackRawBytes( unpackUINT16() ); | ||
case BYTES_32: | ||
{ | ||
long size = unpackUINT32(); | ||
if ( size <= Integer.MAX_VALUE ) | ||
{ | ||
return unpackBytes( (int) size ); | ||
return unpackRawBytes( (int) size ); | ||
} | ||
else | ||
{ | ||
|
@@ -558,17 +581,17 @@ private byte[] unpackUtf8(byte markerByte) throws IOException | |
final byte markerHighNibble = (byte) (markerByte & 0xF0); | ||
final byte markerLowNibble = (byte) (markerByte & 0x0F); | ||
|
||
if ( markerHighNibble == TINY_STRING ) { return unpackBytes( markerLowNibble ); } | ||
if ( markerHighNibble == TINY_STRING ) { return unpackRawBytes( markerLowNibble ); } | ||
switch(markerByte) | ||
{ | ||
case STRING_8: return unpackBytes( unpackUINT8() ); | ||
case STRING_16: return unpackBytes( unpackUINT16() ); | ||
case STRING_8: return unpackRawBytes( unpackUINT8() ); | ||
case STRING_16: return unpackRawBytes( unpackUINT16() ); | ||
case STRING_32: | ||
{ | ||
long size = unpackUINT32(); | ||
if ( size <= Integer.MAX_VALUE ) | ||
{ | ||
return unpackBytes( (int) size ); | ||
return unpackRawBytes( (int) size ); | ||
} | ||
else | ||
{ | ||
|
@@ -608,7 +631,7 @@ private long unpackUINT32() throws IOException | |
return in.readInt() & 0xFFFFFFFFL; | ||
} | ||
|
||
private byte[] unpackBytes( int size ) throws IOException | ||
private byte[] unpackRawBytes(int size ) throws IOException | ||
{ | ||
byte[] heapBuffer = new byte[size]; | ||
in.readBytes( heapBuffer, 0, heapBuffer.length ); | ||
|
@@ -711,5 +734,4 @@ public UnPackable( String message ) | |
super( message ); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,12 @@ public static ServerVersion version( Driver driver ) | |
} | ||
} | ||
|
||
public static ServerVersion version( Session session ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to inline this method in |
||
{ | ||
String versionString = session.run( "RETURN 1" ).consume().server().version(); | ||
return version( versionString ); | ||
} | ||
|
||
public static ServerVersion version( String server ) | ||
{ | ||
if ( server == null ) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to close here?
I think connection will be properly close by the session if it throws from here.