From 4911c268c2373192f6e1d6e24feed8624a6526c9 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 5 Aug 2015 19:55:53 +0200 Subject: [PATCH] Changed repositories to work with SQLDroid, which seems to have very limited support for blobs, at least it didn't work when I used stream. --- .../ch/dissem/bitmessage/utils/BytesTest.java | 21 ++++++++++++++++++- .../repository/JdbcAddressRepository.java | 16 +++++++------- .../bitmessage/repository/JdbcHelper.java | 3 +-- .../bitmessage/repository/JdbcInventory.java | 9 ++++---- .../repository/JdbcMessageRepository.java | 5 +++-- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/domain/src/test/java/ch/dissem/bitmessage/utils/BytesTest.java b/domain/src/test/java/ch/dissem/bitmessage/utils/BytesTest.java index 52eb106..1af8d37 100644 --- a/domain/src/test/java/ch/dissem/bitmessage/utils/BytesTest.java +++ b/domain/src/test/java/ch/dissem/bitmessage/utils/BytesTest.java @@ -16,6 +16,7 @@ package ch.dissem.bitmessage.utils; +import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -31,7 +32,7 @@ public class BytesTest { @Test public void ensureExpandsCorrectly() { byte[] source = {1}; - byte[] expected = {0,1}; + byte[] expected = {0, 1}; assertArrayEquals(expected, Bytes.expand(source, 2)); } @@ -53,6 +54,24 @@ public class BytesTest { } } + /** + * This test is used to compare different implementations of the single byte lt comparison. It an safely be ignored. + */ + @Test + @Ignore + public void testLowerThanSingleByte() { + byte[] a = new byte[1]; + byte[] b = new byte[1]; + for (int i = 0; i < 255; i++) { + for (int j = 0; j < 255; j++) { + System.out.println("a = " + i + "\tb = " + j); + a[0] = (byte) i; + b[0] = (byte) j; + assertEquals(i < j, Bytes.lt(a, b)); + } + } + } + @Test public void testLowerThan() { for (int i = 0; i < 1000; i++) { diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcAddressRepository.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcAddressRepository.java index f1e55d6..099690d 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcAddressRepository.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcAddressRepository.java @@ -96,16 +96,17 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito ResultSet rs = stmt.executeQuery("SELECT address, alias, public_key, private_key, subscribed FROM Address WHERE " + where); while (rs.next()) { BitmessageAddress address; - Blob privateKeyBlob = rs.getBlob("private_key"); - if (privateKeyBlob != null) { - PrivateKey privateKey = PrivateKey.read(privateKeyBlob.getBinaryStream()); + + byte[] privateKeyBytes = rs.getBytes("private_key"); + if (privateKeyBytes != null) { + PrivateKey privateKey = PrivateKey.read(new ByteArrayInputStream(privateKeyBytes)); address = new BitmessageAddress(privateKey); } else { address = new BitmessageAddress(rs.getString("address")); - Blob publicKeyBlob = rs.getBlob("public_key"); - if (publicKeyBlob != null) { + byte[] publicKeyBytes = rs.getBytes("public_key"); + if (publicKeyBytes != null) { Pubkey pubkey = Factory.readPubkey(address.getVersion(), address.getStream(), - publicKeyBlob.getBinaryStream(), (int) publicKeyBlob.length(), false); + new ByteArrayInputStream(publicKeyBytes), publicKeyBytes.length, false); if (address.getVersion() == 4 && pubkey instanceof V3Pubkey) { pubkey = new V4Pubkey((V3Pubkey) pubkey); } @@ -179,8 +180,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito if (data != null) { ByteArrayOutputStream out = new ByteArrayOutputStream(); data.writeUnencrypted(out); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - ps.setBlob(parameterIndex, in); + ps.setBytes(parameterIndex, out.toByteArray()); } else { ps.setBlob(parameterIndex, (Blob) null); } diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcHelper.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcHelper.java index 4fbea30..b6e7461 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcHelper.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcHelper.java @@ -82,8 +82,7 @@ abstract class JdbcHelper { if (data != null) { ByteArrayOutputStream os = new ByteArrayOutputStream(); data.write(os); - byte[] bytes = os.toByteArray(); - ps.setBinaryStream(parameterIndex, new ByteArrayInputStream(bytes), bytes.length); + ps.setBytes(parameterIndex, os.toByteArray()); } else { ps.setBlob(parameterIndex, (Blob) null); } diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java index c7e8873..6a9e35c 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java @@ -24,6 +24,7 @@ import ch.dissem.bitmessage.ports.Inventory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; import java.sql.*; import java.util.LinkedList; import java.util.List; @@ -78,8 +79,8 @@ public class JdbcInventory extends JdbcHelper implements Inventory { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT data, version FROM Inventory WHERE hash = X'" + vector + "'"); if (rs.next()) { - Blob data = rs.getBlob("data"); - return Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()); + byte[] data = rs.getBytes("data"); + return Factory.getObjectMessage(rs.getInt("version"), new ByteArrayInputStream(data), data.length); } else { LOG.info("Object requested that we don't have. IV: " + vector); return null; @@ -107,8 +108,8 @@ public class JdbcInventory extends JdbcHelper implements Inventory { ResultSet rs = stmt.executeQuery(query.toString()); List result = new LinkedList<>(); while (rs.next()) { - Blob data = rs.getBlob("data"); - result.add(Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length())); + byte[] data = rs.getBytes("data"); + result.add(Factory.getObjectMessage(rs.getInt("version"), new ByteArrayInputStream(data), data.length)); } return result; } catch (Exception e) { diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java index 278163f..a2476e5 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java @@ -25,6 +25,7 @@ import ch.dissem.bitmessage.ports.MessageRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.sql.*; import java.util.ArrayList; @@ -106,9 +107,9 @@ public class JdbcMessageRepository extends JdbcHelper implements MessageReposito ResultSet rs = stmt.executeQuery("SELECT id, iv, type, sender, recipient, data, sent, received, status FROM Message WHERE " + where); while (rs.next()) { byte[] iv = rs.getBytes("iv"); - Blob data = rs.getBlob("data"); + byte[] data = rs.getBytes("data"); Plaintext.Type type = Plaintext.Type.valueOf(rs.getString("type")); - Plaintext.Builder builder = Plaintext.readWithoutSignature(type, data.getBinaryStream()); + Plaintext.Builder builder = Plaintext.readWithoutSignature(type, new ByteArrayInputStream(data)); long id = rs.getLong("id"); builder.id(id); builder.IV(new InventoryVector(iv));