Java Code Examples for com.google.appengine.api.files.FileWriteChannel#write()

The following examples show how to use com.google.appengine.api.files.FileWriteChannel#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskServlet.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
private void processFileWithContent( final PersistenceManager pm, final Key fileKey, final String fileTypeString ) throws IOException {
	LOGGER.fine( "File key: " + fileKey + ", file type: " + fileTypeString );
	
	final FileType fileType = FileType.fromString( fileTypeString );
	
	final FileMetaData fmd;
	try {
		fmd =  (FileMetaData) pm.getObjectById( FILE_TYPE_CLASS_MAP.get( fileType ), fileKey );
	} catch ( final JDOObjectNotFoundException jonfe ) {
		LOGGER.warning( "File not found! (Deleted?)" );
		return;
	}
	LOGGER.fine( "sha1: " + fmd.getSha1() );
	if ( fmd.getBlobKey() != null && fmd.getContent() == null ) {
		LOGGER.warning( "This file is already processed!" );
		return;
	}
	if ( fmd.getContent() == null ) {
		LOGGER.warning( "File does not have content!" );
		return;
	}
	
	// Store content in the Blobstore
	final FileService      fileService = FileServiceFactory.getFileService();
	final AppEngineFile    appeFile    = fileService.createNewBlobFile( FILE_TYPE_MIME_TYPE_MAP.get( fileType ), fmd.getSha1() );
	final FileWriteChannel channel     = fileService.openWriteChannel( appeFile, true );
	final ByteBuffer       bb          = ByteBuffer.wrap( fmd.getContent().getBytes() );
	while ( bb.hasRemaining() )
		channel.write( bb );
	channel.closeFinally();
	
	fmd.setBlobKey( fileService.getBlobKey( appeFile ) );
	fmd.setContent( null );
	
	// I do not catch exceptions (so the task will be retried)
}
 
Example 2
Source File: ServeBlobServlet.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void writeToFile(AppEngineFile file, String content) throws IOException {
    FileWriteChannel channel = service.openWriteChannel(file, true);
    try {
        channel.write(ByteBuffer.wrap(content.getBytes()));
    } finally {
        channel.closeFinally();
    }
}
 
Example 3
Source File: BlobstoreTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected BlobKey writeNewBlobFile(String text) throws IOException {
    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile file = fileService.createNewBlobFile("text/plain", "uploadedText.txt");
    FileWriteChannel channel = fileService.openWriteChannel(file, true);
    try {
        channel.write(ByteBuffer.wrap(text.getBytes()));
    } finally {
        channel.closeFinally();
    }
    return fileService.getBlobKey(file);
}
 
Example 4
Source File: GCSGAEStorageProcessor.java    From wt1 with Apache License 2.0 5 votes vote down vote up
private synchronized void flushBuffer(boolean reinit) throws IOException {
	if (curBuf == null) {
		// processor has already been shutdown
		return;
	}
	curBufGZ.flush();
	curBufGZ.close();

	if (writtenEvents > 0) {
		String name = CSVFormatWriter.newFileName("b" + BackendServiceFactory.getBackendService().getCurrentInstance());

		logger.info("Opening new file name=" + name);
		AppEngineFile file = newFile(name);
		FileWriteChannel channel = fileService.openWriteChannel(file, true);

		logger.info("Writing new file name=" + name + " inSize=" + writtenBeforeGZ + " gzSize=" +  curBuf.size() + ")");
		channel.write(ByteBuffer.wrap(curBuf.toByteArray()));
		logger.info("Closing new file");
		channel.closeFinally();
		logger.info("Closed new file");

		Stats stats = ProcessingQueue.getInstance().getStats();
		synchronized (stats) {
			stats.createdFiles++;
			stats.savedEvents += writtenEvents;
			stats.savedEventsGZippedSize += curBuf.size();
			stats.savedEventsInputSize += writtenBeforeGZ;
		}
	} else {
		logger.info("No events to flush");
	}

	curBuf = null;
	curBufGZ = null;
	if (reinit) {
		initBuffer();
	}
}