I'm trying to write a stream of bytes into my postgres server (PostgreSQL 9.5beta2, Windows 8 64bit).
The insert query is as follows:
PreparedStatement insert = Database.prepare("INSERT INTO hierarchical_correlation (reference, target, values) VALUES (?,?,?::BYTEA) ON CONFLICT (reference, target) DO UPDATE SET values = EXCLUDED.values")
The schmea for hierarhical_correlation is
CREATE TABLE public.hierarchical_correlation (
reference HANDLE NOT NULL,
target HANDLE NOT NULL,
values BYTEA,
PRIMARY KEY (reference, target)
);
And I'm inserting the values lik this:
final byte[] score = new byte[]{127,-128,127}; // These are exemplary values, but I have checked and my values are always in that range.
final String reference = scapeplot.reference.hash;
final String target = scapeplot.resolve(0).get().hash;
insert.setString(1, reference);
insert.setString(2, target);
insert.setBinaryStream(3, new ByteArrayInputStream(score));
insert.setBytes(3, score); // I have tried both
Since byte in Java is of range -128 to 127, and as far as I can tell postgres has 0-255 I expect -128 to be 0 and 127 to be 128 to 255. When I evaluate the above, and check its value I get something like(those are obviously not the equivalent bytes, but the values the below string generated where either 128 or -127)
'7F807F80807F8080808080808080808080'
This is obviously kinda right, but completely wrong: 0x7F is 127, which is right, given no rebasing by JDBC and 0x80 is 128, which obviously is wrong, but given that it produces and overflow, we are at the right value.
So how would I go about inserting these values? I've tried changing, neither value produces the results I want.
bytea_output = 'escape' # hex, escape
I actually suspect it's a bug on the JDBC side, but I've not yet exhausted my positivity.