linwe
2024-07-29 aeb7d068be92312dcdcea75e1240bcf2a78dd0fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2012 All Rights Reserved.
 */
package org.springblade.common.utils.sms;
 
import java.io.*;
 
/**
 *
 * @author runzhi
 */
public class StreamUtil {
    private StreamUtil(){}
 
    private static final int DEFAULT_BUFFER_SIZE = 8192;
 
    public static void io(InputStream in, OutputStream out) throws IOException {
        io(in, out, -1);
    }
 
    public static void io(InputStream in, OutputStream out, int bufferSize) throws IOException {
        if (bufferSize == -1) {
            bufferSize = DEFAULT_BUFFER_SIZE;
        }
 
        byte[] buffer = new byte[bufferSize];
        int amount;
 
        while ((amount = in.read(buffer)) >= 0) {
            out.write(buffer, 0, amount);
        }
    }
 
    public static void io(Reader in, Writer out) throws IOException {
        io(in, out, -1);
    }
 
    public static void io(Reader in, Writer out, int bufferSize) throws IOException {
        if (bufferSize == -1) {
            bufferSize = DEFAULT_BUFFER_SIZE >> 1;
        }
 
        char[] buffer = new char[bufferSize];
        int amount;
 
        while ((amount = in.read(buffer)) >= 0) {
            out.write(buffer, 0, amount);
        }
    }
 
    public static OutputStream synchronizedOutputStream(OutputStream out) {
        return new SynchronizedOutputStream(out);
    }
 
    public static OutputStream synchronizedOutputStream(OutputStream out, Object lock) {
        return new SynchronizedOutputStream(out, lock);
    }
 
    public static String readText(InputStream in) throws IOException {
        return readText(in, null, -1);
    }
 
    public static String readText(InputStream in, String encoding) throws IOException {
        return readText(in, encoding, -1);
    }
 
    public static String readText(InputStream in, String encoding, int bufferSize)
                                                                                  throws IOException {
        Reader reader = (encoding == null) ? new InputStreamReader(in) : new InputStreamReader(in,
            encoding);
 
        return readText(reader, bufferSize);
    }
 
    public static String readText(Reader reader) throws IOException {
        return readText(reader, -1);
    }
 
    public static String readText(Reader reader, int bufferSize) throws IOException {
        StringWriter writer = new StringWriter();
 
        io(reader, writer, bufferSize);
        return writer.toString();
    }
 
    private static class SynchronizedOutputStream extends OutputStream {
        private OutputStream out;
        private Object       lock;
 
        SynchronizedOutputStream(OutputStream out) {
            this(out, out);
        }
 
        SynchronizedOutputStream(OutputStream out, Object lock) {
            this.out = out;
            this.lock = lock;
        }
 
        @Override
        public void write(int datum) throws IOException {
            synchronized (lock) {
                out.write(datum);
            }
        }
 
        @Override
        public void write(byte[] data) throws IOException {
            synchronized (lock) {
                out.write(data);
            }
        }
 
        @Override
        public void write(byte[] data, int offset, int length) throws IOException {
            synchronized (lock) {
                out.write(data, offset, length);
            }
        }
 
        @Override
        public void flush() throws IOException {
            synchronized (lock) {
                out.flush();
            }
        }
 
        @Override
        public void close() throws IOException {
            synchronized (lock) {
                out.close();
            }
        }
    }
}