类com.google.appengine.api.datastore.Blob源码实例Demo

下面列出了怎么用com.google.appengine.api.datastore.Blob的API类实例代码及写法,或者点击链接到github查看源代码。

@Override
public AppEngineDataStore<V> set(String key, V value) throws IOException {
  Preconditions.checkNotNull(key);
  Preconditions.checkNotNull(value);
  lock.lock();
  try {
    Entity entity = new Entity(getId(), key);
    entity.setUnindexedProperty(FIELD_VALUE, new Blob(IOUtils.serialize(value)));
    dataStoreService.put(entity);
    if (memcache != null) {
      memcache.put(key, value, memcacheExpiration);
    }
  } finally {
    lock.unlock();
  }
  return this;
}
 
源代码2 项目: appengine-tck   文件: StringDataTest.java
@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());
}
 
private static SimpleModule getWriteBlobAsBase64Module() {
  JsonSerializer<Blob> dateSerializer = new JsonSerializer<Blob>() {
    @Override
    public void serialize(Blob value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException {
      byte[] bytes = value.getBytes();
      jgen.writeBinary(bytes, 0, bytes.length);
    }
  };
  SimpleModule writeBlobAsBase64Module = new SimpleModule("writeBlobAsBase64Module",
      new Version(1, 0, 0, null, null, null));
  writeBlobAsBase64Module.addSerializer(Blob.class, dateSerializer);
  return writeBlobAsBase64Module;
}
 
@Test
public void testReadBlobParameter() throws Exception {
  Method method =
      TestEndpoint.class.getDeclaredMethod("doBlob", Blob.class);
  Object[] params = readParameters("{\"blob\":\"AQIDBA==\"}", method);

  assertEquals(1, params.length);
  assertThat(((Blob) params[0]).getBytes()).isEqualTo(new byte[]{1, 2, 3, 4});
}
 
@SuppressWarnings("unused")
public void testBlobAsBase64() throws Exception {
  Object value = new Object() {
    public Blob getBlob() {
      return new Blob(new byte[]{1, 2, 3, 4});
    }
  };
  ObjectNode output = ObjectMapperUtil.createStandardObjectMapper()
      .readValue(writeToResponse(value), ObjectNode.class);
  assertEquals("AQIDBA==", output.path("blob").asText());
}
 
源代码6 项目: appengine-pipelines   文件: ShardedValue.java
@Override
public Entity toEntity() {
  Entity entity = toProtoEntity();
  entity.setUnindexedProperty(SHARD_ID_PROPERTY, shardId);
  entity.setUnindexedProperty(VALUE_PROPERTY, new Blob(value));
  return entity;
}
 
源代码7 项目: appengine-pipelines   文件: ExceptionRecord.java
public ExceptionRecord(Entity entity) {
  super(entity);
  Blob serializedExceptionBlob = (Blob) entity.getProperty(EXCEPTION_PROPERTY);
  byte[] serializedException = serializedExceptionBlob.getBytes();
  try {
    exception = (Throwable) SerializationUtils.deserialize(serializedException);
  } catch (IOException e) {
    throw new RuntimeException("Failed to deserialize exception for " + getKey(), e);
  }
}
 
源代码8 项目: appengine-pipelines   文件: ExceptionRecord.java
@Override
public Entity toEntity() {
  try {
    Entity entity = toProtoEntity();
    byte[] serializedException = SerializationUtils.serialize(exception);
    entity.setUnindexedProperty(EXCEPTION_PROPERTY, new Blob(serializedException));
    return entity;
  } catch (IOException e) {
    throw new RuntimeException("Failed to serialize exception for " + getKey(), e);
  }
}
 
源代码9 项目: appengine-pipelines   文件: AppEngineBackEnd.java
@Override
public Object serializeValue(PipelineModelObject model, Object value) throws IOException {
  byte[] bytes = SerializationUtils.serialize(value);
  if (bytes.length < MAX_BLOB_BYTE_SIZE) {
    return new Blob(bytes);
  }
  int shardId = 0;
  int offset = 0;
  final List<Entity> shardedValues = new ArrayList<>(bytes.length / MAX_BLOB_BYTE_SIZE + 1);
  while (offset < bytes.length) {
    int limit = offset + MAX_BLOB_BYTE_SIZE;
    byte[] chunk = Arrays.copyOfRange(bytes, offset, Math.min(limit, bytes.length));
    offset = limit;
    shardedValues.add(new ShardedValue(model, shardId++, chunk).toEntity());
  }
  return tryFiveTimes(new Operation<List<Key>>("serializeValue") {
    @Override
    public List<Key> call() {
      Transaction tx = dataStore.beginTransaction();
      List<Key> keys;
      try {
        keys = dataStore.put(tx, shardedValues);
        tx.commit();
      } finally {
        if (tx.isActive()) {
          tx.rollback();
        }
      }
      return keys;
    }
  });
}
 
static SessionData createSessionFromEntity(Entity entity) {
  SessionData data = new SessionData();
  data.setExpirationTime((Long) entity.getProperty(EXPIRES_PROP));

  Blob valueBlob = (Blob) entity.getProperty(VALUES_PROP);
  @SuppressWarnings("unchecked")
  Map<String, Object> valueMap = (Map<String, Object>) deserialize(valueBlob.getBytes());
  data.setValueMap(valueMap);
  return data;
}
 
/**
 * Return an {@link Entity} for the given key and data in the empty
 * namespace.
 */
static Entity createEntityForSession(String key, SessionData data) {
  String originalNamespace = NamespaceManager.get();
  try {
    NamespaceManager.set("");
    Entity entity = new Entity(SESSION_ENTITY_TYPE, key);
    entity.setProperty(EXPIRES_PROP, data.getExpirationTime());
    entity.setProperty(VALUES_PROP, new Blob(serialize(data.getValueMap())));
    return entity;
  } finally {
    NamespaceManager.set(originalNamespace);
  }
}
 
源代码12 项目: appengine-tck   文件: StringDataTest.java
@Test
public void testBlobType() {
    String propertyName = "blobProp";
    List<Entity> elist = doQuery(kindName, propertyName, null, false);
    Blob blob = (Blob) elist.get(0).getProperty(propertyName);
    Blob sameDat = (Blob) elist.get(0).getProperty(propertyName);
    Blob diffDat = (Blob) elist.get(1).getProperty(propertyName);
    assertTrue(blob.equals(sameDat));
    assertFalse(blob.equals(diffDat));
    byte[] blobData = blob.getBytes();
    assertTrue(Arrays.equals("blobImage".getBytes(), blobData) ||
               Arrays.equals("blobText".getBytes(), blobData) ||
               Arrays.equals("blobData".getBytes(), blobData));
    assertEquals(blob.hashCode(), blob.hashCode());
}
 
源代码13 项目: appengine-tck   文件: UnindexedPropertiesTest.java
@Test
public void testUnindexedProperties() throws Exception {
    Entity entity = new Entity(UNINDEXED_ENTITY);
    entity.setUnindexedProperty("unindexedString", "unindexedString");
    entity.setUnindexedProperty("unindexedList", new ArrayList<String>(Arrays.asList("listElement1", "listElement2", "listElement3")));
    entity.setUnindexedProperty("unindexedText", new Text("unindexedText"));
    entity.setUnindexedProperty("unindexedBlob", new Blob("unindexedBlob".getBytes()));
    entity.setProperty("text", new Text("text"));
    entity.setProperty("blob", new Blob("blob".getBytes()));

    Key key = service.put(entity);
    sync(3000);  // Not using ancestor queries, so pause before doing queries below.
    Entity entity2 = service.get(key);

    assertTrue(isUnindexed(getRawProperty(entity2, "unindexedString")));
    assertTrue(isUnindexed(getRawProperty(entity2, "unindexedList")));
    assertTrue(isUnindexed(getRawProperty(entity2, "unindexedText")));
    assertTrue(isUnindexed(getRawProperty(entity2, "unindexedBlob")));
    assertTrue(isUnindexed(getRawProperty(entity2, "text")));
    assertTrue(isUnindexed(getRawProperty(entity2, "blob")));

    assertNull(getResult(new Query(UNINDEXED_ENTITY).setFilter(new FilterPredicate("unindexedString", EQUAL, "unindexedString"))));
    assertNull(getResult(new Query(UNINDEXED_ENTITY).setFilter(new FilterPredicate("unindexedList", EQUAL, "listElement1"))));
    assertNull(getResult(new Query(UNINDEXED_ENTITY).setFilter(new FilterPredicate("unindexedText", EQUAL, "unindexedText"))));
    assertNull(getResult(new Query(UNINDEXED_ENTITY).setFilter(new FilterPredicate("text", EQUAL, "text"))));

    service.delete(key);
}
 
源代码14 项目: endpoints-java   文件: ServletRequestParamReader.java
@Override
public Blob deserialize(JsonParser jsonParser, DeserializationContext context)
    throws IOException {
  return new Blob(jsonParser.getBinaryValue());
}
 
源代码15 项目: shortyz   文件: RefreshPuzzleList.java
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    cal.add(Calendar.DATE, -10);

    Downloaders downloaders = new Downloaders();
    DataService data = new DataService();

    for (int i = 0; i < 10; i++) {
        List<Puzzle> puzzles = downloaders.getPuzzles(cal.getTime());
        System.out.println("Got " + puzzles.size() + " puzzles for " +
            cal.getTime());

        for (Puzzle puz : puzzles) {
            PuzzleListing listing = data.findPuzzleListingBySourceAndDate(puz.getSource(),
                    cal.getTime());

            if (listing != null) {
                System.out.println("Puzzle from " + puz.getSource() +
                    " already in database.");
            } else {
                System.out.println("Persisting from " + puz.getSource() +
                    ".");
                listing = new PuzzleListing();
                listing.setDate(cal.getTime());
                listing.setSource(puz.getSource());
                listing.setTitle(puz.getTitle());

                ByteArrayOutputStream puzData = new ByteArrayOutputStream();
                ByteArrayOutputStream meta = new ByteArrayOutputStream();
                IO.save(puz, new DataOutputStream(puzData), new DataOutputStream(meta));
                listing.setPuzzleSerial(new Blob(puzData.toByteArray()));
                listing.setMetaSerial(new Blob(meta.toByteArray()));
                data.store(listing);
            }
        }

        cal.add(Calendar.DATE, 1);
    }

    data.close();
    PuzzleServlet.CACHE.put("puzzle-list", null);
}
 
