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


Java Channels.newReader方法代码示例

本文整理汇总了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
  }
}
 
开发者ID:gradle-clojure,项目名称:gradle-clojure,代码行数:11,代码来源:ClojureNRepl.java

示例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;
    }
 
开发者ID:ripreal,项目名称:V8LogScanner,代码行数:12,代码来源:RgxReader.java

示例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
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:48,代码来源:NativeProcessTest.java

示例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");
}
 
开发者ID:moueimei,项目名称:flume-release-1.7.0,代码行数:57,代码来源:NetcatSource.java

示例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));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:FileChannelLinesSpliterator.java


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