-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathBOMReader.java
69 lines (60 loc) · 1.61 KB
/
BOMReader.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
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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel.commons;
import java.io.IOException;
import java.io.Reader;
/**
* Wraps a given reader and removes a Unicode BOM (byte order mark) if present.
* <p>
* As Java sadly is incapable of handling these bytes, we discard them here. Note that in order to write a BOM,
* {@link Streams#UNICODE_BOM_CHARACTER} can be used.
*
* @see CSVWriter#writeUnicodeBOM()
*/
public class BOMReader extends Reader {
private boolean bomSkipped = false;
private final Reader delegate;
/**
* Creates a new reader wrapping the given one.
*
* @param reader the reader to wrap (and filter)
*/
public BOMReader(Reader reader) {
this.delegate = reader;
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
if (bomSkipped) {
return delegate.read(cbuf, off, len);
}
if (len == 0) {
return 0;
}
int c = delegate.read();
if (Character.getType(c) == Character.FORMAT) {
c = delegate.read();
}
bomSkipped = true;
if (c == 0) {
return 0;
}
cbuf[off++] = (char) c;
if (len == 1) {
return 1;
}
return 1 + delegate.read(cbuf, off, len - 1);
}
@Override
public boolean ready() throws IOException {
return delegate.ready();
}
@Override
public void close() throws IOException {
delegate.close();
}
}