-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move zip specific logic to separate fileWriterFactory class
- Loading branch information
Showing
4 changed files
with
109 additions
and
81 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/java/org/cbioportal/file/export/FileWriterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.cbioportal.file.export; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
public interface FileWriterFactory { | ||
Writer newWriter(String name) throws IOException; | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/cbioportal/file/export/ZipOutputStreamWriterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.cbioportal.file.export; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
public class ZipOutputStreamWriterFactory implements FileWriterFactory, Closeable { | ||
|
||
private final ZipOutputStream zipOutputStream; | ||
|
||
public ZipOutputStreamWriterFactory(OutputStream outputStream) { | ||
this.zipOutputStream = new ZipOutputStream(outputStream); | ||
} | ||
|
||
@Override | ||
public Writer newWriter(String name) throws IOException { | ||
return new ZipEntryOutputStreamWriter(name, zipOutputStream); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
zipOutputStream.close(); | ||
} | ||
|
||
static class ZipEntryOutputStreamWriter extends OutputStreamWriter { | ||
private final ZipOutputStream zipOutputStream; | ||
|
||
public ZipEntryOutputStreamWriter(String name, ZipOutputStream zipOutputStream) throws IOException { | ||
super(zipOutputStream); | ||
zipOutputStream.putNextEntry(new ZipEntry(name)); | ||
this.zipOutputStream = zipOutputStream; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
flush(); | ||
zipOutputStream.closeEntry(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters