-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathBufferedWriter.java
More file actions
135 lines (114 loc) · 3.26 KB
/
Copy pathBufferedWriter.java
File metadata and controls
135 lines (114 loc) · 3.26 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
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
/* Copyright (c) 2007 Jython Developers */
package org.python.core.io;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
/**
* Buffer for a writable sequential RawIO object.
*
* @author Philip Jenvey
*/
public class BufferedWriter extends BufferedIOMixin {
/** The underlying buffer */
protected ByteBuffer buffer;
/**
* Construct a BufferedWriter of bufferSize, wrapping the given
* RawIOBase.
*
* @param rawIO {@inheritDoc}
* @param bufferSize {@inheritDoc}
*/
public BufferedWriter(RawIOBase rawIO, int bufferSize) {
super(rawIO, bufferSize);
rawIO.checkWritable();
buffer = ByteBuffer.allocate(this.bufferSize);
}
@Override
public int write(ByteBuffer bytes) {
if (bufferSize == 0) {
return rawIO.write(bytes);
}
int bytesSize = bytes.remaining();
// The total amount of data on hand; the buffer plus 'bytes'
int total = buffer.position() + bytesSize;
if (total < bufferSize) {
// Have less than bufferSize on hand: just buffer
buffer.put(bytes);
return bytesSize;
}
// The amount of 'bytes' leftover, to buffer, after writing in
// bufferSize chunks
int toBuffer = total % bufferSize;
// The amount of 'bytes' to be written
int bytesToWrite = bytesSize - toBuffer;
int origBytesLimit = bytes.limit();
bytes.limit(bytes.position() + bytesToWrite);
int totalToWrite = total - toBuffer;
int count = totalToWrite;
ByteBuffer[] bulk = new ByteBuffer[] {buffer, bytes};
// Prepare the buffer for writing
buffer.flip();
while (count > 0) {
count -= rawIO.write(bulk);
}
// Prepare the buffer for buffering
buffer.clear();
if (toBuffer > 0) {
bytes.limit(origBytesLimit);
buffer.put(bytes);
}
return totalToWrite;
}
@Override
public Channel getChannel() {
return rawIO.getChannel();
}
@Override
public void flush() {
if (buffer.position() > 0) {
buffer.flip();
while (buffer.hasRemaining()) {
rawIO.write(buffer);
}
buffer.clear();
}
super.flush();
}
@Override
public long tell() {
return rawIO.tell() + buffer.position();
}
@Override
public long seek(long pos, int whence) {
flush();
return rawIO.seek(pos, whence);
}
@Override
public boolean buffered() {
return buffer.position() > 0;
}
@Override
public ByteBuffer readall() {
// Never readable; just raise the appropriate exception
checkClosed();
checkReadable();
return null;
}
@Override
public int readinto(ByteBuffer bytes) {
// Never readable; just raise the appropriate exception
checkClosed();
checkReadable();
return -1;
}
@Override
public int read1(ByteBuffer bytes) {
// Never readable; just raise the appropriate exception
checkClosed();
checkReadable();
return -1;
}
@Override
public boolean readable() {
return false;
}
}