Skip to content

Commit

Permalink
[7.17] Fix Azure InputStream#read method (#96034) (#96045)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcofdez committed May 12, 2023
1 parent 724c7d1 commit eb9e452
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/changelog/96034.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 96034
summary: Fix Azure `InputStream#read` method
area: Snapshot/Restore
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.sun.net.httpserver.HttpHandler;

import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.blobstore.BlobContainer;
import org.elasticsearch.common.blobstore.BlobPath;
import org.elasticsearch.common.blobstore.BlobStore;
Expand All @@ -30,7 +31,9 @@
import org.elasticsearch.repositories.blobstore.ESMockAPIBasedRepositoryIntegTestCase;
import org.elasticsearch.rest.RestStatus;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
Expand All @@ -43,6 +46,7 @@

import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

@SuppressForbidden(reason = "this test uses a HttpServer to emulate an Azure endpoint")
Expand Down Expand Up @@ -267,4 +271,24 @@ public void testNotFoundErrorMessageContainsFullKey() throws Exception {
assertThat(exception.getMessage(), containsString("nested/dir/blob] not found"));
}
}

public void testReadByteByByte() throws Exception {
try (BlobStore store = newBlobStore()) {
BlobContainer container = store.blobContainer(BlobPath.EMPTY.add(UUIDs.randomBase64UUID()));
byte[] data = randomBytes(randomIntBetween(128, 512));
String blobName = randomName();
container.writeBlob(blobName, new ByteArrayInputStream(data), data.length, true);

InputStream originalDataInputStream = new ByteArrayInputStream(data);
try (InputStream azureInputStream = container.readBlob(blobName)) {
for (int i = 0; i < data.length; i++) {
assertThat(originalDataInputStream.read(), is(equalTo(azureInputStream.read())));
}

assertThat(azureInputStream.read(), is(equalTo(-1)));
assertThat(originalDataInputStream.read(), is(equalTo(-1)));
}
container.delete();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,19 @@ private ByteBuf copyBuffer(ByteBuffer buffer) {
@Override
public int read() throws IOException {
byte[] b = new byte[1];
return read(b, 0, 1);
int bytesRead = read(b, 0, 1);

if (bytesRead > 1) {
throw new IOException("Stream returned more data than requested");
}

if (bytesRead == 1) {
return b[0] & 0xFF;
} else if (bytesRead == 0) {
throw new IOException("Stream returned unexpected number of bytes");
} else {
return -1;
}
}

@Override
Expand Down

0 comments on commit eb9e452

Please sign in to comment.