當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。