1+ /*
2+ * Copyright (C) 2008 Wayne Meissner
3+ *
4+ * This file is part of the JNR project.
5+ *
6+ * Licensed under the Apache License, Version 2.0 (the "License");
7+ * you may not use this file except in compliance with the License.
8+ * You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+
19+ package jnr .enxio .channels ;
20+
21+ import java .io .IOException ;
22+ import java .nio .ByteBuffer ;
23+ import java .nio .channels .ByteChannel ;
24+ import java .nio .channels .SocketChannel ;
25+ import java .nio .channels .spi .SelectorProvider ;
26+
27+ import jnr .constants .platform .Shutdown ;
28+
29+ public abstract class NativeSocketChannel extends SocketChannel
30+ implements ByteChannel , NativeSelectableChannel {
31+
32+ private int fd = -1 ;
33+
34+ public NativeSocketChannel (int fd ) {
35+ this (NativeSelectorProvider .getInstance (), fd );
36+ }
37+
38+ public NativeSocketChannel () {
39+ super (NativeSelectorProvider .getInstance ());
40+ }
41+
42+ NativeSocketChannel (SelectorProvider provider , int fd ) {
43+ super (provider );
44+ this .fd = fd ;
45+ }
46+
47+ public void setFD (int fd ) {
48+ this .fd = fd ;
49+ }
50+
51+ @ Override
52+ protected void implCloseSelectableChannel () throws IOException {
53+ Native .close (fd );
54+ }
55+
56+ @ Override
57+ protected void implConfigureBlocking (boolean block ) throws IOException {
58+ Native .setBlocking (fd , block );
59+ }
60+
61+ public final int getFD () {
62+ return fd ;
63+ }
64+
65+ public int read (ByteBuffer dst ) throws IOException {
66+ int n = Native .read (fd , dst );
67+ switch (n ) {
68+ case 0 :
69+ return -1 ;
70+
71+ case -1 :
72+ switch (Native .getLastError ()) {
73+ case EAGAIN :
74+ case EWOULDBLOCK :
75+ return 0 ;
76+
77+ default :
78+ throw new IOException (Native .getLastErrorString ());
79+ }
80+
81+ default :
82+ return n ;
83+ }
84+ }
85+
86+ public int write (ByteBuffer src ) throws IOException {
87+ int n = Native .write (fd , src );
88+ if (n < 0 ) {
89+ throw new IOException (Native .getLastErrorString ());
90+ }
91+
92+ return n ;
93+ }
94+
95+ protected void _shutdownInput () throws IOException {
96+ int n = Native .shutdown (fd , SHUT_RD );
97+ if (n < 0 ) {
98+ throw new IOException (Native .getLastErrorString ());
99+ }
100+ }
101+
102+
103+
104+ protected void _shutdownOutput () throws IOException {
105+ int n = Native .shutdown (fd , SHUT_WR );
106+ if (n < 0 ) {
107+ throw new IOException (Native .getLastErrorString ());
108+ }
109+ }
110+
111+ private final static int SHUT_RD = Shutdown .SHUT_RD .intValue ();
112+ private final static int SHUT_WR = Shutdown .SHUT_WR .intValue ();
113+
114+
115+ }
0 commit comments