当前位置: 首页>>代码示例>>Java>>正文


Java ReadableByteChannel.read方法代码示例

本文整理汇总了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);
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:25,代码来源:HttpClientTransmitterImpl.java

示例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);
}
 
开发者ID:linkingli,项目名称:FaceDistinguish,代码行数:24,代码来源:HttpUtil.java

示例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;
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:30,代码来源:InputStreamReceiver.java

示例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;
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:36,代码来源:Server.java

示例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;
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:41,代码来源:RpcServer.java

示例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;
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:15,代码来源:CoolRMINioRemoter.java

示例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());
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:33,代码来源:AbstractReadOnlyContentStoreTest.java

示例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();
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:10,代码来源:Packetizer.java

示例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();
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:12,代码来源:RelayTunnel.java

示例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;
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:9,代码来源:MainChannel.java

示例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);
    }
  }
}
 
开发者ID:naver,项目名称:hadoop,代码行数:10,代码来源:PacketReceiver.java

示例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;
}
 
开发者ID:rizsi,项目名称:rcom,代码行数:28,代码来源:RoundBuffer.java

示例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);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:36,代码来源:FileChannelImpl.java

示例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;
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:33,代码来源:FileChannelImpl.java

示例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();
    }
}
 
开发者ID:eclipse,项目名称:gemoc-studio,代码行数:34,代码来源:IFileUtils.java


注:本文中的java.nio.channels.ReadableByteChannel.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。