Skip to content

Commit

Permalink
Fix writes with 0 length
Browse files Browse the repository at this point in the history
  • Loading branch information
aozarov committed Mar 7, 2016
1 parent d3224b3 commit cded234
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,20 @@ public Tuple<String, byte[]> read(StorageObject from, Map<Option, ?> options, lo
public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length,
boolean last) {
try {
if (length == 0 && !last) {
return;
}
GenericUrl url = new GenericUrl(uploadId);
HttpRequest httpRequest = storage.getRequestFactory().buildPutRequest(url,
new ByteArrayContent(null, toWrite, toWriteOffset, length));
long limit = destOffset + length;
StringBuilder range = new StringBuilder("bytes ");
range.append(destOffset).append('-').append(limit - 1).append('/');
if (length == 0) {
range.append('*');
} else {
range.append(destOffset).append('-').append(limit - 1);
}
range.append('/');
if (last) {
range.append(limit);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,29 @@ public void testReadAndWriteChannels() throws IOException {
assertTrue(storage.delete(BUCKET, blobName));
}

@Test
public void testReadAndWriteChannelsWithDifferentFileSize() throws IOException {
String blobNamePrefix = "test-read-and-write-channels-blob-";
int[] blobSizes = {0, 700, 1024 * 256, 2 * 1024 * 1024, 4 * 1024 * 1024, 4 * 1024 * 1024 + 1};
Random rnd = new Random();
for (int blobSize : blobSizes) {
String blobName = blobNamePrefix + blobSize;
BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
byte[] bytes = new byte[blobSize];
rnd.nextBytes(bytes);
try (WriteChannel writer = storage.writer(blob)) {
writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
}
ByteBuffer readBytes;
try (ReadChannel reader = storage.reader(blob.blobId())) {
readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length);
reader.read(readBytes);
}
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array());
assertTrue(storage.delete(BUCKET, blobName));
}
}

@Test
public void testReadAndWriteCaptureChannels() throws IOException {
String blobName = "test-read-and-write-capture-channels-blob";
Expand Down

0 comments on commit cded234

Please sign in to comment.