本文整理汇总了Java中java.nio.channels.ReadableByteChannel.read方法的典型用法代码示例。如果您正苦于以下问题:Java ReadableByteChannel.read方法的具体用法?Java ReadableByteChannel.read怎么用?Java ReadableByteChannel.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.ReadableByteChannel
的用法示例。
在下文中一共展示了ReadableByteChannel.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: channelCopy
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
private static void channelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException
{
final ByteBuffer buffer = ByteBuffer.allocateDirect(2 * 1024);
while (src.read(buffer) != -1)
{
// prepare the buffer to be drained
buffer.flip();
// write to the channel, may block
dest.write(buffer);
// If partial transfer, shift remainder down
// If buffer is empty, same as doing clear()
buffer.compact();
}
// EOF will leave buffer in fill state
buffer.flip();
// make sure the buffer is fully drained.
while (buffer.hasRemaining())
{
dest.write(buffer);
}
}
示例2: readStreamAsStr
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
/**
* 将流转换为字符串
*
* @param is
* @return
* @throws IOException
*/
public static String readStreamAsStr(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
WritableByteChannel dest = Channels.newChannel(bos);
ReadableByteChannel src = Channels.newChannel(is);
ByteBuffer bb = ByteBuffer.allocate(4096);
while (src.read(bb) != -1) {
bb.flip();
dest.write(bb);
bb.clear();
}
src.close();
dest.close();
return new String(bos.toByteArray(), Constants.ENCODING);
}
示例3: read
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
@Override
public int read(ReadableByteChannel bc, int remainingBytes) throws IOException {
int n;
try {
synchronized (bb) {
boolean needNotify=nRead==nWrite;
int spaceInBuffer=getPositiveSpaceInBuffer();
int l=Math.min(remainingBytes, bb.capacity()-bb.position());
l=Math.min(l, spaceInBuffer);
bb.limit(l+bb.position());
n=bc.read(bb);
if(n>0)
{
nWrite+=n;
if(needNotify)
{
bb.notifyAll();
}
}
if(bb.position()==bb.capacity())
{
bb.clear();
}
}
} catch (InterruptedException e) {
throw new IOException(e);
}
return n;
}
示例4: channelIO
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
/**
* Helper for {@link #channelRead(ReadableByteChannel, ByteBuffer)}
* and {@link #channelWrite(WritableByteChannel, ByteBuffer)}. Only
* one of readCh or writeCh should be non-null.
*
* @see #channelRead(ReadableByteChannel, ByteBuffer)
* @see #channelWrite(WritableByteChannel, ByteBuffer)
*/
private static int channelIO(ReadableByteChannel readCh,
WritableByteChannel writeCh,
ByteBuffer buf) throws IOException {
int originalLimit = buf.limit();
int initialRemaining = buf.remaining();
int ret = 0;
while (buf.remaining() > 0) {
try {
int ioSize = Math.min(buf.remaining(), NIO_BUFFER_LIMIT);
buf.limit(buf.position() + ioSize);
ret = (readCh == null) ? writeCh.write(buf) : readCh.read(buf);
if (ret < ioSize) {
break;
}
} finally {
buf.limit(originalLimit);
}
}
int nBytes = initialRemaining - buf.remaining();
return (nBytes > 0) ? nBytes : ret;
}
示例5: channelIO
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
/**
* Helper for {@link #channelRead(java.nio.channels.ReadableByteChannel, java.nio.ByteBuffer)}
* and {@link #channelWrite(GatheringByteChannel, BufferChain)}. Only
* one of readCh or writeCh should be non-null.
*
* @param readCh read channel
* @param writeCh write channel
* @param buf buffer to read or write into/out of
* @return bytes written
* @throws java.io.IOException e
* @see #channelRead(java.nio.channels.ReadableByteChannel, java.nio.ByteBuffer)
* @see #channelWrite(GatheringByteChannel, BufferChain)
*/
private static int channelIO(ReadableByteChannel readCh,
WritableByteChannel writeCh,
ByteBuffer buf) throws IOException {
int originalLimit = buf.limit();
int initialRemaining = buf.remaining();
int ret = 0;
while (buf.remaining() > 0) {
try {
int ioSize = Math.min(buf.remaining(), NIO_BUFFER_LIMIT);
buf.limit(buf.position() + ioSize);
ret = (readCh == null) ? writeCh.write(buf) : readCh.read(buf);
if (ret < ioSize) {
break;
}
} finally {
buf.limit(originalLimit);
}
}
int nBytes = initialRemaining - buf.remaining();
return (nBytes > 0) ? nBytes : ret;
}
示例6: read
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
@Override
public int read(ReadableByteChannel bc, int remainingBytes) throws IOException {
recvBuffer.limit(recvBuffer.position()+remainingBytes);
int n=bc.read(recvBuffer);
if(n==remainingBytes)
{
byte[] msg=new byte[recvBuffer.position()];
recvBuffer.flip();
recvBuffer.get(msg);
toProcess.add(msg);
recvBuffer.clear();
}
return n;
}
示例7: testGetReaderForExistingContentUrl
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
/**
* Checks that the various methods of obtaining a reader are supported.
*/
@Test
public void testGetReaderForExistingContentUrl() throws Exception
{
ContentStore store = getStore();
String contentUrl = getExistingContentUrl();
if (contentUrl == null)
{
logger.warn("Store test testGetReaderForExistingContentUrl not possible on " + store.getClass().getName());
return;
}
// Get the reader
assertTrue("URL returned in set seems to no longer exist", store.exists(contentUrl));
ContentReader reader = store.getReader(contentUrl);
assertNotNull("Reader should never be null", reader);
assertTrue("Reader says content doesn't exist", reader.exists());
assertFalse("Reader should not be closed before a read", reader.isClosed());
assertFalse("The reader channel should not be open yet", reader.isChannelOpen());
// Open the channel
ReadableByteChannel readChannel = reader.getReadableChannel();
readChannel.read(ByteBuffer.wrap(new byte[500]));
assertFalse("Reader should not be closed during a read", reader.isClosed());
assertTrue("The reader channel should be open during a read", reader.isChannelOpen());
// Close the channel
readChannel.close();
assertTrue("Reader should be closed after a read", reader.isClosed());
assertFalse("The reader channel should be closed after a read", reader.isChannelOpen());
}
示例8: packetize
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
public IPv4Packet packetize(ReadableByteChannel channel, int maxChunkSize) throws IOException {
payloadBuffer.limit(maxChunkSize).position(0);
int payloadLength = channel.read(payloadBuffer);
if (payloadLength == -1) {
return null;
}
payloadBuffer.flip();
return inflate();
}
示例9: readInt
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
private static int readInt(ReadableByteChannel channel) throws IOException {
final int intSize = 4;
ByteBuffer buffer = ByteBuffer.allocate(intSize);
do {
if (channel.read(buffer) == -1) {
throw new IOException("Cannot read from channel");
}
} while (buffer.hasRemaining());
buffer.flip();
return buffer.getInt();
}
示例10: read
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
@Override
public int read(ReadableByteChannel bc, int remainingBytes) throws IOException {
byte[] data=new byte[remainingBytes];
ByteBuffer dst=ByteBuffer.wrap(data);
int n=bc.read(dst);
System.out.println("Recv: "+new String(data, 0, n));
return n;
}
示例11: readChannelFully
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
private static void readChannelFully(ReadableByteChannel ch, ByteBuffer buf)
throws IOException {
while (buf.remaining() > 0) {
int n = ch.read(buf);
if (n < 0) {
throw new IOException("Premature EOF reading from " + ch);
}
}
}
示例12: read
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
@Override
public int read(ReadableByteChannel bc, int remainingBytes) throws IOException {
int n=Math.min(remainingBytes, buffer.capacity()-buffer.position());
List<Send> toactivate=null;
synchronized (buffer) {
buffer.limit(buffer.position()+n);
n=bc.read(buffer);
if(n>0)
{
nRead+=n;
toactivate=RoundBuffer.this.senders;
}
if(buffer.position()==buffer.capacity())
{
// Buffer is filled. We restart on the other end.
buffer.position(0);
}
}
if(toactivate!=null)
{
for(Send s: toactivate)
{
s.dataAvailable();
}
}
return n;
}
示例13: transferFromArbitraryChannel
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
private long transferFromArbitraryChannel(ReadableByteChannel src,
long position, long count)
throws IOException
{
// Untrusted target: Use a newly-erased buffer
int c = (int)Math.min(count, TRANSFER_SIZE);
ByteBuffer bb = Util.getTemporaryDirectBuffer(c);
long tw = 0; // Total bytes written
long pos = position;
try {
Util.erase(bb);
while (tw < count) {
bb.limit((int)Math.min((count - tw), (long)TRANSFER_SIZE));
// ## Bug: Will block reading src if this channel
// ## is asynchronously closed
int nr = src.read(bb);
if (nr <= 0)
break;
bb.flip();
int nw = write(bb, pos);
tw += nw;
if (nw != nr)
break;
pos += nw;
bb.clear();
}
return tw;
} catch (IOException x) {
if (tw > 0)
return tw;
throw x;
} finally {
Util.releaseTemporaryDirectBuffer(bb);
}
}
示例14: transferFromArbitraryChannel
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
private long transferFromArbitraryChannel(ReadableByteChannel src,
long position, long count)
throws IOException
{
// Untrusted target: Use a newly-erased buffer
int c = (int)Math.min(count, TRANSFER_SIZE);
ByteBuffer bb = ByteBuffer.allocate(c);
long tw = 0; // Total bytes written
long pos = position;
try {
while (tw < count) {
bb.limit((int)Math.min((count - tw), (long)TRANSFER_SIZE));
// ## Bug: Will block reading src if this channel
// ## is asynchronously closed
int nr = src.read(bb);
if (nr <= 0)
break;
bb.flip();
int nw = write(bb, pos);
tw += nw;
if (nw != nr)
break;
pos += nw;
bb.clear();
}
return tw;
} catch (IOException x) {
if (tw > 0)
return tw;
throw x;
}
}
示例15: isStreamEqual
import java.nio.channels.ReadableByteChannel; //导入方法依赖的package包/类
private static boolean isStreamEqual(InputStream i1, InputStream i2)
throws IOException {
ReadableByteChannel ch1 = Channels.newChannel(i1);
ReadableByteChannel ch2 = Channels.newChannel(i2);
ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);
try {
while (true) {
int n1 = ch1.read(buf1);
int n2 = ch2.read(buf2);
if (n1 == -1 || n2 == -1) return n1 == n2;
buf1.flip();
buf2.flip();
for (int i = 0; i < Math.min(n1, n2); i++)
if (buf1.get() != buf2.get())
return false;
buf1.compact();
buf2.compact();
}
} finally {
if (i1 != null) i1.close();
if (i2 != null) i2.close();
}
}