ActiveIO - 1.0

Clover coverage report - ActiveIO - 1.0 Coverage timestamp: Fri Apr 22 2005 14:27:22 PDT FRAMES NO FRAMES |
file stats: | LOC: | 105 |  | Methods: | 6
| NCLOC: | 58 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
NIOSynchChannel.java | 66.7% | 76.9% | 83.3% | 75% |

1 |  |
     /**
2 |  |
      *
3 |  |
      * Copyright 2004 Hiram Chirino
4 |  |
      *
5 |  |
      * Licensed under the Apache License, Version 2.0 (the "License");
6 |  |
      * you may not use this file except in compliance with the License.
7 |  |
      * You may obtain a copy of the License at
8 |  |
      *
9 |  |
      * http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
16 |  |
      *
17 |  |
      **/
18 |  |
19 |  |
     package org.activeio.net;
20 |  |
21 |  |
     import java.io.IOException;
22 |  |
     import java.net.SocketTimeoutException;
23 |  |
     import java.nio.ByteBuffer;
24 |  |
     import java.nio.channels.SocketChannel;
25 |  |
26 |  |
     import org.activeio.Packet;
27 |  |
     import org.activeio.SynchChannel;
28 |  |
     import org.activeio.SynchChannelServer;
29 |  |
     import org.activeio.Packet.ByteSequence;
30 |  |
     import org.activeio.packet.ByteBufferPacket;
31 |  |
     import org.activeio.packet.EOSPacket;
32 |  |
     import org.activeio.packet.EmptyPacket;
33 |  |
34 |  |
     /**
35 |  |
      * A {@see org.activeio.SynchChannel} implementation that uses a {@see java.nio.channels.SocketChannel}
36 |  |
      * to talk to the network.
37 |  |
      *
38 |  |
      * Using a SocketChannelSynchChannel should be more efficient than using a SocketSynchChannel since
39 |  |
      * direct ByteBuffer can be used to reduce the jvm overhead needed to copy byte[]s.
40 |  |
      *
41 |  |
      * @version $Revision$
42 |  |
      */
43 |  |
     final public class NIOSynchChannel extends NIOBaseChannel implements SynchChannel {
44 |  |
45 |  |
         private ByteBuffer inputByteBuffer;
46 |  |
     //    private Packet data2;
47 |  |
48 | 28 |
         protected NIOSynchChannel(SocketChannel socketChannel) throws IOException {
49 | 28 |
             this(socketChannel, true );
50 |  |
         }
51 |  |
52 | 28 |
         protected NIOSynchChannel(SocketChannel socketChannel, boolean useDirect) throws IOException {
53 | 28 |
             super(socketChannel, useDirect);
54 |  |
         }
55 |  |
56 | 2064 |
         public Packet read(long timeout) throws IOException {
57 | 2064 |
             try {
58 |  |
59 | 2064 |
                 if( timeout==SynchChannelServer.WAIT_FOREVER_TIMEOUT )
60 | 0 |
                     setSoTimeout( 0 );
61 | 2064 |
                 else if( timeout==SynchChannelServer.NO_WAIT_TIMEOUT )
62 | 0 |
                     setSoTimeout( 1 );
63 |  |
                 else
64 | 2064 |
                     setSoTimeout( (int)timeout );
65 |  |
66 | 2064 |
                 if( inputByteBuffer==null || !inputByteBuffer.hasRemaining() ) {
67 | 14 |
                     inputByteBuffer = allocateBuffer();
68 |  |
                 }
69 |  |
70 | 2064 |
                 int size = socketChannel.read(inputByteBuffer);
71 | 2064 |
                 if( size == -1 )
72 | 4 |
                     return EOSPacket.EOS_PACKET;
73 | 2060 |
                 if( size == 0 )
74 | 0 |
                     return EmptyPacket.EMPTY_PACKET;
75 |  |
76 | 2060 |
                 ByteBuffer remaining = inputByteBuffer.slice();
77 | 2060 |
                 Packet data = new ByteBufferPacket(((ByteBuffer)inputByteBuffer.flip()).slice());
78 |  |
79 |  |
                 // Keep the remaining buffer around to fill with data.
80 | 2060 |
                 inputByteBuffer = remaining;
81 | 2060 |
                 return data;
82 |  |
83 |  |
             } catch (SocketTimeoutException e) {
84 | 0 |
                 return null;
85 |  |
             }
86 |  |
         }
87 |  |
88 | 2058 |
         public void write(Packet packet) throws IOException {
89 | 2058 |
             ByteBuffer data;
90 | 2058 |
             if( packet.getClass()==ByteBufferPacket.class ) {
91 | 2058 |
                 data = ((ByteBufferPacket)packet).getByteBuffer();
92 |  |
             } else {
93 | 0 |
                 ByteSequence sequence = packet.asByteSequence();
94 | 0 |
                 data = ByteBuffer.wrap(sequence.getData(), sequence.getOffset(), sequence.getLength());
95 |  |
             }
96 | 2058 |
             socketChannel.write( data );
97 |  |
         }
98 |  |
99 | 24 |
         public void start() throws IOException {
100 |  |
         }
101 |  |
102 | 0 |
         public void stop(long timeout) throws IOException {
103 |  |
         }
104 |  |
105 |  |
     }