-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathStreamIO.java
More file actions
282 lines (251 loc) · 8.01 KB
/
StreamIO.java
File metadata and controls
282 lines (251 loc) · 8.01 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Copyright (c) 2007 Jython Developers */
package org.python.core.io;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.channels.spi.AbstractInterruptibleChannel;
import org.python.core.Py;
import org.python.modules.posix.PosixModule;
/**
* Raw I/O implementation for simple streams.
*
* Supports Input/Outputstreams and Readable/WritableByteChannels.
*
* @author Philip Jenvey
*/
public class StreamIO extends RawIOBase {
/** The underlying read channel */
private ReadableByteChannel readChannel;
/** The underlying write channel */
private WritableByteChannel writeChannel;
/** The original InputStream if one was passed in, used for
* __tojava__ */
private InputStream inputStream;
/** The original OutputStream if one was passed in, used for
* __tojava__ */
private OutputStream outputStream;
/** true if the underlying file is actually closed on close() */
private boolean closefd;
/**
* Construct a StreamIO for the given read channel.
*
* @param readChannel a ReadableByteChannel
* @param closefd boolean whether the underlying file is closed on
* close() (defaults to True)
*/
public StreamIO(ReadableByteChannel readChannel, boolean closefd) {
this.readChannel = readChannel;
this.closefd = closefd;
}
/**
* Construct a StreamIO for the given read channel.
*
* @param readChannel a ReadableByteChannel
*/
public StreamIO(ReadableByteChannel readChannel) {
this(readChannel, true);
}
/**
* Construct a StreamIO for the given write channel.
*
* @param writeChannel a WritableByteChannel
* @param closefd boolean whether the underlying file is closed on
* close() (defaults to True)
*/
public StreamIO(WritableByteChannel writeChannel, boolean closefd) {
this.writeChannel = writeChannel;
this.closefd = closefd;
}
/**
* Construct a StreamIO for the given write channel.
*
* @param writeChannel a WritableByteChannel
*/
public StreamIO(WritableByteChannel writeChannel) {
this(writeChannel, true);
}
/**
* Construct a StreamIO for the given read/write streams.
*
* @param inputStream an InputStream
* @param closefd boolean whether the underlying file is closed on
* close() (defaults to True)
*/
public StreamIO(InputStream inputStream, boolean closefd) {
this(newChannel(inputStream), closefd);
this.inputStream = inputStream;
}
/**
* Construct a StreamIO for the given read/write streams.
*
* @param outputStream an OutputStream
* @param closefd boolean whether the underlying file is closed on
* close() (defaults to True)
*/
public StreamIO(OutputStream outputStream, boolean closefd) {
//XXX: It turns out we needed to write our own channel for
// InputStreams. Do we need to do the same for OutputStreams?
this(Channels.newChannel(outputStream), closefd);
this.outputStream = outputStream;
}
@Override
public int readinto(ByteBuffer buf) {
checkClosed();
checkReadable();
try {
return readChannel.read(buf);
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
}
@Override
public int write(ByteBuffer buf) {
checkClosed();
checkWritable();
try {
return writeChannel.write(buf);
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
}
@Override
public void flush() {
if (outputStream == null) {
return;
}
try {
outputStream.flush();
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
}
@Override
public void close() {
if (closed()) {
return;
}
if (closefd) {
try {
if (readChannel != null) {
readChannel.close();
if (writeChannel != null && readChannel != writeChannel) {
writeChannel.close();
}
} else {
writeChannel.close();
}
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
}
super.close();
}
/** Unwrap one or more nested FilterInputStreams. */
private static FileDescriptor getInputFileDescriptor(InputStream stream) throws IOException {
if (stream == null) {
return null;
}
if (stream instanceof FileInputStream) {
return ((FileInputStream)stream).getFD();
}
if (stream instanceof FilterInputStream) {
Field inField = null;
try {
inField = FilterInputStream.class.getDeclaredField("in");
inField.setAccessible(true);
return getInputFileDescriptor((InputStream)inField.get(stream));
} catch (Exception e) {
// XXX: masking other exceptions
} finally {
if (inField != null && inField.isAccessible()) {
inField.setAccessible(false);
}
}
}
return null;
}
/** Unwrap one or more nested FilterOutputStreams. */
private static FileDescriptor getOutputFileDescriptor(OutputStream stream) throws IOException {
if (stream == null) {
return null;
}
if (stream instanceof FileOutputStream) {
return ((FileOutputStream)stream).getFD();
}
if (stream instanceof FilterOutputStream) {
Field outField = null;
try {
outField = FilterOutputStream.class.getDeclaredField("out");
outField.setAccessible(true);
return getOutputFileDescriptor((OutputStream)outField.get(stream));
} catch (Exception e) {
// XXX: masking other exceptions
} finally {
if (outField != null && outField.isAccessible()) {
outField.setAccessible(false);
}
}
}
return null;
}
@Override
public boolean isatty() {
checkClosed();
FileDescriptor fd;
try {
if ((fd = getInputFileDescriptor(inputStream)) == null
&& (fd = getOutputFileDescriptor(outputStream)) == null) {
return false;
}
} catch (IOException e) {
return false;
}
return PosixModule.getPOSIX().isatty(fd);
}
@Override
public boolean readable() {
return readChannel != null;
}
@Override
public boolean writable() {
return writeChannel != null;
}
@Override
public OutputStream asOutputStream() {
if (writable()) {
if (outputStream == null) {
return Channels.newOutputStream(writeChannel);
}
return outputStream;
}
return super.asOutputStream();
}
@Override
public InputStream asInputStream() {
if (readable()) {
if (inputStream == null) {
return Channels.newInputStream(readChannel);
}
return inputStream;
}
return super.asInputStream();
}
@Override
public Channel getChannel() {
return readable() ? readChannel : writeChannel;
}
private static ReadableByteChannel newChannel(InputStream in) {
return Channels.newChannel(in);
}
}