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;
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++) {

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);
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);
}

View File

@ -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);
}

View File

@ -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<ObjectMessage> 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) {

View File

@ -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));