1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.plexus.archiver.util;
17
18 import javax.annotation.WillClose;
19 import javax.annotation.WillNotClose;
20
21 import java.io.BufferedInputStream;
22 import java.io.BufferedOutputStream;
23 import java.io.ByteArrayInputStream;
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.nio.file.Files;
29
30 import org.codehaus.plexus.archiver.ArchiverException;
31 import org.codehaus.plexus.util.IOUtil;
32
33 public class Streams {
34
35 public static final InputStream EMPTY_INPUTSTREAM = new ByteArrayInputStream(new byte[0]);
36
37 public static BufferedInputStream bufferedInputStream(InputStream is) {
38 return is instanceof BufferedInputStream ? (BufferedInputStream) is : new BufferedInputStream(is, 65536);
39 }
40
41 public static BufferedOutputStream bufferedOutputStream(OutputStream os) {
42 return os instanceof BufferedOutputStream ? (BufferedOutputStream) os : new BufferedOutputStream(os, 65536);
43 }
44
45 public static byte[] cacheBuffer() {
46 return new byte[8 * 1024];
47 }
48
49 public static InputStream fileInputStream(File file) throws IOException {
50 return Files.newInputStream(file.toPath());
51 }
52
53 public static InputStream fileInputStream(File file, String operation) throws ArchiverException {
54 try {
55 return Files.newInputStream(file.toPath());
56 } catch (IOException e) {
57 throw new ArchiverException(
58 "Problem reading input file for " + operation + " " + file.getParent() + ", " + e.getMessage());
59 }
60 }
61
62 public static OutputStream fileOutputStream(File file) throws IOException {
63 return Files.newOutputStream(file.toPath());
64 }
65
66 public static OutputStream fileOutputStream(File file, String operation) throws ArchiverException {
67 try {
68 return Files.newOutputStream(file.toPath());
69 } catch (IOException e) {
70 throw new ArchiverException(
71 "Problem creating output file for " + operation + " " + file.getParent() + ", " + e.getMessage());
72 }
73 }
74
75 public static void copyFully(@WillClose InputStream zIn, @WillClose OutputStream out, String gzip)
76 throws ArchiverException {
77
78 try {
79 copyFullyDontCloseOutput(zIn, out, gzip);
80 out.close();
81 out = null;
82 } catch (final IOException e) {
83 throw new ArchiverException("Failure copying.", e);
84 } finally {
85 IOUtil.close(out);
86 }
87 }
88
89 public static void copyFullyDontCloseOutput(@WillClose InputStream zIn, @WillNotClose OutputStream out, String gzip)
90 throws ArchiverException {
91
92 try {
93 byte[] buffer = cacheBuffer();
94 int count = 0;
95 do {
96 try {
97 out.write(buffer, 0, count);
98 } catch (IOException e) {
99 throw new ArchiverException(
100 "Problem writing to output in " + gzip + " operation " + e.getMessage());
101 }
102 count = zIn.read(buffer, 0, buffer.length);
103 } while (count != -1);
104 zIn.close();
105 zIn = null;
106 } catch (IOException e) {
107 throw new ArchiverException("Problem reading from source file in " + gzip + " operation " + e.getMessage());
108
109 } finally {
110 IOUtil.close(zIn);
111 }
112 }
113 }