|
|||||||||||||||||||
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 | |||||||||||||||
SynchChannelInputStream.java | 50% | 53.8% | 57.1% | 53.2% |
|
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 |
package org.activeio.adapter;
|
|
18 |
|
|
19 |
import java.io.IOException;
|
|
20 |
import java.io.InputStream;
|
|
21 |
|
|
22 |
import org.activeio.Channel;
|
|
23 |
import org.activeio.Packet;
|
|
24 |
import org.activeio.SynchChannel;
|
|
25 |
import org.activeio.packet.EOSPacket;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* Provides an InputStream for a given SynchChannel.
|
|
29 |
*
|
|
30 |
* @version $Revision$
|
|
31 |
*/
|
|
32 |
public class SynchChannelInputStream extends InputStream { |
|
33 |
|
|
34 |
private final SynchChannel channel;
|
|
35 |
private Packet lastPacket;
|
|
36 |
private boolean closed; |
|
37 |
private long timeout = Channel.WAIT_FOREVER_TIMEOUT; |
|
38 |
|
|
39 |
/**
|
|
40 |
* @param channel
|
|
41 |
*/
|
|
42 | 4 |
public SynchChannelInputStream(final SynchChannel channel) {
|
43 | 4 |
this.channel = channel;
|
44 |
} |
|
45 |
|
|
46 |
/**
|
|
47 |
* @see java.io.InputStream#read()
|
|
48 |
*/
|
|
49 | 0 |
public int read() throws IOException { |
50 | 0 |
while( true ) { |
51 | 0 |
if( lastPacket==null ) { |
52 | 0 |
try {
|
53 | 0 |
lastPacket = channel.read(timeout); |
54 |
} catch (IOException e) {
|
|
55 | 0 |
throw (IOException)new IOException("Channel failed: "+e.getMessage()).initCause(e); |
56 |
} |
|
57 |
} |
|
58 | 0 |
if( lastPacket.hasRemaining() ) {
|
59 | 0 |
return lastPacket.read();
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
/**
|
|
65 |
* @see java.io.InputStream#read(byte[], int, int)
|
|
66 |
*/
|
|
67 | 13 |
public int read(byte[] b, int off, int len) throws IOException { |
68 | 13 |
while( true ) { |
69 | 15 |
if( lastPacket==null || !lastPacket.hasRemaining() ) { |
70 | 9 |
try {
|
71 | 9 |
lastPacket = channel.read(timeout); |
72 |
} catch (IOException e) {
|
|
73 | 1 |
throw (IOException)new IOException("Channel failed: "+e.getMessage()).initCause(e); |
74 |
} |
|
75 |
} |
|
76 | 14 |
if( lastPacket==EOSPacket.EOS_PACKET ) {
|
77 | 0 |
return -1;
|
78 |
} |
|
79 | 14 |
if( lastPacket!=null && lastPacket.hasRemaining() ) { |
80 | 12 |
return lastPacket.read(b, off, len);
|
81 |
} |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
/**
|
|
86 |
* @see java.io.InputStream#close()
|
|
87 |
*/
|
|
88 | 4 |
public void close() throws IOException { |
89 | 4 |
closed=true;
|
90 | 4 |
super.close();
|
91 |
} |
|
92 |
|
|
93 | 0 |
public boolean isClosed() { |
94 | 0 |
return closed;
|
95 |
} |
|
96 |
|
|
97 |
/**
|
|
98 |
* @param timeout
|
|
99 |
*/
|
|
100 | 12 |
public void setTimeout(long timeout) { |
101 | 12 |
if( timeout <= 0 )
|
102 | 4 |
timeout = Channel.WAIT_FOREVER_TIMEOUT; |
103 | 12 |
this.timeout = timeout;
|
104 |
} |
|
105 |
|
|
106 |
/**
|
|
107 |
* @return
|
|
108 |
*/
|
|
109 | 0 |
public long getTimeout() { |
110 | 0 |
if( timeout == Channel.WAIT_FOREVER_TIMEOUT )
|
111 | 0 |
return 0;
|
112 | 0 |
return timeout;
|
113 |
} |
|
114 |
} |
|
115 |
|
|