OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 67 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils;
15
 
16
import java.io.BufferedOutputStream;
17
import java.io.File;
18
import java.io.FileOutputStream;
19
import java.io.IOException;
20
import java.io.InputStream;
21
import java.io.OutputStream;
22
import java.nio.charset.Charset;
23
 
24
public class StreamUtils {
25
 
26
    public static final OutputStream NULL_OS = new OutputStream() {
27
        @Override
28
        public void write(int b) throws IOException {
29
            // ignore
30
        }
31
 
32
        public void write(byte b[], int off, int len) throws IOException {
33
            if (b == null)
34
                throw new NullPointerException();
35
            // ignore
36
        }
37
    };
38
    private static final Charset UTF8 = Charset.forName("UTF-8");
39
 
40
    /**
41
     * Verbatim copy an entry from input to output stream.
42
     *
43
     * @param in the source.
44
     * @param out the destination.
45
     * @throws IOException if an error occurs while reading or writing.
46
     */
47
    public static void copy(InputStream in, OutputStream out) throws IOException {
48
        copy(in, out, 512 * 1024);
49
    }
50
 
51
    public static void copy(InputStream in, OutputStream out, final int bufferSize) throws IOException {
52
        final byte[] buffer = new byte[bufferSize];
53
        while (true) {
54
            int count = in.read(buffer);
55
            if (count == -1)
56
                break;
57
            out.write(buffer, 0, count);
58
        }
59
    }
60
 
61
    public static void copy(InputStream ins, File out) throws IOException {
62
        // buffered since read() in copy(InputStream, OutputStream) may return 1 byte at a time
63
        final OutputStream ous = new BufferedOutputStream(new FileOutputStream(out));
64
        try {
65
            copy(ins, ous);
66
        } finally {
67
            ous.close();
68
        }
69
    }
70
 
71
    public static void writeln(final String s, final OutputStream out) throws IOException {
72
        write(s + "\n", out);
73
    }
74
 
75
    public static void write(final String s, final OutputStream out) throws IOException {
76
        write(s, out, UTF8);
77
    }
78
 
79
    public static void write(final String s, final OutputStream out, Charset charset) throws IOException {
80
        out.write(s.getBytes(charset));
81
    }
82
 
83
}