/** * Converts a datastore blob to either a byte[] or a Byte[]. * * @param shortBlob The short blob to convert. * @param pojoType The destination type for the array. * @return Object instead of Object[] because primitive arrays don't extend * Object[]. */ private Object convertShortBlobToByteArray(ShortBlob shortBlob, Class<?> pojoType) { if (pojoType.isPrimitive()) { return shortBlob.getBytes(); } else { byte[] bytes = shortBlob.getBytes(); Byte[] array = (Byte[]) Array.newInstance(pojoType, bytes.length); for (int i = 0; i < bytes.length; i++) { array[i] = bytes[i]; } return array; } }
public static byte[] getBytes(Object ob) { if (ob instanceof Blob) { Blob gob = (Blob) ob; return gob.getBytes(); } else { if (ob instanceof ShortBlob) { ShortBlob gob = (ShortBlob) ob; return gob.getBytes(); } else { throw new IllegalArgumentException(ob+" is not byte[] datastore type"); } } } public static int getInt(Object ob)
@Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { columnName = table.getColumnName(column); if (value != null) { ShortBlob blob = (ShortBlob) value; dialog.setBytes(blob.getBytes()); button.setText(dialog.getContentDescription()); } else { button.setText(null); } return button; }
public static void serialize(SerializationStreamWriter streamWriter, ShortBlob instance) throws SerializationException { byte[] bytes = instance.getBytes(); if (bytes == null) { streamWriter.writeInt(-1); } else { streamWriter.writeInt(bytes.length); for (byte b : bytes) { streamWriter.writeByte(b); } } } }
@Override protected void setValue(Object value) { if (value != null) { ShortBlob blob = (ShortBlob) value; MagicResult guess; try { guess = magic.guess(blob.getBytes()); } catch (IOException ex) { throw new IllegalArgumentException(ex); } if (guess != null) { super.setValue(guess.getDescription()); } else { super.setValue("???"); } } else { super.setValue(value); } } }
@Test public void testShortBlobType() { String propertyName = "byteStrProp"; List<Entity> elist = doQuery(kindName, propertyName, ShortBlob.class, true); ShortBlob shortblob = (ShortBlob) elist.get(0).getProperty(propertyName); ShortBlob sameDat = (ShortBlob) elist.get(0).getProperty(propertyName); ShortBlob diffDat = (ShortBlob) elist.get(1).getProperty(propertyName); assertTrue(shortblob.equals(sameDat)); assertFalse(shortblob.equals(diffDat)); Arrays.equals("shortblob".getBytes(), shortblob.getBytes()); assertEquals(0, shortblob.compareTo(sameDat)); assertTrue(shortblob.compareTo(diffDat) != 0); assertEquals(shortblob.hashCode(), shortblob.hashCode()); }
@Test public void testSize() { String kind = kindName + "-size"; int recordSize = (1000 * 1000); // Max. 1000000. byte[] filledRec = new byte[recordSize]; Arrays.fill(filledRec, (byte) 0x41); Blob bigBlob = new Blob(filledRec); assertEquals(recordSize, bigBlob.getBytes().length); Entity eBlob = new Entity(kind, rootKey); eBlob.setProperty("blobProp", bigBlob); service.put(eBlob); recordSize = 500 ; // Max. 500. filledRec = new byte[recordSize]; Arrays.fill(filledRec, (byte) 0x41); ShortBlob shortBlob = new ShortBlob(filledRec); assertEquals(recordSize, shortBlob.getBytes().length); Entity eShortBlob = new Entity(kind, rootKey); eShortBlob.setProperty("byteStrProp", shortBlob); service.put(eShortBlob); service.delete(eBlob.getKey(), eShortBlob.getKey()); }