1818
1919package org .openqa .selenium .io ;
2020
21- import com .google .common .io .Closeables ;
22-
2321import java .io .BufferedOutputStream ;
2422import java .io .ByteArrayInputStream ;
2523import java .io .ByteArrayOutputStream ;
@@ -44,49 +42,32 @@ public void zip(File inputDir, File output) throws IOException {
4442 throw new IOException ("File already exists: " + output );
4543 }
4644
47- FileOutputStream fos = null ;
48- try {
49- fos = new FileOutputStream (output );
45+ try (FileOutputStream fos = new FileOutputStream (output )) {
5046 zip (inputDir , fos );
51- } finally {
52- Closeables .close (fos , false );
5347 }
5448 }
5549
5650 public String zip (File inputDir ) throws IOException {
57- ByteArrayOutputStream bos = new ByteArrayOutputStream ();
58-
59- try {
51+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream ()) {
6052 zip (inputDir , bos );
6153 return Base64 .getEncoder ().encodeToString (bos .toByteArray ());
62- } finally {
63- bos .close ();
6454 }
6555 }
6656
6757 public String zipFile (File baseDir , File fileToCompress ) throws IOException {
6858 checkArgument (fileToCompress .isFile (), "File should be a file: " + fileToCompress );
6959
70- ByteArrayOutputStream bos = new ByteArrayOutputStream ();
71- ZipOutputStream zos = new ZipOutputStream (bos );
72-
73- try {
60+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream ();
61+ ZipOutputStream zos = new ZipOutputStream (bos )) {
7462 addToZip (baseDir .getAbsolutePath (), zos , fileToCompress );
7563 return Base64 .getEncoder ().encodeToString (bos .toByteArray ());
76- } finally {
77- zos .close ();
78- bos .close ();
7964 }
8065
8166 }
8267
8368 private void zip (File inputDir , OutputStream writeTo ) throws IOException {
84- ZipOutputStream zos = null ;
85- try {
86- zos = new ZipOutputStream (writeTo );
69+ try (ZipOutputStream zos = new ZipOutputStream (writeTo )) {
8770 addToZip (inputDir .getAbsolutePath (), zos , inputDir );
88- } finally {
89- Closeables .close (zos , false );
9071 }
9172 }
9273
@@ -119,30 +100,19 @@ private void addToZip(String basePath, ZipOutputStream zos, File toAdd) throws I
119100 public void unzip (String source , File outputDir ) throws IOException {
120101 byte [] bytes = Base64 .getMimeDecoder ().decode (source );
121102
122- ByteArrayInputStream bis = null ;
123- try {
124- bis = new ByteArrayInputStream (bytes );
103+ try (ByteArrayInputStream bis = new ByteArrayInputStream (bytes )) {
125104 unzip (bis , outputDir );
126- } finally {
127- Closeables .close (bis , false );
128105 }
129106 }
130107
131108 public void unzip (File source , File outputDir ) throws IOException {
132- FileInputStream fis = null ;
133-
134- try {
135- fis = new FileInputStream (source );
109+ try (FileInputStream fis = new FileInputStream (source )) {
136110 unzip (fis , outputDir );
137- } finally {
138- Closeables .close (fis , false );
139111 }
140112 }
141113
142114 public void unzip (InputStream source , File outputDir ) throws IOException {
143- ZipInputStream zis = new ZipInputStream (source );
144-
145- try {
115+ try (ZipInputStream zis = new ZipInputStream (source )) {
146116 ZipEntry entry ;
147117 while ((entry = zis .getNextEntry ()) != null ) {
148118 File file = new File (outputDir , entry .getName ());
@@ -153,8 +123,6 @@ public void unzip(InputStream source, File outputDir) throws IOException {
153123
154124 unzipFile (outputDir , zis , entry .getName ());
155125 }
156- } finally {
157- zis .close ();
158126 }
159127 }
160128
@@ -165,15 +133,12 @@ public void unzipFile(File output, InputStream zipStream, String name)
165133 if (!FileHandler .createDir (toWrite .getParentFile ()))
166134 throw new IOException ("Cannot create parent director for: " + name );
167135
168- OutputStream out = new BufferedOutputStream (new FileOutputStream (toWrite ), BUF_SIZE );
169- try {
136+ try (OutputStream out = new BufferedOutputStream (new FileOutputStream (toWrite ), BUF_SIZE )) {
170137 byte [] buffer = new byte [BUF_SIZE ];
171138 int read ;
172139 while ((read = zipStream .read (buffer )) != -1 ) {
173140 out .write (buffer , 0 , read );
174141 }
175- } finally {
176- out .close ();
177142 }
178143 }
179144}
0 commit comments