Search in sources :

Example 21 with BufferedReader

use of java.io.BufferedReader 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 22 with BufferedReader

use of java.io.BufferedReader 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 23 with BufferedReader

use of java.io.BufferedReader 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 24 with BufferedReader

use of java.io.BufferedReader 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)

Example 25 with BufferedReader

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

the class SocketClientSinkTest method testSinkAutoFlush.

@Test
public void testSinkAutoFlush() throws Exception {
    final ServerSocket server = new ServerSocket(0);
    final int port = server.getLocalPort();
    final SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0, true);
    simpleSink.open(new Configuration());
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    Thread sinkRunner = new Thread("Test sink runner") {

        @Override
        public void run() {
            try {
                // need two messages here: send a fin to cancel the client state:FIN_WAIT_2 while the server is CLOSE_WAIT
                simpleSink.invoke(TEST_MESSAGE + '\n');
            } 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();
    simpleSink.close();
    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

BufferedReader (java.io.BufferedReader)4790 InputStreamReader (java.io.InputStreamReader)2944 IOException (java.io.IOException)2260 FileReader (java.io.FileReader)1112 File (java.io.File)818 InputStream (java.io.InputStream)730 ArrayList (java.util.ArrayList)676 FileInputStream (java.io.FileInputStream)626 URL (java.net.URL)390 Test (org.junit.Test)367 FileNotFoundException (java.io.FileNotFoundException)329 StringReader (java.io.StringReader)282 BufferedWriter (java.io.BufferedWriter)205 HashMap (java.util.HashMap)199 Matcher (java.util.regex.Matcher)196 OutputStreamWriter (java.io.OutputStreamWriter)190 HttpURLConnection (java.net.HttpURLConnection)190 PrintWriter (java.io.PrintWriter)187 Reader (java.io.Reader)169 Socket (java.net.Socket)151