Search in sources :

Example 26 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class JavaSocketTests method testSocketReadOnClosedConnection.

@Test
public void testSocketReadOnClosedConnection() throws Exception {
    final Thread acceptThread = new Thread() {

        Logger log = LoggerFactory.getLogger("acceptThread");

        @Override
        public void run() {
            boolean running = true;
            try {
                Socket echoSocket = serverSocket.accept();
                log.info("Accepted connection: {}", echoSocket.getInetAddress());
                echoSocket.setSoTimeout(2000);
                while (echoSocket.isConnected() && !echoSocket.isClosed()) {
                    StringBuilder responseBuilder = new StringBuilder(500);
                    InputStream reader = echoSocket.getInputStream();
                    OutputStream writer = echoSocket.getOutputStream();
                    do {
                        int readByte = -1;
                        int available = -1;
                        try {
                            available = reader.available();
                            log.info("InputStream.available returned {}", available);
                            readByte = reader.read();
                            log.trace("Processing byte: {}", readByte);
                            switch(readByte) {
                                case -1:
                                    if (echoSocket.isConnected() && !echoSocket.isClosed()) {
                                        log.info("Available returned {}", reader.available());
                                        log.warn("Socket claims to still be open, but END_OF_STREAM received - closing echoSocket");
                                        try {
                                            echoSocket.close();
                                        } catch (Exception ex) {
                                            log.warn("Exception encountered closing echoSocket after END_OF_STREAM received", ex);
                                        }
                                    }
                                    running = false;
                                    break;
                                case 10:
                                    log.info("Complete Message - Sending Response");
                                    byte[] response = responseBuilder.toString().getBytes();
                                    responseBuilder.setLength(0);
                                    writer.write(response, 0, response.length);
                                    writer.write('\n');
                                    break;
                                default:
                                    responseBuilder.append((char) readByte);
                            }
                        } catch (SocketTimeoutException timeoutEx) {
                            log.info("Timeout reading data - available returned {}", available);
                        }
                    } while (echoSocket.isConnected() && !echoSocket.isClosed());
                }
            } catch (IOException ioEx) {
                log.error("IOException in run method", ioEx);
            } finally {
                try {
                    serverSocket.close();
                } catch (IOException ioEx) {
                    log.error("Exception encountered closing server socket", ioEx);
                }
            }
            log.info("Finished processing connection");
        }
    };
    acceptThread.start();
    clientSocket = new Socket();
    clientSocket.setSoTimeout(1000);
    clientSocket.connect(serverSocket.getLocalSocketAddress(), 10000);
    clientSocket.setTcpNoDelay(true);
    log.info("Begining message send loop ");
    byte[] message = "Hello World".getBytes();
    BufferedReader reader;
    for (int i = 1; i <= messageCount; ++i) {
        reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        OutputStream writer = clientSocket.getOutputStream();
        log.info("Sending payload");
        writer.write(message, 0, message.length);
        writer.flush();
        log.info("Sending terminator");
        writer.write('\n');
        writer.flush();
        log.info("Received Response #{}: {}", i, reader.readLine());
        Thread.sleep(1000);
    }
    log.info("Message send loop complete - closing connection");
    // Javadoc for Socket says closing the InputStream will close the connection
    clientSocket.getInputStream().close();
    if (!clientSocket.isClosed()) {
        log.warn("Closing input stream didn't close socket");
        clientSocket.close();
    }
    log.info("Sleeping ...");
    Thread.sleep(5000);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Logger(org.slf4j.Logger) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 27 with InputStreamReader

use of java.io.InputStreamReader in project hadoop by apache.

the class LdapGroupsMapping method extractPassword.

String extractPassword(String pwFile) {
    if (pwFile.isEmpty()) {
        // an anonymous bind
        return "";
    }
    StringBuilder password = new StringBuilder();
    try (Reader reader = new InputStreamReader(new FileInputStream(pwFile), StandardCharsets.UTF_8)) {
        int c = reader.read();
        while (c > -1) {
            password.append((char) c);
            c = reader.read();
        }
        return password.toString().trim();
    } catch (IOException ioe) {
        throw new RuntimeException("Could not read password file: " + pwFile, ioe);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 28 with InputStreamReader

use of java.io.InputStreamReader in project groovy by apache.

the class SourceExtensionHandler method getRegisteredExtensions.

public static Set<String> getRegisteredExtensions(ClassLoader loader) {
    Set<String> extensions = new LinkedHashSet<String>();
    extensions.add("groovy");
    try {
        Enumeration<URL> globalServices = loader.getResources("META-INF/services/org.codehaus.groovy.source.Extensions");
        while (globalServices.hasMoreElements()) {
            BufferedReader svcIn = null;
            URL service = globalServices.nextElement();
            try {
                svcIn = new BufferedReader(new InputStreamReader(service.openStream()));
                String extension = svcIn.readLine();
                while (extension != null) {
                    extension = extension.trim();
                    if (!extension.startsWith("#") && extension.length() > 0) {
                        extensions.add(extension);
                    }
                    extension = svcIn.readLine();
                }
            } catch (IOException ex) {
                throw new GroovyRuntimeException("IO Exception attempting to load registered source extension " + service.toExternalForm() + ". Exception: " + ex.toString());
            } finally {
                if (svcIn != null)
                    svcIn.close();
            }
        }
    } catch (IOException ex) {
        throw new GroovyRuntimeException("IO Exception getting registered source extensions. Exception: " + ex.toString());
    }
    return extensions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) URL(java.net.URL)

Example 29 with InputStreamReader

use of java.io.InputStreamReader in project flink by apache.

the class SocketTextStreamFunction method run.

@Override
public void run(SourceContext<String> ctx) throws Exception {
    final StringBuilder buffer = new StringBuilder();
    long attempt = 0;
    while (isRunning) {
        try (Socket socket = new Socket()) {
            currentSocket = socket;
            LOG.info("Connecting to server socket " + hostname + ':' + port);
            socket.connect(new InetSocketAddress(hostname, port), CONNECTION_TIMEOUT_TIME);
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            char[] cbuf = new char[8192];
            int bytesRead;
            while (isRunning && (bytesRead = reader.read(cbuf)) != -1) {
                buffer.append(cbuf, 0, bytesRead);
                int delimPos;
                while (buffer.length() >= delimiter.length() && (delimPos = buffer.indexOf(delimiter)) != -1) {
                    String record = buffer.substring(0, delimPos);
                    // truncate trailing carriage return
                    if (delimiter.equals("\n") && record.endsWith("\r")) {
                        record = record.substring(0, record.length() - 1);
                    }
                    ctx.collect(record);
                    buffer.delete(0, delimPos + delimiter.length());
                }
            }
        }
        // if we dropped out of this loop due to an EOF, sleep and retry
        if (isRunning) {
            attempt++;
            if (maxNumRetries == -1 || attempt < maxNumRetries) {
                LOG.warn("Lost connection to server socket. Retrying in " + delayBetweenRetries + " msecs...");
                Thread.sleep(delayBetweenRetries);
            } else {
                // throw new EOFException("Reached end of stream and reconnects are not enabled.");
                break;
            }
        }
    }
    // collect trailing data
    if (buffer.length() > 0) {
        ctx.collect(buffer.toString());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket)

Example 30 with InputStreamReader

use of java.io.InputStreamReader in project flink by apache.

the class SocketClientSinkTest method testSocketSink.

@Test
public void testSocketSink() throws Exception {
    final ServerSocket server = new ServerSocket(0);
    final int port = server.getLocalPort();
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    Thread sinkRunner = new Thread("Test sink runner") {

        @Override
        public void run() {
            try {
                SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0);
                simpleSink.open(new Configuration());
                simpleSink.invoke(TEST_MESSAGE + '\n');
                simpleSink.close();
            } catch (Throwable t) {
                error.set(t);
            }
        }
    };
    sinkRunner.start();
    Socket sk = server.accept();
    BufferedReader rdr = new BufferedReader(new InputStreamReader(sk.getInputStream()));
    String value = rdr.readLine();
    sinkRunner.join();
    server.close();
    if (error.get() != null) {
        Throwable t = error.get();
        t.printStackTrace();
        fail("Error in spawned thread: " + t.getMessage());
    }
    assertEquals(TEST_MESSAGE, value);
}
Also used : Configuration(org.apache.flink.configuration.Configuration) InputStreamReader(java.io.InputStreamReader) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

InputStreamReader (java.io.InputStreamReader)4181 BufferedReader (java.io.BufferedReader)2938 IOException (java.io.IOException)1843 InputStream (java.io.InputStream)1083 FileInputStream (java.io.FileInputStream)767 ArrayList (java.util.ArrayList)494 URL (java.net.URL)471 File (java.io.File)448 Reader (java.io.Reader)446 Test (org.junit.Test)336 HttpURLConnection (java.net.HttpURLConnection)253 ByteArrayInputStream (java.io.ByteArrayInputStream)231 OutputStreamWriter (java.io.OutputStreamWriter)218 FileNotFoundException (java.io.FileNotFoundException)210 HashMap (java.util.HashMap)171 Socket (java.net.Socket)170 OutputStream (java.io.OutputStream)150 URLConnection (java.net.URLConnection)148 StringWriter (java.io.StringWriter)135 Path (org.apache.hadoop.fs.Path)123