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 import java.nio.file.Path;
30
31 import org.codehaus.plexus.archiver.ArchiverException;
32 import org.codehaus.plexus.util.io.CachingOutputStream;
33
34 public class Streams {
35
36 public static final InputStream EMPTY_INPUTSTREAM = new ByteArrayInputStream(new byte[0]);
37
38 public static BufferedInputStream bufferedInputStream(InputStream is) {
39 return is instanceof BufferedInputStream ? (BufferedInputStream) is : new BufferedInputStream(is, 65536);
40 }
41
42 public static BufferedOutputStream bufferedOutputStream(OutputStream os) {
43 return os instanceof BufferedOutputStream ? (BufferedOutputStream) os : new BufferedOutputStream(os, 65536);
44 }
45
46 public static byte[] cacheBuffer() {
47 return new byte[8 * 1024];
48 }
49
50 public static InputStream fileInputStream(File file) throws IOException {
51 return Files.newInputStream(file.toPath());
52 }
53
54 public static InputStream fileInputStream(File file, String operation) throws ArchiverException {
55 try {
56 return Files.newInputStream(file.toPath());
57 } catch (IOException e) {
58 throw new ArchiverException(
59 "Problem reading input file for " + operation + " " + file.getParent() + ", " + e.getMessage());
60 }
61 }
62
63 public static OutputStream fileOutputStream(File file) throws IOException {
64 return new CachingOutputStream(file);
65 }
66
67 public static OutputStream fileOutputStream(Path file) throws IOException {
68 return new CachingOutputStream(file);
69 }
70
71 public static OutputStream fileOutputStream(File file, String operation) throws ArchiverException {
72 try {
73 return new CachingOutputStream(file);
74 } catch (IOException e) {
75 throw new ArchiverException(
76 "Problem creating output file for " + operation + " " + file.getParent() + ", " + e.getMessage());
77 }
78 }
79
80 public static void copyFully(@WillClose InputStream zIn, @WillClose OutputStream out, String gzip)
81 throws ArchiverException {
82
83 try (OutputStream o = out) {
84 copyFullyDontCloseOutput(zIn, o, gzip);
85 } catch (final IOException e) {
86 throw new ArchiverException("Failure copying.", e);
87 }
88 }
89
90 public static void copyFullyDontCloseOutput(@WillClose InputStream zIn, @WillNotClose OutputStream out, String gzip)
91 throws ArchiverException {
92
93 try (InputStream in = zIn) {
94 byte[] buffer = cacheBuffer();
95 int count = 0;
96 do {
97 try {
98 out.write(buffer, 0, count);
99 } catch (IOException e) {
100 throw new ArchiverException(
101 "Problem writing to output in " + gzip + " operation " + e.getMessage());
102 }
103 count = in.read(buffer, 0, buffer.length);
104 } while (count != -1);
105 } catch (IOException e) {
106 throw new ArchiverException("Problem reading from source file in " + gzip + " operation " + e.getMessage());
107 }
108 }
109 }