1 | /*- | |
2 | * #%L | |
3 | * io.earcam.instrumental.io | |
4 | * %% | |
5 | * Copyright (C) 2018 earcam | |
6 | * %% | |
7 | * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT) | |
8 | * | |
9 | * You <b>must</b> choose to accept, in full - any individual or combination of | |
10 | * the following licenses: | |
11 | * <ul> | |
12 | * <li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li> | |
13 | * <li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li> | |
14 | * <li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li> | |
15 | * <li><a href="https://opensource.org/licenses/MIT">MIT</a></li> | |
16 | * </ul> | |
17 | * #L% | |
18 | */ | |
19 | package io.earcam.utilitarian.io; | |
20 | ||
21 | import java.io.ByteArrayOutputStream; | |
22 | import java.io.IOException; | |
23 | import java.io.InputStream; | |
24 | import java.io.OutputStream; | |
25 | import java.io.UncheckedIOException; | |
26 | import java.util.Objects; | |
27 | ||
28 | import javax.annotation.ParametersAreNonnullByDefault; | |
29 | import javax.annotation.WillNotClose; | |
30 | ||
31 | @ParametersAreNonnullByDefault | |
32 | public final class IoStreams { | |
33 | ||
34 | private static final int BUFFER_SIZE = 8192; | |
35 | ||
36 | ||
37 | private IoStreams() | |
38 | {} | |
39 | ||
40 | ||
41 | /** | |
42 | * While waiting for Java9's {@code InputStream.readAllBytes()} | |
43 | * | |
44 | * @param input the stream to drain | |
45 | * @return the byte array obtained from draining {@code input} | |
46 | * @throws UncheckedIOException if an {@link IOException} this thrown | |
47 | */ | |
48 | public static byte[] readAllBytes(@WillNotClose InputStream input) | |
49 | { | |
50 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
51 | transfer(input, baos); | |
52 |
1
1. readAllBytes : mutated return of Object value for io/earcam/utilitarian/io/IoStreams::readAllBytes to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return baos.toByteArray(); |
53 | } | |
54 | ||
55 | ||
56 | /** | |
57 | * While waiting for Java9's {@code InputStream.transferTo(OutputStream)} | |
58 | * | |
59 | * @param input read from | |
60 | * @param output write to, but does not close | |
61 | * @return the total number of bytes transferred | |
62 | * @throws UncheckedIOException if an {@link IOException} this thrown | |
63 | */ | |
64 | public static long transfer(@WillNotClose InputStream in, @WillNotClose OutputStream out) | |
65 | { | |
66 | Objects.requireNonNull(in, "in"); | |
67 | Objects.requireNonNull(out, "out"); | |
68 | ||
69 | byte[] buffer = new byte[BUFFER_SIZE]; | |
70 | long count = 0; | |
71 | int read; | |
72 | try { | |
73 |
1
1. transfer : negated conditional → KILLED |
while((read = in.read(buffer, 0, BUFFER_SIZE)) != -1) { |
74 |
1
1. transfer : removed call to java/io/OutputStream::write → KILLED |
out.write(buffer, 0, read); |
75 |
1
1. transfer : Replaced long addition with subtraction → KILLED |
count += read; |
76 | } | |
77 |
1
1. transfer : replaced return of long value with value + 1 for io/earcam/utilitarian/io/IoStreams::transfer → KILLED |
return count; |
78 | } catch(IOException e) { | |
79 | throw new UncheckedIOException(e); | |
80 | } | |
81 | } | |
82 | } | |
Mutations | ||
52 |
1.1 |
|
73 |
1.1 |
|
74 |
1.1 |
|
75 |
1.1 |
|
77 |
1.1 |