+ public void _close() {
+ if(o != null) {
+ try {
+ if(type() == TYPE_STREAM) {
+ if(listen()) ((ServerSocket)o).close();
+ else ((Socket)o).close();
+ } else {
+ ((DatagramSocket)o).close();
+ }
+ } catch(IOException e) {
+ /* ignore */
+ }
+ }
+ }
+
+ public int read(byte[] a, int off, int length) throws ErrnoException {
+ if(type() == TYPE_STREAM) {
+ if(is == null) throw new ErrnoException(EPIPE);
+ try {
+ int n = is.read(a,off,length);
+ return n < 0 ? 0 : n;
+ } catch(IOException e) {
+ throw new ErrnoException(EIO);
+ }
+ } else {
+ DatagramSocket ds = (DatagramSocket) o;
+ dp.setData(a,off,length);
+ try {
+ ds.receive(dp);
+ } catch(IOException e) {
+ throw new ErrnoException(EIO);
+ }
+ return dp.getLength();
+ }
+ }
+
+ public int write(byte[] a, int off, int length) throws ErrnoException {
+ if(type() == TYPE_STREAM) {
+ if(os == null) throw new ErrnoException(EPIPE);
+ try {
+ os.write(a,off,length);
+ return length;
+ } catch(IOException e) {
+ throw new ErrnoException(EIO);
+ }
+ } else {
+ DatagramSocket ds = (DatagramSocket) o;
+ dp.setData(a,off,length);
+ try {
+ ds.send(dp);
+ } catch(IOException e) {
+ throw new ErrnoException(EIO);
+ }
+ return dp.getLength();
+ }
+ }
+
+ // FEATURE: Check that these are correct
+ public int flags() {
+ if(is != null && os != null) return O_RDWR;
+ if(is != null) return O_RDONLY;
+ if(os != null) return O_WRONLY;
+ return 0;
+ }
+
+ // FEATURE: Populate this properly
+ public FStat _fstat() { return new FStat(); }