本文整理汇总了Java中com.google.appengine.api.files.FileWriteChannel.closeFinally方法的典型用法代码示例。如果您正苦于以下问题:Java FileWriteChannel.closeFinally方法的具体用法?Java FileWriteChannel.closeFinally怎么用?Java FileWriteChannel.closeFinally使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.appengine.api.files.FileWriteChannel
的用法示例。
在下文中一共展示了FileWriteChannel.closeFinally方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: dump
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
private static AppEngineFile dump(@Nullable String mimeType, String downloadFilename,
ByteBuffer bytes) throws IOException {
bytes.rewind();
AppEngineFile file = newBlob(mimeType, downloadFilename);
FileWriteChannel out = getFileService().openWriteChannel(file, true);
while (bytes.hasRemaining()) {
out.write(bytes);
}
out.closeFinally();
// Verify if what's in the file matches what we wanted to write -- the Files
// API is still experimental and I've seen it misbehave.
bytes.rewind();
byte[] expected = getBytes(bytes);
byte[] actual = slurp(file);
if (!Arrays.equals(expected, actual)) {
// These may be big log messages, but we need to log something that helps debugging.
log.warning("Tried to write: " + prettyBytes(expected));
log.warning("Bytes found in file: " + prettyBytes(actual));
throw new IOException("File " + file + " does not contain the bytes we intended to write");
}
return file;
}
示例2: writeSchema
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
public static void writeSchema(FileService fileService, String bucketName, String schemaKey, List<String> fieldNames, List<String> fieldTypes) throws IOException,
FileNotFoundException, FinalizationException, LockException {
GSFileOptionsBuilder schemaOptionsBuilder = new GSFileOptionsBuilder()
.setBucket(bucketName)
.setKey(schemaKey)
.setAcl("project-private");
AppEngineFile schemaFile = fileService.createNewGSFile(schemaOptionsBuilder.build());
FileWriteChannel schemaChannel = fileService.openWriteChannel(schemaFile, true);
PrintWriter schemaWriter = new PrintWriter(Channels.newWriter(schemaChannel, "UTF8"));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < fieldNames.size(); i++) {
sb.append(fieldNames.get(i) + ":" + fieldTypes.get(i));
if (i < fieldNames.size() - 1) {
sb.append(",");
}
}
schemaWriter.print(sb);
schemaWriter.close();
schemaChannel.closeFinally();
}
示例3: doGet
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
@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();
}
}
示例4: processFileWithContent
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
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)
}
示例5: setUp
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
@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);
}
示例6: writeToFile
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
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();
}
}
示例7: writeNewBlobFile
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
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);
}
示例8: flushBuffer
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
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();
}
}
示例9: writeToFile
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
private static BlobKey writeToFile(Image image) throws IOException {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(toMimeType(image.getFormat()));
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
writeChannel.write(ByteBuffer.wrap(image.getImageData()));
writeChannel.closeFinally();
return fileService.getBlobKey(file);
}
示例10: storeBlob
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
private BlobKey storeBlob(String contentType, String fileName, InputStream stream) throws IOException {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(contentType, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
ByteBuffer buf = ByteBuffer.allocateDirect(10);
byte[] bytes = new byte[1024];
int count = 0;
int index = 0;
// Continue writing bytes until there are no more
while (count >= 0) {
if (index == count) {
count = stream.read(bytes);
index = 0;
}
// Fill ByteBuffer
while (index < count && buf.hasRemaining()) {
buf.put(bytes[index++]);
}
// Set the limit to the current position and the
// position to 0
// making the new bytes visible for write()
buf.flip();
// Write the bytes to the channel
int numWritten = writeChannel.write(buf);
// Check if all bytes were written
if (buf.hasRemaining()) {
buf.compact();
} else {
buf.clear();
}
}
writeChannel.closeFinally();
return fileService.getBlobKey(file);
}
示例11: structureFileAndPostToDb
import com.google.appengine.api.files.FileWriteChannel; //导入方法依赖的package包/类
/**
* @return
* @throws IOException
*/
private boolean structureFileAndPostToDb() throws IOException {
/* Define the File API specific instances */
BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
FileService fileService = FileServiceFactory.getFileService();
/* */
final AppEngineFile blobFile = fileService.createNewBlobFile(dataWhole.getMimeType(), dataWhole.getFileName());
/* */
final FileWriteChannel writeChannel = fileService.openWriteChannel(blobFile, true);
/* */
Boolean transaction = ofy().transact(new Work<Boolean>() {
@Override
public Boolean run() {
dataPieces = ofy().load().keys(dataWhole.getDataPieceKeyList());
return dataPieces.size() > 0 ? true : false;
}
});
if (transaction) {
int length;
byte[] buffer = new byte[1024000];
for (int i = 1; i <= dataPieces.size(); i++) {
Key<DataPiece> pieceKey = Key.create(Key.create(DataWhole.class, dataWhole.getKey()), DataPiece.class,
dataWhole.getKey() + i);
DataPiece dataPiece = dataPieces.get(pieceKey);
logger.log(Level.INFO, "DataPiece Blobkey is " + dataPiece.getBlobKey());
BlobstoreInputStream blobStream = new BlobstoreInputStream(dataPiece.getBlobKey());
while ((length = blobStream.read(buffer)) != -1) {
writeChannel.write(ByteBuffer.wrap(buffer, 0, length));
}
blobStream.close();
blobStoreService.delete(dataPiece.getBlobKey());
}
writeChannel.closeFinally();
dataWhole.setBlobKey(fileService.getBlobKey(blobFile));
logger.log(Level.INFO, "New Blobkey is " + fileService.getBlobKey(blobFile));
/* Remove the pieces once it has been successfully restructured */
for (DataPiece piece : dataPieces.values()) {
blobStoreService.delete(piece.getBlobKey());
}
Boolean dwTransaction = ofy().transact(new Work<Boolean>() {
@Override
public Boolean run() {
/* Persist the modified DataWhole */
dataWhole.setDataPieceKeyList(new ArrayList<Key<DataPiece>>());
ofy().save().entity(dataWhole).now();
/* Remove the DataWhole's children */
ofy().delete().entities(dataPieces.values()).now();
return true;
}
});
return dwTransaction;
}
return false;
}