~ubuntu-branches/ubuntu/utopic/libcommons-compress-java/utopic

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/compress/utils/IOUtils.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-11-02 13:55:47 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20131102135547-nhcubj3ae7rv6t13
Tags: 1.6-1
* New upstream release
* Updated Standards-Version to 3.9.5 (no changes)
* Build depend on debhelper >= 9
* Removed the unused debian/orig-tar.sh script

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
        }
71
71
        return count;
72
72
    }
73
 
 
 
73
    
 
74
    /**
 
75
     * Skips the given number of bytes by repeatedly invoking skip on
 
76
     * the given input stream if necessary.
 
77
     *
 
78
     * <p>This method will only skip less than the requested number of
 
79
     * bytes if the end of the input stream has been reached.</p>
 
80
     *
 
81
     * @param input stream to skip bytes in
 
82
     * @param numToSkip the number of bytes to skip
 
83
     * @return the number of bytes actually skipped
 
84
     * @throws IOException
 
85
     */
 
86
    public static long skip(InputStream input, long numToSkip) throws IOException {
 
87
        long available = numToSkip;
 
88
        while (numToSkip > 0) {
 
89
            long skipped = input.skip(numToSkip);
 
90
            if (skipped == 0) {
 
91
                break;
 
92
            }
 
93
            numToSkip -= skipped;
 
94
        }
 
95
        return (available - numToSkip);
 
96
    }
 
97
 
 
98
    /**
 
99
     * Reads as much from input as possible to fill the given array.
 
100
     *
 
101
     * <p>This method may invoke read repeatedly to fill the array and
 
102
     * only read less bytes than the length of the array if the end of
 
103
     * the stream has been reached.</p>
 
104
     *
 
105
     * @param input stream to read from
 
106
     * @param b buffer to fill
 
107
     * @return the number of bytes actually read
 
108
     * @throws IOException
 
109
     */
 
110
    public static int readFully(InputStream input, byte[] b) throws IOException {
 
111
        return readFully(input, b, 0, b.length);
 
112
    }
 
113
 
 
114
    /**
 
115
     * Reads as much from input as possible to fill the given array
 
116
     * with the given amount of bytes.
 
117
     *
 
118
     * <p>This method may invoke read repeatedly to read the bytes and
 
119
     * only read less bytes than the requested length if the end of
 
120
     * the stream has been reached.</p>
 
121
     *
 
122
     * @param input stream to read from
 
123
     * @param b buffer to fill
 
124
     * @param offset offset into the buffer to start filling at
 
125
     * @param len of bytes to read
 
126
     * @return the number of bytes actually read
 
127
     * @throws IOException
 
128
     *             if an I/O error has occurred
 
129
     */
 
130
    public static int readFully(InputStream input, byte[] b, int offset, int len)
 
131
        throws IOException {
 
132
        if (len < 0 || offset < 0 || len + offset > b.length) {
 
133
            throw new IndexOutOfBoundsException();
 
134
        }
 
135
        int count = 0, x = 0;
 
136
        while (count != len) {
 
137
            x = input.read(b, offset + count, len - count);
 
138
            if (x == -1) {
 
139
                break;
 
140
            }
 
141
            count += x;
 
142
        }
 
143
        return count;
 
144
    }
74
145
 
75
146
    // toByteArray(InputStream) copied from:
76
147
    // commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?revision=1428941