/
InputStreamHelper.java
227 lines (207 loc) · 6.73 KB
/
InputStreamHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* file: InputStreamHelper.java
* author: Jon Iles
* copyright: (c) Packwood Software 2016
* date: 06/06/2016
*/
/*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
package net.sf.mpxj.common;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
/**
* Helper methods for dealing with InputStreams.
*/
public class InputStreamHelper
{
/**
* Copy the data from an InputStream to a temp file.
*
* @param inputStream data source
* @param tempFileSuffix suffix to use for temp file
* @return File instance
*/
public static File writeStreamToTempFile(InputStream inputStream, String tempFileSuffix) throws IOException
{
FileOutputStream outputStream = null;
try
{
File file = Files.createTempFile("mpxj", tempFileSuffix).toFile();
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while (true)
{
int bytesRead = inputStream.read(buffer);
if (bytesRead == -1)
{
break;
}
outputStream.write(buffer, 0, bytesRead);
}
return file;
}
finally
{
if (outputStream != null)
{
outputStream.close();
}
}
}
/**
* Expands a zip file input stream into a temporary directory.
*
* @param inputStream zip file input stream
* @return File instance representing the temporary directory
*/
public static File writeZipStreamToTempDir(InputStream inputStream) throws IOException
{
File dir = FileHelper.createTempDir();
try
{
processZipStream(dir, inputStream);
}
catch (ZipException ex)
{
// Java doesn't support zip files with zero byte entries.
// We could use a different library which does handle these zip files, but
// I'm reluctant to add new dependencies just for this. Rather than
// propagating the error, we'll just stop at this point and see if we
// can make sense of anything we have extracted from the zip file so far.
// For what it's worth I haven't come across a valid compressed schedule file
// which includes zero bytes files.
if (!ex.getMessage().equals("only DEFLATED entries can have EXT descriptor"))
{
throw ex;
}
}
return dir;
}
/**
* Reads a specified number of bytes from the input stream and populates
* a new byte array. If the required number of bytes can't be read
* an exception will be raised.
*
* @param is InputStream instance
* @param size number of bytes to read
* @return new byte array instance
*/
public static byte[] read(InputStream is, int size) throws IOException
{
return read(is, new byte[size]);
}
/**
* Read bytes from the input stream to fill the provided array.
* If the array can't be filled an exception will be raised.
*
* @param is InputStream instance
* @param data array to fill
* @return filled byte array
*/
public static byte[] read(InputStream is, byte[] data) throws IOException
{
return read(is, data, data.length);
}
/**
* Read bytes from the input stream to fill the provided array.
* If the array can't be filled with the requested number of bytes,
* an exception will be raised.
*
* @param is InputStream instance
* @param data array to fill
* @param size number of bytes to read
* @return filled byte array
*/
public static byte[] read(InputStream is, byte[] data, int size) throws IOException
{
int bytesRead = is.read(data, 0, size);
if (bytesRead != size)
{
throw new RuntimeException("Unable to read the required number of bytes");
}
return data;
}
/**
* Expands a zip file input stream into a temporary directory.
*
* @param dir temporary directory
* @param inputStream zip file input stream
*/
private static void processZipStream(File dir, InputStream inputStream) throws IOException
{
String canonicalDestinationDirPath = dir.getCanonicalPath();
ZipInputStream zip = new ZipInputStream(inputStream);
while (true)
{
ZipEntry entry = zip.getNextEntry();
if (entry == null)
{
break;
}
File file = new File(dir, entry.getName());
// https://snyk.io/research/zip-slip-vulnerability
String canonicalDestinationFile = file.getCanonicalPath();
if (!canonicalDestinationFile.startsWith(canonicalDestinationDirPath + File.separator))
{
throw new IOException("Entry is outside of the target dir: " + entry.getName());
}
if (entry.isDirectory())
{
FileHelper.mkdirsQuietly(file);
continue;
}
File parent = file.getParentFile();
if (parent != null)
{
FileHelper.mkdirsQuietly(parent);
}
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = zip.read(bytes)) >= 0)
{
fos.write(bytes, 0, length);
}
fos.close();
}
}
/**
* The documentation for {@code InputStream.skip} indicates that it can bail out early, and not skip
* the requested number of bytes. I've encountered this in practice, hence this helper method.
*
* @param stream InputStream instance
* @param skip number of bytes to skip
*/
public static void skip(InputStream stream, long skip) throws IOException
{
long count = skip;
while (count > 0)
{
long skipped = stream.skip(count);
if (skipped == 0)
{
throw new IOException("Cannot skip forward within InputStream");
}
count -= skipped;
}
}
}