本文整理汇总了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);
}
示例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;
}
}
示例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;
}
}
示例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;
}
}
示例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;
}
}
示例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));
}
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
示例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);
}
示例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;
}