源代码16 项目: sc2gears   文件: FileMetaData.java
public void setContent( Blob content ) {
 this.content = content;
}
 
源代码17 项目: sc2gears   文件: FileMetaData.java
public Blob getContent() {
 return content;
}
 
源代码18 项目: sc2gears   文件: Map.java
public Blob getImage() {
	return image;
}
 
源代码19 项目: sc2gears   文件: Map.java
public void setImage(Blob image) {
	this.image = image;
}
 
源代码20 项目: sc2gears   文件: TaskServlet.java
private void customTask( final HttpServletRequest request, final PersistenceManager pm ) throws IOException {
	LOGGER.fine( "Key: " + request.getParameter( "key" ) + ", file type: " + request.getParameter( "fileType" ) );
	
	final FileType fileType = FileType.fromString( request.getParameter( "fileType" ) );
	if ( fileType == null ) {
		LOGGER.severe( "Invalid File type!" );
		return;
	}
	
	final Key key = KeyFactory.stringToKey( request.getParameter( "key" ) );
	
	final DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
       final Entity e;
	try {
        e = ds.get( key );
       } catch ( final EntityNotFoundException enfe ) {
		LOGGER.log( Level.WARNING, "Entity not found!", enfe );
        return;
       }
	
       if ( !e.getKind().equals( "Rep" ) && !e.getKind().equals( "Smpd" ) && !e.getKind().equals( "OtherFile" ) ) {
		LOGGER.severe( "Invalid Entity kind:" + e.getKind() );
		return;
       }
       
       if ( (Long) e.getProperty( "v" ) == 4 ) {
		LOGGER.warning( "Entity is already v4!" );
		return;
	}
       
       // Update common properties:
       // TODO
       final int size = ( (Long) e.getProperty( "size" ) ).intValue();
       if ( size < FileServlet.DATASTORE_CONTENT_STORE_LIMIT ) {
           final FileService fileService = FileServiceFactory.getFileService();
   		final AppEngineFile   appeFile = new AppEngineFile( FileSystem.BLOBSTORE, ( (BlobKey) e.getProperty( "blobKey" ) ).getKeyString() );
   		final FileReadChannel channel  = fileService.openReadChannel( appeFile, false );
   		final byte[]          content  = new byte[ size ];
   		final ByteBuffer      wrapper  = ByteBuffer.wrap( content );
   		while ( channel.read( wrapper ) > 0 )
   			;
   		channel.close();
   		
   		e.setProperty( "content", new Blob( content ) );
   		e.setProperty( "blobKey", null );
   		fileService.delete( appeFile );
       }
       e.setUnindexedProperty( "blobKey", e.getProperty( "blobKey" ) );
       e.setUnindexedProperty( "content", e.getProperty( "content" ) );
       
       switch ( fileType ) {
       case SC2REPLAY :
           e.setUnindexedProperty( "matchup", e.getProperty( "matchup" ) );
       	break;
       case MOUSE_PRINT :
       	break;
       case OTHER :
       	break;
       default:
       	throw new RuntimeException( "Invalid file type!" );
       }
       
       // UPGRADE COMPLETE!
	e.setProperty( "v", 4 );
	ds.put( e );
}
 
