OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 94 Rev 156
Line 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils;
14
 package org.openconcerto.utils;
15
 
15
 
16
import java.io.BufferedOutputStream;
-
 
17
import java.io.BufferedWriter;
16
import java.io.BufferedWriter;
18
import java.io.ByteArrayOutputStream;
17
import java.io.ByteArrayOutputStream;
19
import java.io.File;
18
import java.io.File;
20
import java.io.FileOutputStream;
-
 
21
import java.io.IOException;
19
import java.io.IOException;
22
import java.io.InputStream;
20
import java.io.InputStream;
23
import java.io.OutputStream;
21
import java.io.OutputStream;
24
import java.io.OutputStreamWriter;
22
import java.io.OutputStreamWriter;
25
import java.io.Writer;
23
import java.io.Writer;
26
import java.nio.charset.Charset;
24
import java.nio.charset.Charset;
-
 
25
import java.nio.file.Files;
-
 
26
import java.nio.file.StandardCopyOption;
-
 
27
import java.util.logging.Level;
27
 
28
 
28
public class StreamUtils {
29
public class StreamUtils {
29
 
30
 
30
    public static final OutputStream NULL_OS = new OutputStream() {
31
    public static final OutputStream NULL_OS = new OutputStream() {
31
        @Override
32
        @Override
Line 46... Line 47...
46
     * @param in the source.
47
     * @param in the source.
47
     * @param out the destination.
48
     * @param out the destination.
48
     * @throws IOException if an error occurs while reading or writing.
49
     * @throws IOException if an error occurs while reading or writing.
49
     */
50
     */
50
    public static void copy(InputStream in, OutputStream out) throws IOException {
51
    public static void copy(InputStream in, OutputStream out) throws IOException {
-
 
52
        // TODO use in.transferTo(out) in Java 9
51
        copy(in, out, 512 * 1024);
53
        copy(in, out, 512 * 1024);
52
    }
54
    }
53
 
55
 
54
    public static void copy(InputStream in, OutputStream out, final int bufferSize) throws IOException {
56
    public static void copy(InputStream in, OutputStream out, final int bufferSize) throws IOException {
55
        copy(in, out, bufferSize, -1);
57
        copy(in, out, bufferSize, -1);
56
    }
58
    }
57
 
59
 
58
    public static long copy(InputStream in, OutputStream out, final int bufferSize, final long length) throws IOException {
60
    public static long copy(InputStream in, OutputStream out, final int bufferSize, final long length) throws IOException {
-
 
61
        if (bufferSize < 1)
-
 
62
            throw new IllegalArgumentException("Buffer size too small : " + bufferSize);
59
        final byte[] buffer = new byte[bufferSize];
63
        final byte[] buffer = new byte[bufferSize];
60
        long totalCount = 0;
64
        long totalCount = 0;
61
        final boolean copyAll = length < 0;
65
        final boolean copyAll = length < 0;
62
        while (copyAll || totalCount < length) {
66
        while (copyAll || totalCount < length) {
63
            final long toRead = copyAll ? buffer.length : Math.min(length - totalCount, buffer.length);
67
            final long toRead = copyAll ? buffer.length : Math.min(length - totalCount, buffer.length);
64
            // since buffer.length is an int
68
            // since buffer.length is an int
65
            assert 0 <= toRead && toRead <= Integer.MAX_VALUE;
69
            assert 0 < toRead && toRead <= Integer.MAX_VALUE;
66
            final int count = in.read(buffer, 0, (int) toRead);
70
            final int count = in.read(buffer, 0, (int) toRead);
67
            if (count == -1)
71
            if (count <= 0) {
-
 
72
                // like Files.copy(InputStream, OutputStream), stop if reading 0 bytes
-
 
73
                if (count == 0)
-
 
74
                    Log.get().log(Level.WARNING, "", new IllegalStateException("read() returned 0 for " + in));
68
                break;
75
                break;
-
 
76
            }
69
            totalCount += count;
77
            totalCount += count;
70
            out.write(buffer, 0, count);
78
            out.write(buffer, 0, count);
71
        }
79
        }
72
        // < if end of stream
80
        // < if end of stream
73
        assert copyAll || totalCount <= length;
81
        assert copyAll || totalCount <= length;
74
        return totalCount;
82
        return totalCount;
75
    }
83
    }
76
 
84
 
77
    public static void copy(InputStream ins, File out) throws IOException {
85
    public static void copy(InputStream ins, File out) throws IOException {
78
        // buffered since read() in copy(InputStream, OutputStream) may return 1 byte at a time
-
 
79
        final OutputStream ous = new BufferedOutputStream(new FileOutputStream(out));
86
        Files.copy(ins, out.toPath(), StandardCopyOption.REPLACE_EXISTING);
80
        try {
-
 
81
            copy(ins, ous);
-
 
82
        } finally {
-
 
83
            ous.close();
-
 
84
        }
-
 
85
    }
87
    }
86
 
88
 
87
    /**
89
    /**
88
     * Read until the end of the stream is reached. NOTE : since this method didn't create the
90
     * Read until the end of the stream is reached. NOTE : since this method didn't create the
89
     * stream, it doesn't close it.
91
     * stream, it doesn't close it.