Example usage for java.nio.channels Channels newReader

List of usage examples for java.nio.channels Channels newReader

Introduction

In this page you can find the example usage for java.nio.channels Channels newReader.

Prototype

public static Reader newReader(ReadableByteChannel ch, Charset charset) 

Source Link

Document

Constructs a reader that decodes bytes from the given channel according to the given charset.

Usage

From source file:Dengue.CDengueManager.java

private static void sendInfoToCPU(String pStrJSON, int pIntPort) {

    new Thread(() -> {
        try (Socket client = new Socket(InetAddress.getLocalHost(), pIntPort)) {

            Writer objWriter = Channels.newWriter(Channels.newChannel(client.getOutputStream()),
                    StandardCharsets.US_ASCII.name());
            objWriter.write(pStrJSON);//  w w  w.j  a va2  s  .  co  m
            objWriter.flush();

            client.shutdownOutput();

            try (Reader objReader = Channels.newReader(Channels.newChannel(client.getInputStream()),
                    StandardCharsets.US_ASCII.name());
                    BufferedReader objOutReader = new BufferedReader(objReader)) {
                System.out.println((char) objOutReader.read());

            }

        } catch (IOException e) {
            System.out.println(e);
        }
    }).start();
}

From source file:com.spectralogic.ds3client.helpers.channels.WindowedChannelFactory_Test.java

@Test
public void getReturnsWindow() throws Exception {
    try (final ByteArraySeekableByteChannel channel = new ByteArraySeekableByteChannel()) {
        final Writer writer = Channels.newWriter(channel, "UTF-8");
        writer.write("0123456789");
        writer.close();/*from   ww  w .j a v  a 2  s. co m*/

        try (final WindowedChannelFactory windowedChannelFactory = new WindowedChannelFactory(channel)) {
            try (final SeekableByteChannel window = windowedChannelFactory.get(2L, 6L)) {
                assertThat(IOUtils.toString(Channels.newReader(window, "UTF-8")), is("234567"));
            }
        }
    }
}

From source file:org.apache.weasel.V06Handshake.java

public WebSocket<T> clientHandshake(T channel, URI uri, String subprotocol, String origin, String cookies,
        String... otherHeaders) throws IOException {
    // TODO: support non-blocking
    String key = generateKey();// w ww .  j  av a2s. c  o m
    ByteBuffer request = buildRequest(uri, key, origin, subprotocol, cookies, otherHeaders);
    channel.write(request);
    BufferedReader reader = new BufferedReader(Channels.newReader(channel, "UTF-8"));
    if (!processResponse(reader, key)) {
        channel.close();
        throw new WebSocketException("Handshake failed");
    }
    return new V06WebSocket<T>(channel, true);
}

From source file:org.apache.beam.sdk.io.LocalFileSystemTest.java

@Test
public void testReadWithExistingFile() throws Exception {
    String expected = "my test string";
    File existingFile = temporaryFolder.newFile();
    Files.write(expected, existingFile, StandardCharsets.UTF_8);
    String data;//from   w w  w  .  ja v  a2s . co m
    try (Reader reader = Channels.newReader(
            localFileSystem.open(LocalResourceId.fromPath(existingFile.toPath(), false /* isDirectory */)),
            StandardCharsets.UTF_8.name())) {
        data = new LineReader(reader).readLine();
    }
    assertEquals(expected, data);
}

From source file:com.spectralogic.ds3client.helpers.RequestMatchers.java

private static String channelToString(final SeekableByteChannel channel) {
    try {/*www  .ja v a2  s.  c o  m*/
        channel.position(0);
        return IOUtils.toString(Channels.newReader(channel, "UTF-8"));
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.spectralogic.ds3client.helpers.channels.WindowedSeekableByteChannel_Test.java

private static String channelToString(final SeekableByteChannel channel) throws IOException {
    return IOUtils.toString(Channels.newReader(channel, "UTF-8"));
}

From source file:com.streak.logging.analysis.AnalysisUtility.java

public static String loadSchemaString(String schemaFileName)
        throws FileNotFoundException, LockException, IOException {
    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile schemaFile = new AppEngineFile(schemaFileName);
    FileReadChannel readChannel = fileService.openReadChannel(schemaFile, false);
    BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
    String schemaLine;//  ww  w.  j  av a 2  s .  c o  m
    try {
        schemaLine = reader.readLine().trim();
    } catch (NullPointerException npe) {
        throw new IOException("Encountered NPE reading " + schemaFileName);
    }
    reader.close();
    readChannel.close();
    return schemaLine;
}

From source file:com.linkedin.databus.core.DbusEventBuffer.java

public int readEvents(ReadableByteChannel readChannel, Encoding _encoding) throws InvalidEventException {
    switch (_encoding) {
    case BINARY:/*from ww w . j  a  va 2 s  .  co m*/
        return readEvents(readChannel);
    case JSON:
    case JSON_PLAIN_VALUE: {

        BufferedReader in = new BufferedReader(Channels.newReader(readChannel, "UTF-8"));
        try {
            return DbusEventSerializable.appendToEventBuffer(in, this, null, false);
        } catch (JsonParseException e) {
            throw new InvalidEventException(e);
        } catch (IOException e) {
            throw new InvalidEventException(e);
        }
    }
    }
    return -1;
}