本文整理汇总了Java中java.nio.channels.ByteChannel类的典型用法代码示例。如果您正苦于以下问题:Java ByteChannel类的具体用法?Java ByteChannel怎么用?Java ByteChannel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ByteChannel类属于java.nio.channels包,在下文中一共展示了ByteChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: copySourceFilesToTarget
import java.nio.channels.ByteChannel; //导入依赖的package包/类
@Before
public void copySourceFilesToTarget() throws IOException, URISyntaxException {
src = createTempDir();
dst = createTempDir();
Files.createDirectories(src);
Files.createDirectories(dst);
txtFile = src.resolve("text-file.txt");
try (ByteChannel byteChannel = Files.newByteChannel(txtFile, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
expectedBytes = new byte[3];
expectedBytes[0] = randomByte();
expectedBytes[1] = randomByte();
expectedBytes[2] = randomByte();
byteChannel.write(ByteBuffer.wrap(expectedBytes));
}
}
示例2: writeGetRequestTo
import java.nio.channels.ByteChannel; //导入依赖的package包/类
private void writeGetRequestTo(ByteChannel channel) throws IOException {
// @formatter:off
String request = new StringBuilder("GET").append(SPACE)
.append(this.url.getPath()).append(SPACE).append("HTTP/1.1")
.append(CRLF)
.append("User-Agent: NIOHttp/0.1 Java")
.append(CRLF)
.append("Host:").append(SPACE).append(url.getHost())
.append(CRLF)
.append("Accept-Language: en-us").append(CRLF)
.append("Connection: close").append(CRLF).append(CRLF)
.toString();
// @formatter:on
ByteBuffer sending = ByteBuffer.allocate(request.getBytes().length);
sending.put(request.getBytes());
sending.flip();
channel.write(sending);
}
示例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: readLongArray
import java.nio.channels.ByteChannel; //导入依赖的package包/类
/**
* ByteChannelからlong配列を読み込む
* @param channel
* @return
* @throws IOException
*/
public static long[] readLongArray(@NonNull final ByteChannel channel)
throws IOException {
final int n = readInt(channel);
final ByteBuffer buf = ByteBuffer.allocate(n * 8).order(ByteOrder.BIG_ENDIAN);
final int readBytes = channel.read(buf);
if (readBytes != n * 8) throw new IOException();
buf.clear();
final LongBuffer result = buf.asLongBuffer();
if (result.hasArray()) {
return result.array();
} else {
final long[] b = new long[n];
result.get(b);
return b;
}
}
示例7: 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;
}
}
示例8: readDoubleArray
import java.nio.channels.ByteChannel; //导入依赖的package包/类
/**
* ByteChannelからdouble配列を読み込む
* @param channel
* @return
* @throws IOException
*/
public static double[] readDoubleArray(@NonNull final ByteChannel channel)
throws IOException {
final int n = readInt(channel);
final ByteBuffer buf = ByteBuffer.allocate(n * 8).order(ByteOrder.BIG_ENDIAN);
final int readBytes = channel.read(buf);
if (readBytes != n * 8) throw new IOException();
buf.clear();
final DoubleBuffer result = buf.asDoubleBuffer();
if (result.hasArray()) {
return result.array();
} else {
final double[] b = new double[n];
result.get(b);
return b;
}
}
示例9: 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);
}
示例10: setup
import java.nio.channels.ByteChannel; //导入依赖的package包/类
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.server = new HttpTunnelServer(this.serverConnection);
given(this.serverConnection.open(anyInt())).willAnswer(new Answer<ByteChannel>() {
@Override
public ByteChannel answer(InvocationOnMock invocation) throws Throwable {
MockServerChannel channel = HttpTunnelServerTests.this.serverChannel;
channel.setTimeout((Integer) invocation.getArguments()[0]);
return channel;
}
});
this.servletRequest = new MockHttpServletRequest();
this.servletRequest.setAsyncSupported(true);
this.servletResponse = new MockHttpServletResponse();
this.request = new ServletServerHttpRequest(this.servletRequest);
this.response = new ServletServerHttpResponse(this.servletResponse);
this.serverChannel = new MockServerChannel();
}
示例11: handleCompletion
import java.nio.channels.ByteChannel; //导入依赖的package包/类
void handleCompletion(FetchTask task, Throwable ex) {
if(ex != null)
return;
if(task.getState() != FetchState.SUCCESS) {
printErr("download "+task.infohash().toString(false)+" failed\n");
return;
}
task.getResult().ifPresent(buf -> {
Path torrentName = currentWorkDir.resolve(task.infohash().toString(false) +".torrent");
try(ByteChannel chan = Files.newByteChannel(torrentName, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
ByteBuffer torrent = TorrentUtils.wrapBareInfoDictionary(buf);
Optional<String> name = TorrentUtils.getTorrentName(torrent);
chan.write(torrent);
name.ifPresent(str -> {
println("torrent name: "+ str);
});
println("written meta to "+torrentName);
} catch (IOException e) {
handleException(e);
}
});
}
示例12: createPipeline
import java.nio.channels.ByteChannel; //导入依赖的package包/类
private ChannelPipeline createPipeline(
Peer peer,
ByteChannel channel,
BorrowedBuffer<ByteBuffer> in,
BorrowedBuffer<ByteBuffer> out,
Optional<MSECipher> cipherOptional) {
ChannelPipelineBuilder builder = channelPipelineFactory.buildPipeline(peer);
builder.channel(channel);
builder.protocol(protocol);
builder.inboundBuffer(in);
builder.outboundBuffer(out);
cipherOptional.ifPresent(cipher -> {
builder.decoders(new CipherBufferMutator(cipher.getDecryptionCipher()));
builder.encoders(new CipherBufferMutator(cipher.getEncryptionCipher()));
});
return builder.build();
}
示例13: cancel
import java.nio.channels.ByteChannel; //导入依赖的package包/类
public boolean cancel(boolean mayInterruptIfRunning)
{
try
{
ByteChannel channel=null;
synchronized (this)
{
if (_connection==null && _exception==null && _channel!=null)
{
channel=_channel;
_channel=null;
}
}
if (channel!=null)
{
closeChannel(channel,WebSocketConnectionRFC6455.CLOSE_NO_CLOSE,"cancelled");
return true;
}
return false;
}
finally
{
_done.countDown();
}
}
示例14: ChannelEndPoint
import java.nio.channels.ByteChannel; //导入依赖的package包/类
protected ChannelEndPoint(ByteChannel channel, int maxIdleTime) throws IOException
{
this._channel = channel;
_maxIdleTime=maxIdleTime;
_socket=(channel instanceof SocketChannel)?((SocketChannel)channel).socket():null;
if (_socket!=null)
{
_local=(InetSocketAddress)_socket.getLocalSocketAddress();
_remote=(InetSocketAddress)_socket.getRemoteSocketAddress();
_socket.setSoTimeout(_maxIdleTime);
}
else
{
_local=_remote=null;
}
}
示例15: 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);
}
}