/** Deserializes the specified object from a Blob using an {@link ObjectInputStream}. */
private V deserialize(Entity entity) throws IOException {
  Blob blob = (Blob) entity.getProperty(FIELD_VALUE);
  return IOUtils.deserialize(blob.getBytes());
}
 
源代码22 项目: appengine-pipelines   文件: ShardedValue.java
public ShardedValue(Entity entity) {
  super(entity);
  this.shardId = (Long) entity.getProperty(SHARD_ID_PROPERTY);
  this.value = ((Blob) entity.getProperty(VALUE_PROPERTY)).getBytes();
}
 
源代码23 项目: appengine-pipelines   文件: FanoutTaskRecord.java
public FanoutTaskRecord(Entity entity) {
  super(entity);
  Blob payloadBlob = (Blob) entity.getProperty(PAYLOAD_PROPERTY);
  payload = payloadBlob.getBytes();
}
 
源代码24 项目: appengine-pipelines   文件: FanoutTaskRecord.java
@Override
public Entity toEntity() {
  Entity entity = toProtoEntity();
  entity.setUnindexedProperty(PAYLOAD_PROPERTY, new Blob(payload));
  return entity;
}
 
源代码25 项目: appengine-gcs-client   文件: LocalRawGcsService.java
@Override
public void finishObjectCreation(RawGcsCreationToken token, ByteBuffer chunk, long timeoutMillis)
    throws IOException {
  ensureInitialized();
  Token t = append(token, chunk);

  int totalBytes = 0;

  BlobKey blobKey = getBlobKeyForFilename(t.filename);
  try (WritableByteChannel outputChannel = Channels.newChannel(blobStorage.storeBlob(blobKey))) {
    for (ByteBuffer buffer : inMemoryData.get(t.filename)){
      totalBytes += buffer.remaining();
      outputChannel.write(buffer);
    }
    inMemoryData.remove(t.filename);
  }

  String mimeType = t.options.getMimeType();
  if (Strings.isNullOrEmpty(mimeType)) {
    mimeType = "application/octet-stream";
  }

  BlobInfo blobInfo = new BlobInfo(
      blobKey, mimeType, new Date(), getPathForGcsFilename(t.filename),
      totalBytes);

  String namespace = NamespaceManager.get();
  try {
    NamespaceManager.set("");
    String blobKeyString = blobInfo.getBlobKey().getKeyString();
    Entity blobInfoEntity =
        new Entity(GOOGLE_STORAGE_FILE_KIND, blobKeyString);
    blobInfoEntity.setProperty(BlobInfoFactory.CONTENT_TYPE, blobInfo.getContentType());
    blobInfoEntity.setProperty(BlobInfoFactory.CREATION, blobInfo.getCreation());
    blobInfoEntity.setProperty(BlobInfoFactory.FILENAME, blobInfo.getFilename());
    blobInfoEntity.setProperty(BlobInfoFactory.SIZE, blobInfo.getSize());
    datastore.put(blobInfoEntity);
  } finally {
    NamespaceManager.set(namespace);
  }

  Entity e = new Entity(makeKey(t.filename));
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  try (ObjectOutputStream oout = new ObjectOutputStream(bout)) {
    oout.writeObject(t.options);
  }
  e.setUnindexedProperty(OPTIONS_PROP, new Blob(bout.toByteArray()));
  e.setUnindexedProperty(CREATION_TIME_PROP, System.currentTimeMillis());
  e.setUnindexedProperty(FILE_LENGTH_PROP, totalBytes);
  datastore.put(null, e);
}
 
