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.

This commit is contained in:
Christian Basler 2015-08-05 19:55:53 +02:00
parent b8546e28af
commit 4911c268c2
5 changed files with 37 additions and 17 deletions

View File

@ -16,6 +16,7 @@
package ch.dissem.bitmessage.utils; package ch.dissem.bitmessage.utils;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -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 @Test
public void testLowerThan() { public void testLowerThan() {
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {

View File

@ -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); ResultSet rs = stmt.executeQuery("SELECT address, alias, public_key, private_key, subscribed FROM Address WHERE " + where);
while (rs.next()) { while (rs.next()) {
BitmessageAddress address; BitmessageAddress address;
Blob privateKeyBlob = rs.getBlob("private_key");
if (privateKeyBlob != null) { byte[] privateKeyBytes = rs.getBytes("private_key");
PrivateKey privateKey = PrivateKey.read(privateKeyBlob.getBinaryStream()); if (privateKeyBytes != null) {
PrivateKey privateKey = PrivateKey.read(new ByteArrayInputStream(privateKeyBytes));
address = new BitmessageAddress(privateKey); address = new BitmessageAddress(privateKey);
} else { } else {
address = new BitmessageAddress(rs.getString("address")); address = new BitmessageAddress(rs.getString("address"));
Blob publicKeyBlob = rs.getBlob("public_key"); byte[] publicKeyBytes = rs.getBytes("public_key");
if (publicKeyBlob != null) { if (publicKeyBytes != null) {
Pubkey pubkey = Factory.readPubkey(address.getVersion(), address.getStream(), 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) { if (address.getVersion() == 4 && pubkey instanceof V3Pubkey) {
pubkey = new V4Pubkey((V3Pubkey) pubkey); pubkey = new V4Pubkey((V3Pubkey) pubkey);
} }
@ -179,8 +180,7 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
if (data != null) { if (data != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
data.writeUnencrypted(out); data.writeUnencrypted(out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ps.setBytes(parameterIndex, out.toByteArray());
ps.setBlob(parameterIndex, in);
} else { } else {
ps.setBlob(parameterIndex, (Blob) null); ps.setBlob(parameterIndex, (Blob) null);
} }

View File

@ -82,8 +82,7 @@ abstract class JdbcHelper {
if (data != null) { if (data != null) {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
data.write(os); data.write(os);
byte[] bytes = os.toByteArray(); ps.setBytes(parameterIndex, os.toByteArray());
ps.setBinaryStream(parameterIndex, new ByteArrayInputStream(bytes), bytes.length);
} else { } else {
ps.setBlob(parameterIndex, (Blob) null); ps.setBlob(parameterIndex, (Blob) null);
} }

View File

@ -24,6 +24,7 @@ import ch.dissem.bitmessage.ports.Inventory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.sql.*; import java.sql.*;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -78,8 +79,8 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT data, version FROM Inventory WHERE hash = X'" + vector + "'"); ResultSet rs = stmt.executeQuery("SELECT data, version FROM Inventory WHERE hash = X'" + vector + "'");
if (rs.next()) { if (rs.next()) {
Blob data = rs.getBlob("data"); byte[] data = rs.getBytes("data");
return Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()); return Factory.getObjectMessage(rs.getInt("version"), new ByteArrayInputStream(data), data.length);
} else { } else {
LOG.info("Object requested that we don't have. IV: " + vector); LOG.info("Object requested that we don't have. IV: " + vector);
return null; return null;
@ -107,8 +108,8 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
ResultSet rs = stmt.executeQuery(query.toString()); ResultSet rs = stmt.executeQuery(query.toString());
List<ObjectMessage> result = new LinkedList<>(); List<ObjectMessage> result = new LinkedList<>();
while (rs.next()) { while (rs.next()) {
Blob data = rs.getBlob("data"); byte[] data = rs.getBytes("data");
result.add(Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length())); result.add(Factory.getObjectMessage(rs.getInt("version"), new ByteArrayInputStream(data), data.length));
} }
return result; return result;
} catch (Exception e) { } catch (Exception e) {

View File

@ -25,6 +25,7 @@ import ch.dissem.bitmessage.ports.MessageRepository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.sql.*; import java.sql.*;
import java.util.ArrayList; 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); ResultSet rs = stmt.executeQuery("SELECT id, iv, type, sender, recipient, data, sent, received, status FROM Message WHERE " + where);
while (rs.next()) { while (rs.next()) {
byte[] iv = rs.getBytes("iv"); 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.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"); long id = rs.getLong("id");
builder.id(id); builder.id(id);
builder.IV(new InventoryVector(iv)); builder.IV(new InventoryVector(iv));