本文整理汇总了Java中java.nio.channels.Channels.newReader方法的典型用法代码示例。如果您正苦于以下问题:Java Channels.newReader方法的具体用法?Java Channels.newReader怎么用?Java Channels.newReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.Channels
的用法示例。
在下文中一共展示了Channels.newReader方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: stop
import java.nio.channels.Channels; //导入方法依赖的package包/类
private void stop() {
try (
SocketChannel socket = SocketChannel.open();
PrintWriter writer = new PrintWriter(Channels.newWriter(socket, StandardCharsets.UTF_8.name()), true);
BufferedReader reader = new BufferedReader(Channels.newReader(socket, StandardCharsets.UTF_8.name()))) {
socket.connect(new InetSocketAddress("localhost", controlPort));
} catch (IOException e) {
// bury
}
}
示例2: RgxReader
import java.nio.channels.Channels; //导入方法依赖的package包/类
public RgxReader(String fileName, Charset charset, int _limit) throws FileNotFoundException {
this(CharBuffer.allocate(1024));
fs = new FileInputStream(fileName);
ReadableByteChannel channel = fs.getChannel();
CharsetDecoder decoder = charset.newDecoder();
reader = Channels.newReader(channel, decoder, -1);
buf.limit(0);
limit = _limit;
}
示例3: testDestroySignal
import java.nio.channels.Channels; //导入方法依赖的package包/类
@org.junit.Test
@ForAllEnvironments(section = "remote.platforms")
public void testDestroySignal() throws Exception {
for (int i = 1; i <= 5; i++) {
System.out.println("testDestroySignal: Round " + i + " @ " + getTestExecutionEnvironment().getDisplayName()); // NOI18N
NativeProcessBuilder npb = NativeProcessBuilder.newProcessBuilder(getTestExecutionEnvironment());
npb.getEnvironment().put("LC_ALL", "C"); // NOI18N
npb.setExecutable("/bin/sh").setArguments("-c", "trap \"echo OK && exit\" TERM; echo ready; read X"); // NOI18N
final NativeProcess process = npb.call();
assertEquals(State.RUNNING, process.getState());
final ReadableByteChannel channel = Channels.newChannel(process.getInputStream());
final BufferedReader br = new BufferedReader(Channels.newReader(channel, "UTF-8")); // NOI18N
final Callable<String> lineReader = new Callable<String>() {
@Override
public String call() throws Exception {
return br.readLine();
}
};
String outputLine = getResult(lineReader, 2, TimeUnit.SECONDS);
assertEquals("ready", outputLine); // NOI18N
// Only after we have read 'ready' string we could be sure that
// signal handler is installed...
// Proceed with sending a signal.
process.destroy();
// Signal should lead to process termination.
getResult(new Callable<Void>() {
@Override
public Void call() throws Exception {
process.waitFor();
return null;
}
}, 2, TimeUnit.SECONDS);
assertNotSame(State.RUNNING, process.getState());
outputLine = getResult(lineReader, 2, TimeUnit.SECONDS);
String error = ProcessUtils.readProcessErrorLine(process);
assertEquals("OK", outputLine); // NOI18N
assertEquals("", error); // NOI18N
}
}
示例4: run
import java.nio.channels.Channels; //导入方法依赖的package包/类
@Override
public void run() {
logger.debug("Starting connection handler");
Event event = null;
try {
Reader reader = Channels.newReader(socketChannel, sourceEncoding);
Writer writer = Channels.newWriter(socketChannel, sourceEncoding);
CharBuffer buffer = CharBuffer.allocate(maxLineLength);
buffer.flip(); // flip() so fill() sees buffer as initially empty
while (true) {
// this method blocks until new data is available in the socket
int charsRead = fill(buffer, reader);
logger.debug("Chars read = {}", charsRead);
// attempt to process all the events in the buffer
int eventsProcessed = processEvents(buffer, writer);
logger.debug("Events processed = {}", eventsProcessed);
if (charsRead == -1) {
// if we received EOF before last event processing attempt, then we
// have done everything we can
break;
} else if (charsRead == 0 && eventsProcessed == 0) {
if (buffer.remaining() == buffer.capacity()) {
// If we get here it means:
// 1. Last time we called fill(), no new chars were buffered
// 2. After that, we failed to process any events => no newlines
// 3. The unread data in the buffer == the size of the buffer
// Therefore, we are stuck because the client sent a line longer
// than the size of the buffer. Response: Drop the connection.
logger.warn("Client sent event exceeding the maximum length");
counterGroup.incrementAndGet("events.failed");
writer.write("FAILED: Event exceeds the maximum length (" +
buffer.capacity() + " chars, including newline)\n");
writer.flush();
break;
}
}
}
socketChannel.close();
counterGroup.incrementAndGet("sessions.completed");
} catch (IOException e) {
counterGroup.incrementAndGet("sessions.broken");
try {
socketChannel.close();
} catch (IOException ex) {
logger.error("Unable to close socket channel. Exception follows.", ex);
}
}
logger.debug("Connection handler exiting");
}
示例5: getBufferedReader
import java.nio.channels.Channels; //导入方法依赖的package包/类
private BufferedReader getBufferedReader() {
/**
* A readable byte channel that reads bytes from an underlying
* file channel over a specified range.
*/
ReadableByteChannel rrbc = new ReadableByteChannel() {
@Override
public int read(ByteBuffer dst) throws IOException {
int bytesToRead = fence - index;
if (bytesToRead == 0)
return -1;
int bytesRead;
if (bytesToRead < dst.remaining()) {
// The number of bytes to read is less than remaining
// bytes in the buffer
// Snapshot the limit, reduce it, read, then restore
int oldLimit = dst.limit();
dst.limit(dst.position() + bytesToRead);
bytesRead = fc.read(dst, index);
dst.limit(oldLimit);
} else {
bytesRead = fc.read(dst, index);
}
if (bytesRead == -1) {
index = fence;
return bytesRead;
}
index += bytesRead;
return bytesRead;
}
@Override
public boolean isOpen() {
return fc.isOpen();
}
@Override
public void close() throws IOException {
fc.close();
}
};
return new BufferedReader(Channels.newReader(rrbc, cs.newDecoder(), -1));
}