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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.openfire.net;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
/**
* Class that simulate an InputStream given a un-blocking channel.
*
* @author Daniele Piras
*/
class ChannelInputStream extends InputStream
{
ByteBuffer buf = ByteBuffer.allocate(1024);
ReadableByteChannel inputChannel;
public ChannelInputStream(ReadableByteChannel ic)
{
inputChannel = ic;
}
private void doRead() throws IOException
{
final int cnt = inputChannel.read(buf);
if (cnt > 0)
{
buf.flip();
}
else
{
if (cnt == -1)
{
buf.flip();
}
}
}
public synchronized int read(byte[] bytes, int off, int len)
throws IOException
{
if (buf.position() == 0)
{
doRead();
}
else
{
buf.flip();
}
len = Math.min(len, buf.remaining());
if (len == 0)
{
return -1;
}
buf.get(bytes, off, len);
if (buf.hasRemaining())
{
// Discard read data and move unread data to the begining of the buffer.
// Leave
// the position at the end of the buffer as a way to indicate that there
// is
// unread data
buf.compact();
}
else
{
buf.clear();
}
return len;
}
@Override
public int read() throws IOException
{
byte[] tmpBuf = new byte[1];
int byteRead = read(tmpBuf, 0, 1);
if (byteRead < 1)
{
return -1;
}
else
{
return tmpBuf[0];
}
}
}