Java Code Examples for com.google.appengine.api.files.FileWriteChannel

The following examples show how to use com.google.appengine.api.files.FileWriteChannel. These examples are extracted from open source projects. 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 Project: wt1   Source File: TestServlet.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	try {
	System.out.println(newFileName());
	 FileService fileService = FileServiceFactory.getFileService();
	GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
	.setBucket("wt1-test1")
	.setKey(newFileName());

	AppEngineFile aef = fileService.createNewGSFile(optionsBuilder.build());
	
	FileWriteChannel ch = fileService.openWriteChannel(aef, true);
	
	Channels.newWriter(ch, "utf8").append("pouet").flush();
	
	ch.closeFinally();
	System.out.println("SUCCESS **************");
	} catch (Exception e) {
		System.out.println("FAILURE **************************");
		e.printStackTrace();
	}
}
 
Example 2
Source Project: sc2gears   Source File: TaskServlet.java    License: 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 3
Source Project: appengine-tck   Source File: PrintServlet.java    License: Apache License 2.0 5 votes vote down vote up
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    log.info("Ping - " + req);

    if (requestHandler != null) {
        requestHandler.handleRequest(req);
    }

    lastRequest = req;

    final DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    try {
        Entity entity = new Entity("Qwert");
        entity.setProperty("xyz", 123);
        Key key = ds.put(entity);

        entity = ds.get(key);
        log.info(entity.toString());

        FileService fs = FileServiceFactory.getFileService();
        AppEngineFile file = fs.createNewBlobFile("qwertfile");
        FileWriteChannel fwc = fs.openWriteChannel(file, false);
        try {
            log.info("b_l = " + fwc.write(ByteBuffer.wrap("qwert".getBytes())));
        } finally {
            fwc.close();
        }
    } catch (Exception e) {
        throw new IOException(e);
    }
}
 
Example 4
Source Project: appengine-tck   Source File: ImageServingUrlTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    fileService = FileServiceFactory.getFileService();
    AppEngineFile file = fileService.createNewBlobFile("image/png");
    FileWriteChannel channel = fileService.openWriteChannel(file, true);
    try {
        try (ReadableByteChannel in = Channels.newChannel(getImageStream("capedwarf.png"))) {
            copy(in, channel);
        }
    } finally {
        channel.closeFinally();
    }

    blobKey = fileService.getBlobKey(file);
}
 
Example 5
Source Project: appengine-tck   Source File: ServeBlobServlet.java    License: 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 6
Source Project: appengine-tck   Source File: BlobstoreTestBase.java    License: 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 7
Source Project: wt1   Source File: GCSGAEStorageProcessor.java    License: 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();
	}
}