-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathLineBufferedWriter.java
More file actions
46 lines (38 loc) · 1.05 KB
/
LineBufferedWriter.java
File metadata and controls
46 lines (38 loc) · 1.05 KB
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
/* Copyright (c) 2007 Jython Developers */
package org.python.core.io;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
/**
* Line buffering for a writable sequential RawIO object.
*
* @author Philip Jenvey
*/
public class LineBufferedWriter extends BufferedWriter {
/**
* Construct a LineBufferedWriter wrapping the given RawIOBase.
*
* @param rawIO {@inheritDoc}
*/
public LineBufferedWriter(RawIOBase rawIO) {
super(rawIO, 0);
buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
}
@Override
public int write(ByteBuffer bytes) {
int size = bytes.remaining();
while (bytes.hasRemaining()) {
byte next = bytes.get();
try {
buffer.put(next);
} catch (BufferOverflowException boe) {
// buffer is full; we *must* flush
flush();
buffer.put(next);
}
if (next == LF_BYTE) {
flush();
}
}
return size;
}
}