OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Rev 94 | Go to most recent revision | Details | Compare with Previous | 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;
67 ilm 17
import java.io.BufferedWriter;
17 ilm 18
import java.io.File;
19
import java.io.FileOutputStream;
20
import java.io.IOException;
21
import java.io.InputStream;
22
import java.io.OutputStream;
67 ilm 23
import java.io.OutputStreamWriter;
24
import java.io.Writer;
17 ilm 25
import java.nio.charset.Charset;
26
 
27
public class StreamUtils {
28
 
29
    public static final OutputStream NULL_OS = new OutputStream() {
30
        @Override
31
        public void write(int b) throws IOException {
32
            // ignore
33
        }
34
 
35
        public void write(byte b[], int off, int len) throws IOException {
36
            if (b == null)
37
                throw new NullPointerException();
38
            // ignore
39
        }
40
    };
41
 
42
    /**
43
     * Verbatim copy an entry from input to output stream.
44
     *
45
     * @param in the source.
46
     * @param out the destination.
47
     * @throws IOException if an error occurs while reading or writing.
48
     */
49
    public static void copy(InputStream in, OutputStream out) throws IOException {
50
        copy(in, out, 512 * 1024);
51
    }
52
 
53
    public static void copy(InputStream in, OutputStream out, final int bufferSize) throws IOException {
67 ilm 54
        copy(in, out, bufferSize, -1);
55
    }
56
 
57
    public static long copy(InputStream in, OutputStream out, final int bufferSize, final long length) throws IOException {
17 ilm 58
        final byte[] buffer = new byte[bufferSize];
67 ilm 59
        long totalCount = 0;
60
        final boolean copyAll = length < 0;
61
        while (copyAll || totalCount < length) {
62
            final long toRead = copyAll ? buffer.length : Math.min(length - totalCount, buffer.length);
63
            // since buffer.length is an int
64
            assert 0 <= toRead && toRead <= Integer.MAX_VALUE;
65
            final int count = in.read(buffer, 0, (int) toRead);
17 ilm 66
            if (count == -1)
67
                break;
67 ilm 68
            totalCount += count;
17 ilm 69
            out.write(buffer, 0, count);
70
        }
67 ilm 71
        // < if end of stream
72
        assert copyAll || totalCount <= length;
73
        return totalCount;
17 ilm 74
    }
75
 
76
    public static void copy(InputStream ins, File out) throws IOException {
77
        // buffered since read() in copy(InputStream, OutputStream) may return 1 byte at a time
78
        final OutputStream ous = new BufferedOutputStream(new FileOutputStream(out));
79
        try {
80
            copy(ins, ous);
81
        } finally {
82
            ous.close();
83
        }
84
    }
85
 
86
    public static void writeln(final String s, final OutputStream out) throws IOException {
87
        write(s + "\n", out);
88
    }
89
 
90
    public static void write(final String s, final OutputStream out) throws IOException {
67 ilm 91
        write(s, out, StringUtils.UTF8);
17 ilm 92
    }
93
 
94
    public static void write(final String s, final OutputStream out, Charset charset) throws IOException {
95
        out.write(s.getBytes(charset));
96
    }
97
 
67 ilm 98
    /**
99
     * Wrap the output stream into a writer, and write the XML declaration.
100
     *
101
     * @param outs an output stream.
102
     * @return a writer with the same encoding as the XML.
103
     * @throws IOException if an error occurs.
104
     */
105
    public static BufferedWriter createXMLWriter(OutputStream outs) throws IOException {
106
        return new BufferedWriter(createXMLUnbufferedWriter(outs));
107
    }
108
 
109
    public static Writer createXMLUnbufferedWriter(OutputStream outs) throws IOException {
110
        // see http://www.w3.org/TR/REC-xml/#sec-guessing
111
        // don't use UTF-8 BOM as Java does not support it :
112
        // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
113
        final Writer res = new OutputStreamWriter(outs, StringUtils.UTF8);
114
        res.write("<?xml version='1.0' encoding='UTF-8' ?>\n");
115
        return res;
116
    }
17 ilm 117
}