1 21 package com.db4o.test.legacy; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.config.*; 27 import com.db4o.query.*; 28 import com.db4o.test.*; 29 30 public class ByteArray { 31 32 public static interface IByteArrayHolder { 33 byte[] getBytes(); 34 } 35 36 public static class ByteArrayHolder implements IByteArrayHolder { 37 38 public byte[] _bytes; 39 40 public ByteArrayHolder(byte[] bytes) { 41 this._bytes = bytes; 42 } 43 44 public byte[] getBytes() { 45 return _bytes; 46 } 47 } 48 49 public static class SerializableByteArrayHolder implements Serializable, IByteArrayHolder { 50 51 private static final long serialVersionUID = 1L; 52 53 public byte[] _bytes; 54 55 public SerializableByteArrayHolder(byte[] bytes) { 56 this._bytes = bytes; 57 } 58 59 public byte[] getBytes() { 60 return _bytes; 61 } 62 } 63 64 static final int INSTANCES = 2; 65 66 static final int ARRAY_LENGTH = 1024*512; 67 68 public void store() { 69 Test.close(); 70 71 com.db4o.Db4o.configure().objectClass(SerializableByteArrayHolder.class).translate(new TSerializable()); 72 Test.open(); 73 74 for (int i=0; i<INSTANCES; ++i) { 75 Test.store(new ByteArrayHolder(createByteArray())); 76 Test.store(new SerializableByteArrayHolder(createByteArray())); 77 } 78 } 79 80 public void testByteArrayHolder() { 81 timeQueryLoop("raw byte array", ByteArrayHolder.class); 82 } 83 84 public void testSerializableByteArrayHolder() { 85 timeQueryLoop("TSerializable", SerializableByteArrayHolder.class); 86 } 87 88 private void timeQueryLoop(String label, final Class clazz) { 89 90 Test.close(); 91 Test.open(); 92 93 Query query = Test.query(); 94 query.constrain(clazz); 95 96 ObjectSet os = query.execute(); 97 Test.ensure(INSTANCES == os.size()); 98 99 while (os.hasNext()) { 100 Test.ensure(ARRAY_LENGTH == ((IByteArrayHolder) os.next()) 101 .getBytes().length); 102 } 103 } 104 105 byte[] createByteArray() { 106 byte[] bytes = new byte[ARRAY_LENGTH]; 107 for (int i=0; i<bytes.length; ++i) { 108 bytes[i] = (byte)(i % 256); 109 } 110 return bytes; 111 } 112 } 113 | Popular Tags |