• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ehrmann / vcdiff-java / 26

18 Apr 2026 05:11PM UTC coverage: 83.839% (+0.2%) from 83.679%
26

push

circleci

ehrmann
Fix Coveralls integration

663 of 814 branches covered (81.45%)

1603 of 1912 relevant lines covered (83.84%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/core/src/main/java/com/davidehrmann/vcdiff/io/IOUtils.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17

18
package com.davidehrmann.vcdiff.io;
19

20
import java.io.ByteArrayOutputStream;
21
import java.io.Closeable;
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.io.OutputStream;
25

26
/**
27
 * A subset of the Commons IOUtils class
28
 */
29
public final class IOUtils {
30

31
    private IOUtils() {}
32

33
    /**
34
     * The default buffer size ({@value}) to use for
35
     * {@link #copyLarge(InputStream, OutputStream)}
36
     */
37
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
38

39
    private static final int EOF = -1;
40

41
    /**
42
     * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
43
     * <p>
44
     * This method buffers the input internally, so there is no need to use a
45
     * <code>BufferedInputStream</code>.
46
     *
47
     * @param input  the <code>InputStream</code> to read from
48
     * @return the requested byte array
49
     * @throws NullPointerException if the input is null
50
     * @throws IOException if an I/O error occurs
51
     */
52
    public static byte[] toByteArray(InputStream input) throws IOException {
53
        ByteArrayOutputStream output = new ByteArrayOutputStream();
×
54
        copy(input, output);
×
55
        return output.toByteArray();
×
56
    }
57

58
    /**
59
     * copy bytes from an <code>InputStream</code> to an
60
     * <code>OutputStream</code>.
61
     * <p>
62
     * This method buffers the input internally, so there is no need to use a
63
     * <code>BufferedInputStream</code>.
64
     * <p>
65
     * Large streams (over 2GB) will return a bytes copied value of
66
     * <code>-1</code> after the copy has completed since the correct
67
     * number of bytes cannot be returned as an int. For large streams
68
     * use the <code>copyLarge(InputStream, OutputStream)</code> method.
69
     *
70
     * @param input  the <code>InputStream</code> to read from
71
     * @param output  the <code>OutputStream</code> to write to
72
     * @return the number of bytes copied, or -1 if &gt; Integer.MAX_VALUE
73
     * @throws NullPointerException if the input or output is null
74
     * @throws IOException if an I/O error occurs
75
     * @since 1.1
76
     */
77
    public static int copy(InputStream input, OutputStream output) throws IOException {
78
        long count = copyLarge(input, output);
×
79
        if (count > Integer.MAX_VALUE) {
×
80
            return -1;
×
81
        }
82
        return (int) count;
×
83
    }
84

85
    /**
86
     * copy bytes from a large (over 2GB) <code>InputStream</code> to an
87
     * <code>OutputStream</code>.
88
     * <p>
89
     * This method buffers the input internally, so there is no need to use a
90
     * <code>BufferedInputStream</code>.
91
     * <p>
92
     * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
93
     *
94
     * @param input  the <code>InputStream</code> to read from
95
     * @param output  the <code>OutputStream</code> to write to
96
     * @return the number of bytes copied
97
     * @throws NullPointerException if the input or output is null
98
     * @throws IOException if an I/O error occurs
99
     * @since 1.3
100
     */
101
    public static long copyLarge(InputStream input, OutputStream output)
102
            throws IOException {
103
        return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);
×
104
    }
105

106
    /**
107
     * copy bytes from a large (over 2GB) <code>InputStream</code> to an
108
     * <code>OutputStream</code>.
109
     * <p>
110
     * This method uses the provided buffer, so there is no need to use a
111
     * <code>BufferedInputStream</code>.
112
     * <p>
113
     *
114
     * @param input  the <code>InputStream</code> to read from
115
     * @param output  the <code>OutputStream</code> to write to
116
     * @param buffer the buffer to use for the copy
117
     * @return the number of bytes copied
118
     * @throws NullPointerException if the input or output is null
119
     * @throws IOException if an I/O error occurs
120
     * @since 2.2
121
     */
122
    public static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
123
            throws IOException {
124
        long count = 0;
×
125
        int n;
126
        while (EOF != (n = input.read(buffer))) {
×
127
            output.write(buffer, 0, n);
×
128
            count += n;
×
129
        }
130
        return count;
×
131
    }
132

133
    /**
134
     * Unconditionally close a <code>Closeable</code>.
135
     * <p>
136
     * Equivalent to {@link Closeable#close()}, except any exceptions will be ignored.
137
     * This is typically used in finally blocks.
138
     * <p>
139
     * Example code:
140
     * <pre>
141
     *   Closeable closeable = null;
142
     *   try {
143
     *       closeable = new FileReader("foo.txt");
144
     *       // process closeable
145
     *       closeable.close();
146
     *   } catch (Exception e) {
147
     *       // error handling
148
     *   } finally {
149
     *       IOUtils.closeQuietly(closeable);
150
     *   }
151
     * </pre>
152
     *
153
     * @param closeable the object to close, may be null or already closed
154
     * @since 2.0
155
     */
156
    public static void closeQuietly(Closeable closeable) {
157
        try {
158
            if (closeable != null) {
×
159
                closeable.close();
×
160
            }
161
        } catch (IOException ioe) {
×
162
            // ignore
163
        }
×
164
    }
×
165
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc