summaryrefslogtreecommitdiffstats
path: root/sched
diff options
context:
space:
mode:
authorBenoit Lamarche <benoitlamarche@google.com>2015-02-04 14:42:54 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2015-02-04 14:42:54 +0000
commit67bb245352e54941a7979e6333d3dc06cacc6031 (patch)
tree108968effe95083dcf164c4e5e2b0d74b20d7b75 /sched
parentc53f4b0fda658b11aadcff42f34de5fd7cebf57b (diff)
parenteb2950ca01e52fac3a58f07f57096f014d98532b (diff)
downloadtoolchain_jack-67bb245352e54941a7979e6333d3dc06cacc6031.zip
toolchain_jack-67bb245352e54941a7979e6333d3dc06cacc6031.tar.gz
toolchain_jack-67bb245352e54941a7979e6333d3dc06cacc6031.tar.bz2
Merge "Wrap writing the zip entry in the zip outputstream" into ub-jack
Diffstat (limited to 'sched')
-rw-r--r--sched/src/com/android/sched/vfs/WriteZipFS.java48
1 files changed, 35 insertions, 13 deletions
diff --git a/sched/src/com/android/sched/vfs/WriteZipFS.java b/sched/src/com/android/sched/vfs/WriteZipFS.java
index e8993b7..fe03c27 100644
--- a/sched/src/com/android/sched/vfs/WriteZipFS.java
+++ b/sched/src/com/android/sched/vfs/WriteZipFS.java
@@ -32,6 +32,7 @@ import java.util.EnumSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;
import javax.annotation.Nonnull;
@@ -166,17 +167,11 @@ public class WriteZipFS extends BaseVFS<ZipVDir, ZipVFile> implements VFS {
synchronized OutputStream openWrite(@Nonnull ZipVFile file) {
assert !isClosed();
- try {
- outputStream.putNextEntry(file.getZipEntry());
- if (notifyVFileOpenAndReturnPreviousState()) {
- throw new AssertionError(getLocation().getDescription()
- + " cannot be written to because a previous stream has not been closed.");
- }
- return new UnclosableVFileOutputStream(this);
- } catch (IOException e) {
- // TODO(jplesot): Auto-generated catch block
- throw new AssertionError(e);
+ if (notifyVFileOpenAndReturnPreviousState()) {
+ throw new AssertionError(getLocation().getDescription()
+ + " cannot be written to because a previous stream has not been closed.");
}
+ return new ZipEntryOutputStream(this, file.getZipEntry());
}
//
@@ -287,29 +282,56 @@ public class WriteZipFS extends BaseVFS<ZipVDir, ZipVFile> implements VFS {
return capabilities;
}
- private static class UnclosableVFileOutputStream extends FilterOutputStream {
+ private static class ZipEntryOutputStream extends FilterOutputStream {
@Nonnull
private final WriteZipFS vfs;
+ @Nonnull
+ private final ZipEntry zipEntry;
- public UnclosableVFileOutputStream(@Nonnull WriteZipFS vfs) {
+ private boolean entryWritten = false;
+
+ public ZipEntryOutputStream(@Nonnull WriteZipFS vfs, @Nonnull ZipEntry zipEntry) {
super(vfs.outputStream);
this.vfs = vfs;
+ this.zipEntry = zipEntry;
}
@Override
- public void close() {
+ public void close() throws IOException {
+ writeEntryIfNeeded();
// we do not actually close the stream
vfs.notifyVFileClosed();
}
@Override
public void write(byte[] b) throws IOException {
+ writeEntryIfNeeded();
out.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
+ writeEntryIfNeeded();
out.write(b, off, len);
}
+
+ @Override
+ public void write(int b) throws IOException {
+ writeEntryIfNeeded();
+ out.write(b);
+ }
+
+ private synchronized void writeEntryIfNeeded() throws IOException {
+ if (!entryWritten) {
+ try {
+ ((ZipOutputStream) out).putNextEntry(zipEntry);
+ } catch (ZipException e) {
+ // zip format-related exceptions should not happen, we're only interested in IOExceptions
+ // related to the underlying stream.
+ throw new AssertionError(e);
+ }
+ entryWritten = true;
+ }
+ }
}
}