This is how I read and write large files:
public byte[] readImageData(BlobKey blobKey, long blobSize) { BlobstoreService blobStoreService = BlobstoreServiceFactory .getBlobstoreService(); byte[] allTheBytes = new byte[0]; long amountLeftToRead = blobSize; long startIndex = 0; while (amountLeftToRead > 0) { long amountToReadNow = Math.min( BlobstoreService.MAX_BLOB_FETCH_SIZE - 1, amountLeftToRead); byte[] chunkOfBytes = blobStoreService.fetchData(blobKey, startIndex, startIndex + amountToReadNow - 1); allTheBytes = ArrayUtils.addAll(allTheBytes, chunkOfBytes); amountLeftToRead -= amountToReadNow; startIndex += amountToReadNow; } return allTheBytes; } public BlobKey writeImageData(byte[] bytes) throws IOException { FileService fileService = FileServiceFactory.getFileService(); AppEngineFile file = fileService.createNewBlobFile("image/jpeg"); boolean lock = true; FileWriteChannel writeChannel = fileService .openWriteChannel(file, lock); writeChannel.write(ByteBuffer.wrap(bytes)); writeChannel.closeFinally(); return fileService.getBlobKey(file); }
matt burns
source share