源代码26 项目: shortyz   文件: PuzzleListing.java
/**
 * Set the value of metaSerial
 *
 * @param newmetaSerial new value of metaSerial
 */
public void setMetaSerial(Blob newmetaSerial) {
    this.metaSerial = newmetaSerial;
}
 
源代码27 项目: shortyz   文件: PuzzleListing.java
/**
 * Get the value of metaSerial
 *
 * @return the value of metaSerial
 */
public Blob getMetaSerial() {
    return this.metaSerial;
}
 
源代码28 项目: shortyz   文件: PuzzleListing.java
/**
 * Set the value of boxesSerial
 *
 * @param newboxesSerial new value of boxesSerial
 */
public void setPuzzleSerial(Blob newboxesSerial) {
    this.boxesSerial = newboxesSerial;
}
 
源代码29 项目: shortyz   文件: PuzzleListing.java
/**
 * Get the value of boxesSerial
 *
 * @return the value of boxesSerial
 */
public Blob getPuzzleSerial() {
    return this.boxesSerial;
}
 
源代码30 项目: shortyz   文件: SavedPuzzle.java
/**
 * Set the value of metaSerial
 *
 * @param newmetaSerial new value of metaSerial
 */
public void setMetaSerial(Blob newmetaSerial) {
    this.metaSerial = newmetaSerial;
}