OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 177 Rev 180
Line 47... Line 47...
47
     * Verbatim copy an entry from input to output stream.
47
     * Verbatim copy an entry from input to output stream.
48
     * 
48
     * 
49
     * @param in the source.
49
     * @param in the source.
50
     * @param out the destination.
50
     * @param out the destination.
51
     * @throws IOException if an error occurs while reading or writing.
51
     * @throws IOException if an error occurs while reading or writing.
-
 
52
     * @throws RTInterruptedException if interrupted (can only be checked between
-
 
53
     *         {@link InputStream#read(byte[], int, int)} reads).
52
     */
54
     */
53
    public static void copy(InputStream in, OutputStream out) throws IOException {
55
    public static void copy(InputStream in, OutputStream out) throws IOException {
54
        // TODO use in.transferTo(out) in Java 9
56
        // TODO use in.transferTo(out) in Java 9
55
        copy(in, out, 512 * 1024);
57
        copy(in, out, 512 * 1024);
56
    }
58
    }
Line 67... Line 69...
67
        final boolean copyAll = length < 0;
69
        final boolean copyAll = length < 0;
68
        while (copyAll || totalCount < length) {
70
        while (copyAll || totalCount < length) {
69
            final long toRead = copyAll ? buffer.length : Math.min(length - totalCount, buffer.length);
71
            final long toRead = copyAll ? buffer.length : Math.min(length - totalCount, buffer.length);
70
            // since buffer.length is an int
72
            // since buffer.length is an int
71
            assert 0 < toRead && toRead <= Integer.MAX_VALUE;
73
            assert 0 < toRead && toRead <= Integer.MAX_VALUE;
-
 
74
            if (Thread.interrupted())
-
 
75
                throw new RTInterruptedException();
72
            final int count = in.read(buffer, 0, (int) toRead);
76
            final int count = in.read(buffer, 0, (int) toRead);
73
            if (count <= 0) {
77
            if (count <= 0) {
74
                // like Files.copy(InputStream, OutputStream), stop if reading 0 bytes
78
                // like Files.copy(InputStream, OutputStream), stop if reading 0 bytes
75
                if (count == 0)
79
                if (count == 0)
76
                    Log.get().log(Level.WARNING, "", new IllegalStateException("read() returned 0 for " + in));
80
                    Log.get().log(Level.WARNING, "", new IllegalStateException("read() returned 0 for " + in));
77
                break;
81
                break;
78
            }
82
            }
79
            totalCount += count;
83
            totalCount += count;
-
 
84
            // if interrupted while blocked in read(), then we don't want to use the data read after
-
 
85
            // the interrupt.
-
 
86
            if (Thread.interrupted())
-
 
87
                throw new RTInterruptedException();
80
            out.write(buffer, 0, count);
88
            out.write(buffer, 0, count);
81
        }
89
        }
82
        // < if end of stream
90
        // < if end of stream
83
        assert copyAll || totalCount <= length;
91
        assert copyAll || totalCount <= length;
84
        return totalCount;
92
        return totalCount;