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


Java ByteChannel.read方法代码示例

本文整理汇总了Java中java.nio.channels.ByteChannel.read方法的典型用法代码示例。如果您正苦于以下问题:Java ByteChannel.read方法的具体用法?Java ByteChannel.read怎么用?Java ByteChannel.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.ByteChannel的用法示例。


在下文中一共展示了ByteChannel.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: timeout

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
@Test
public void timeout() throws Exception {
	this.server.delay(1000);
	this.server.start();
	ByteChannel channel = this.connection.open(10);
	long startTime = System.currentTimeMillis();
	try {
		channel.read(ByteBuffer.allocate(5));
		fail("No socket timeout thrown");
	}
	catch (SocketTimeoutException ex) {
		// Expected
		long runTime = System.currentTimeMillis() - startTime;
		assertThat(runTime).isGreaterThanOrEqualTo(10L);
		assertThat(runTime).isLessThan(10000L);
	}
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:18,代码来源:SocketTargetServerConnectionTests.java

示例2: newByteChannel

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
@Test
public void newByteChannel() throws IOException {
  Path test = base.resolve("test");
  BufferedWriter bw = Files.newBufferedWriter(test, Charset.forName("UTF-8"));
  bw.write(text);
  bw.close();
  ByteChannel bc = Files.newByteChannel(test);
  ByteBuffer bb = ByteBuffer.allocate(data.length + 1);
  int read = bc.read(bb);
  assertEquals(data.length, read);
  assertEquals(data.length, bb.position());
  bb.flip();
  byte[] buffer = new byte[data.length];
  bb.get(buffer, 0, data.length);
  assertArrayEquals(data, buffer);
}
 
开发者ID:lukhnos,项目名称:nnio,代码行数:17,代码来源:FilesTest.java

示例3: readCharArray

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからchar配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static char[] readCharArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 2).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 2) throw new IOException();
	buf.clear();
	final CharBuffer result = buf.asCharBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final char[] b = new char[n];
		result.get(b);
		return b;
	}
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:24,代码来源:ChannelHelper.java

示例4: readShortArray

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからshort配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static short[] readShortArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 2).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 2) throw new IOException();
	buf.clear();
	final ShortBuffer result = buf.asShortBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final short[] b = new short[n];
		result.get(b);
		return b;
	}
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:24,代码来源:ChannelHelper.java

示例5: readIntArray

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからint配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static int[] readIntArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 4).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 4) throw new IOException();
	buf.clear();
	final IntBuffer result = buf.asIntBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final int[] b = new int[n];
		result.get(b);
		return b;
	}
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:24,代码来源:ChannelHelper.java

示例6: readFloatArray

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからfloat配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static float[] readFloatArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 4).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 4) throw new IOException();
	buf.clear();
	final FloatBuffer result = buf.asFloatBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final float[] b = new float[n];
		result.get(b);
		return b;
	}
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:24,代码来源:ChannelHelper.java

示例7: readFromChannel

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
protected void readFromChannel(ByteChannel sc) {
    int len = 0;
    ByteBuffer inputBuffer = inBuf; // save a local reference just in case it gets nulled out.

    // TODO: probably need some type of synchronization on the input
    // buffer because it gets nulled out in closeComplete due to
    // timeout and then can potentially cause a crash here.
    if (sc.isOpen() && state != State.CLOSED && inputBuffer != null) {
        try {
            len = sc.read(inputBuffer);
        } catch (IOException e) {
            LOG.trace("readFromChannel: exception on read on port " + localPort, e);
            len = -1;
        }

        if (LOG.isTraceEnabled()) {
            LOG.trace("readFromChannel(" + len + " bytes) from port " + localPort);
        }

        if (len >= 0) {
            addToBuffer(bytes, len);
            inBuf.clear();
        } else if (len < 0) {
            closeComplete();
        }
    } else if (LOG.isTraceEnabled()) {
        LOG.trace(MessageFormat.format("readFromChannel: Looks like connection is closed for port {0}, sc.isOpen()={1}, state={2}, inputBuffer={3}", localPort, sc.isOpen(), state, inputBuffer));
    }
}
 
开发者ID:jyeary,项目名称:dnsjnio,代码行数:30,代码来源:Connection.java

示例8: readByte

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからbyeを読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static byte readByte(@NonNull final ByteChannel channel,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 1);
	final int readBytes = channel.read(buf);
	if (readBytes != 1) throw new IOException();
	buf.clear();
	return buf.get();
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:16,代码来源:ChannelHelper.java

示例9: readChar

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからcharを読み込む
 * @param channel
 * @param work
 * @return
 * @throws IOException
 */
public static char readChar(@NonNull final ByteChannel channel,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 2);
	final int readBytes = channel.read(buf);
	if (readBytes != 2) throw new IOException();
	buf.clear();
	return buf.getChar();
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:17,代码来源:ChannelHelper.java

示例10: readShort

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからshortを読み込む
 * @param channel
 * @param work
 * @return
 * @throws IOException
 */
public static short readShort(@NonNull final ByteChannel channel,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 2);
	final int readBytes = channel.read(buf);
	if (readBytes != 2) throw new IOException();
	buf.clear();
	return buf.getShort();
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:17,代码来源:ChannelHelper.java

示例11: readInt

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからintを読み込む
 * @param channel
 * @param work
 * @return
 * @throws IOException
 */
public static int readInt(@NonNull final ByteChannel channel,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 4);
	final int readBytes = channel.read(buf);
	if (readBytes != 4) throw new IOException();
	buf.clear();
	return buf.getInt();
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:17,代码来源:ChannelHelper.java

示例12: readLong

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからlongを読み込む
 * @param channel
 * @param work
 * @return
 * @throws IOException
 */
public static long readLong(@NonNull final ByteChannel channel,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 8);
	final int readBytes = channel.read(buf);
	if (readBytes != 8) throw new IOException();
	buf.clear();
	return buf.getLong();
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:17,代码来源:ChannelHelper.java

示例13: readFloat

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからfloatを読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static float readFloat(@NonNull final ByteChannel channel)
	throws IOException {
	
	final ByteBuffer buf = ByteBuffer.allocate(4);
	final int readBytes = channel.read(buf);
	if (readBytes != 4) throw new IOException();
	buf.clear();
	return buf.getFloat();
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:16,代码来源:ChannelHelper.java

示例14: readString

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからStringを読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static String readString(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int bytes = readInt(channel);
	final byte[] buf = new byte[bytes];
	final ByteBuffer b = ByteBuffer.wrap(buf);
	final int readBytes = channel.read(b);
	if (readBytes != bytes) throw new IOException();
	return new String(buf, UTF8);
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:17,代码来源:ChannelHelper.java

示例15: readBooleanArray

import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
 * ByteChannelからboolean配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static boolean[] readBooleanArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n);
	final int readBytes = channel.read(buf);
	if (readBytes != n) throw new IOException();
	buf.clear();
	final boolean[] result = new boolean[n];
	for (int i = 0; i < n; i++) {
		result[i] = buf.get() != 0;
	}
	return result;
}
 
开发者ID:saki4510t,项目名称:libcommon,代码行数:21,代码来源:ChannelHelper.java


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