Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 41980d4

Browse files
committed
Switched to just abort
1 parent 450a9ad commit 41980d4

File tree

6 files changed

+42
-42
lines changed

6 files changed

+42
-42
lines changed

microsoft-azure-storage/src/com/microsoft/azure/storage/blob/BlobDecryptStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public void close() throws IOException
6060
}
6161

6262
@Override
63-
void abortAndClose() throws IOException {
64-
this.cryptoStream.close();
63+
void abort() throws IOException {
64+
// no-op. This method is used in the case of aborting uploads, and decrypt streams are on downloads.
6565
}
6666

6767
@Override

microsoft-azure-storage/src/com/microsoft/azure/storage/blob/BlobEncryptStream.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ final class BlobEncryptStream extends BlobOutputStream {
4545
* Holds the cipher stream.
4646
*/
4747
private CipherOutputStream cipherStream;
48+
49+
private BlobOutputStreamInternal blobStream;
4850

4951
/**
5052
* Initializes a new instance of the BlobEncryptStream class for a CloudBlockBlob
@@ -70,8 +72,7 @@ protected BlobEncryptStream(final CloudBlockBlob blockBlob, final AccessConditio
7072
this.options = options;
7173

7274
this.options.setValidateEncryptionPolicy(false);
73-
BlobOutputStreamInternal blobStream =
74-
new BlobOutputStreamInternal(blockBlob, accessCondition, options, opContext);
75+
blobStream = new BlobOutputStreamInternal(blockBlob, accessCondition, options, opContext);
7576
this.cipherStream = new CipherOutputStream(blobStream, cipher);
7677
}
7778

@@ -160,8 +161,8 @@ public void close() throws IOException {
160161
}
161162

162163
@Override
163-
void abortAndClose() throws IOException {
164-
this.cipherStream.close();
164+
void abort() throws IOException {
165+
this.blobStream.abort();
165166
}
166167

167168
}

microsoft-azure-storage/src/com/microsoft/azure/storage/blob/BlobOutputStream.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public void write(final byte[] data) throws IOException {
113113
public abstract void close() throws IOException;
114114

115115
/**
116-
* Closes the output stream without committing the data to the service, typically to be used in cases of errors or
117-
* exceptions in the data source.
116+
* Signals to the BlobOutputStream that it is being aborted and should not commit the data to the service on
117+
* closing, typically to be used in cases of errors or exceptions in the data source.
118118
*/
119-
abstract void abortAndClose() throws IOException;
119+
abstract void abort() throws IOException;
120120
}

microsoft-azure-storage/src/com/microsoft/azure/storage/blob/BlobOutputStreamInternal.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public Thread newThread(Runnable r) {
144144
*/
145145
private final ThreadPoolExecutor threadExecutor;
146146

147+
/**
148+
* Indicates whether the stream has been aborted and therefore closing will skip committing data.
149+
*/
150+
private boolean aborted;
151+
147152
/**
148153
* Initializes a new instance of the BlobOutputStream class.
149154
*
@@ -314,17 +319,20 @@ public synchronized void close() throws IOException {
314319
this.checkStreamState();
315320

316321
// flush any remaining data
317-
this.flush();
322+
if (!this.aborted) {
323+
this.flush();
324+
}
318325

319326
// shut down the ExecutorService.
320327
this.threadExecutor.shutdown();
321328

322329
// try to commit the blob
323-
try {
324-
this.commit();
325-
}
326-
catch (final StorageException e) {
327-
throw Utility.initIOException(e);
330+
if (!this.aborted) {
331+
try {
332+
this.commit();
333+
} catch (final StorageException e) {
334+
throw Utility.initIOException(e);
335+
}
328336
}
329337
}
330338
finally {
@@ -340,18 +348,8 @@ public synchronized void close() throws IOException {
340348
}
341349

342350
@Override
343-
public void abortAndClose() throws IOException {
344-
try {
345-
this.checkStreamState();
346-
347-
this.threadExecutor.shutdown();
348-
} finally {
349-
this.lastError = new IOException(SR.STREAM_CLOSED);
350-
351-
if (!this.threadExecutor.isShutdown()) {
352-
this.threadExecutor.shutdown();
353-
}
354-
}
351+
public void abort() throws IOException {
352+
this.aborted = true;
355353
}
356354

357355
/**

microsoft-azure-storage/src/com/microsoft/azure/storage/blob/CloudBlockBlob.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -893,20 +893,21 @@ public byte[] getByteArray() {
893893
useOpenWrite = true;
894894
if (useOpenWrite) {
895895
final BlobOutputStream writeStream = this.openOutputStream(accessCondition, options, opContext);
896-
if (options.getCommitWriteOnInputStreamException()) {
897-
try {
898-
writeStream.write(inputDataStream, length);
899-
} finally {
900-
writeStream.close();
901-
}
902-
} else {
903-
try {
904-
writeStream.write(inputDataStream, length);
905-
writeStream.close();
906-
} catch (Exception e) {
907-
writeStream.abortAndClose();
908-
throw e;
896+
/*
897+
We want to give the customer the option to skip the commit on close in case of input stream failures.
898+
While we catch all exceptions here, both from reading the IS and writing to the service, any write which
899+
fails in such a way as to throw effectively aborts the upload anyway, so calling abort in all cases
900+
achieves the intended behavior.
901+
*/
902+
try {
903+
writeStream.write(inputDataStream, length);
904+
} catch (Exception e) {
905+
if (!options.getCommitWriteOnInputStreamException()) {
906+
writeStream.abort();
909907
}
908+
throw e;
909+
} finally {
910+
writeStream.close();
910911
}
911912
}
912913
else {

microsoft-azure-storage/src/com/microsoft/azure/storage/blob/LengthLimitingStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public void close() throws IOException {
8282
}
8383

8484
@Override
85-
void abortAndClose() throws IOException {
86-
// no op
85+
void abort() throws IOException {
86+
// no op. Abort is only used to abort uploads and this type is only used on download paths.
8787
}
8888

8989
}

0 commit comments

Comments
 (0)