-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringOutputStream.java
42 lines (35 loc) · 1.13 KB
/
StringOutputStream.java
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
package com.tazkiyatech.utils.streams;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import androidx.annotation.NonNull;
/**
* Provides an easy method for writing a {@link String} to an {@link OutputStream}.
*/
public class StringOutputStream implements AutoCloseable {
@NonNull
private final OutputStream outputStream;
/**
* Constructor.
*
* @param outputStream the {@link OutputStream} instance to write to.
*/
public StringOutputStream(@NonNull OutputStream outputStream) {
this.outputStream = outputStream;
}
/**
* Writes {@code value} to the {@link OutputStream} instance that this class wraps.
*
* @param value the {@link String} value to write to the output stream.
* @throws IOException if an I/O error occurs.
*/
public void write(@NonNull String value) throws IOException {
outputStream.write(value.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
}
@Override
public void close() throws IOException {
outputStream.flush();
outputStream.close();
}
}