-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathSocketIOBase.java
More file actions
83 lines (71 loc) · 1.89 KB
/
SocketIOBase.java
File metadata and controls
83 lines (71 loc) · 1.89 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
/* Copyright (c) 2007 Jython Developers */
package org.python.core.io;
import java.io.IOException;
import java.nio.channels.Channel;
import org.python.core.Py;
/**
* Base raw I/O implementation for sockets.
*
* @author Philip Jenvey
*/
public abstract class SocketIOBase<T extends Channel> extends RawIOBase {
/** The underlying socket */
protected T socketChannel;
/** true if the socket is allowed to be read from */
private boolean readable = false;
/** true if the socket is allowed to be written to */
private boolean writable = false;
/**
* Construct a SocketIOBase for the given socket Channel
*
* @param socketChannel a Channel to wrap
* @param mode a raw io socket mode String
*/
public SocketIOBase(T socketChannel, String mode) {
this.socketChannel = socketChannel;
parseMode(mode);
}
/**
* Parse the raw io socket mode string.
*
* The mode can be 'r', 'w' or 'rw' for reading, writing or
* reading and writing.
*
* @param mode a raw io socket mode String
*/
protected void parseMode(String mode) {
if (mode.equals("r")) {
readable = true;
} else if (mode.equals("w")) {
writable = true;
} else if (mode.equals("rw")) {
readable = writable = true;
} else {
throw Py.ValueError("invalid mode: '" + mode + "'");
}
}
@Override
public void close() {
if (closed()) {
return;
}
try {
socketChannel.close();
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
super.close();
}
@Override
public T getChannel() {
return socketChannel;
}
@Override
public boolean readable() {
return readable;
}
@Override
public boolean writable() {
return writable;
}
}