1 | package fr.sii.ogham.core.util; | |
2 | ||
3 | import java.io.ByteArrayInputStream; | |
4 | import java.io.File; | |
5 | import java.io.FileOutputStream; | |
6 | import java.io.IOException; | |
7 | import java.io.InputStream; | |
8 | import java.nio.charset.Charset; | |
9 | ||
10 | /** | |
11 | * Helper class for I/O management: | |
12 | * <ul> | |
13 | * <li>Read a stream and provide its content as byte array</li> | |
14 | * </ul> | |
15 | * <p> | |
16 | * This work can be done by several libraries. The aim of this class is to be | |
17 | * able to change the implementation easily to use another library for example. | |
18 | * </p> | |
19 | * <p> | |
20 | * For example, we could find which library is available in the classpath and | |
21 | * use this library instead of forcing users to include Apache Commons IO | |
22 | * library. | |
23 | * </p> | |
24 | * | |
25 | * @author Aurélien Baudet | |
26 | * | |
27 | */ | |
28 | public final class IOUtils { | |
29 | /** | |
30 | * <p> | |
31 | * Get the contents of an InputStream as a byte[]. | |
32 | * </p> | |
33 | * <p> | |
34 | * This method buffers the input internally, so there is no need to use a | |
35 | * BufferedInputStream. | |
36 | * </p> | |
37 | * | |
38 | * @param stream | |
39 | * the InputStream to read from | |
40 | * @return the requested byte array | |
41 | * @throws NullPointerException | |
42 | * if the input is null | |
43 | * @throws IOException | |
44 | * if an I/O error occurs | |
45 | */ | |
46 | public static byte[] toByteArray(InputStream stream) throws IOException { | |
47 |
6
1. toByteArray : replaced return value with null for fr/sii/ogham/core/util/IOUtils::toByteArray → NO_COVERAGE 2. toByteArray : replaced return value with null for fr/sii/ogham/core/util/IOUtils::toByteArray → TIMED_OUT 3. toByteArray : replaced return value with null for fr/sii/ogham/core/util/IOUtils::toByteArray → KILLED 4. toByteArray : replaced return value with null for fr/sii/ogham/core/util/IOUtils::toByteArray → KILLED 5. toByteArray : replaced return value with null for fr/sii/ogham/core/util/IOUtils::toByteArray → KILLED 6. toByteArray : replaced return value with null for fr/sii/ogham/core/util/IOUtils::toByteArray → KILLED |
return org.apache.commons.io.IOUtils.toByteArray(stream); |
48 | } | |
49 | ||
50 | /** | |
51 | * <p> | |
52 | * Get the contents of an InputStream as a String using the default | |
53 | * character encoding of the platform. | |
54 | * </p> | |
55 | * <p> | |
56 | * This method buffers the input internally, so there is no need to use a | |
57 | * BufferedInputStream. | |
58 | * </p> | |
59 | * | |
60 | * @param stream | |
61 | * the InputStream to read from | |
62 | * @return the content of the stream as string | |
63 | * @throws NullPointerException | |
64 | * if the input is null | |
65 | * @throws IOException | |
66 | * when the stream can't be read | |
67 | */ | |
68 | public static String toString(InputStream stream) throws IOException { | |
69 |
4
1. toString : replaced return value with "" for fr/sii/ogham/core/util/IOUtils::toString → NO_COVERAGE 2. toString : replaced return value with "" for fr/sii/ogham/core/util/IOUtils::toString → KILLED 3. toString : replaced return value with "" for fr/sii/ogham/core/util/IOUtils::toString → KILLED 4. toString : replaced return value with "" for fr/sii/ogham/core/util/IOUtils::toString → KILLED |
return toString(stream, Charset.defaultCharset()); |
70 | } | |
71 | ||
72 | /** | |
73 | * <p> | |
74 | * Get the contents of an InputStream as a String using the default | |
75 | * character encoding of the platform. | |
76 | * </p> | |
77 | * <p> | |
78 | * This method buffers the input internally, so there is no need to use a | |
79 | * BufferedInputStream. | |
80 | * </p> | |
81 | * | |
82 | * @param stream | |
83 | * the InputStream to read from | |
84 | * @param charset | |
85 | * the charset to use | |
86 | * @return the content of the stream as string | |
87 | * @throws NullPointerException | |
88 | * if the input is null | |
89 | * @throws IOException | |
90 | * when the stream can't be read | |
91 | */ | |
92 | public static String toString(InputStream stream, Charset charset) throws IOException { | |
93 |
4
1. toString : replaced return value with "" for fr/sii/ogham/core/util/IOUtils::toString → NO_COVERAGE 2. toString : replaced return value with "" for fr/sii/ogham/core/util/IOUtils::toString → KILLED 3. toString : replaced return value with "" for fr/sii/ogham/core/util/IOUtils::toString → KILLED 4. toString : replaced return value with "" for fr/sii/ogham/core/util/IOUtils::toString → KILLED |
return org.apache.commons.io.IOUtils.toString(stream, charset); |
94 | } | |
95 | ||
96 | public static void copy(byte[] buf, File file) throws IOException { | |
97 | try(ByteArrayInputStream input = new ByteArrayInputStream(buf); FileOutputStream output = new FileOutputStream(file)) { | |
98 | org.apache.commons.io.IOUtils.copy(input, output); | |
99 | } | |
100 | } | |
101 | | |
102 | private IOUtils() { | |
103 | super(); | |
104 | } | |
105 | } | |
Mutations | ||
47 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
69 |
1.1 2.2 3.3 4.4 |
|
93 |
1.1 2.2 3.3 4.4 |