OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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