Search in sources :

Example 96 with OutputStreamWriter

use of java.io.OutputStreamWriter in project weave by continuuity.

the class FailureRestartTestRun method getInstances.

private Set<Integer> getInstances(Iterable<Discoverable> discoverables) throws IOException {
    Set<Integer> instances = Sets.newHashSet();
    for (Discoverable discoverable : discoverables) {
        InetSocketAddress socketAddress = discoverable.getSocketAddress();
        Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort());
        try {
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
            LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
            String msg = "Failure";
            writer.println(msg);
            String line = reader.readLine();
            Assert.assertTrue(line.endsWith(msg));
            instances.add(Integer.parseInt(line.substring(0, line.length() - msg.length())));
        } finally {
            socket.close();
        }
    }
    return instances;
}
Also used : Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) LineReader(com.google.common.io.LineReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter)

Example 97 with OutputStreamWriter

use of java.io.OutputStreamWriter in project weave by continuuity.

the class LocalFileTestRun method testLocalFile.

@Test
public void testLocalFile() throws Exception {
    String header = Files.readFirstLine(new File(getClass().getClassLoader().getResource("header.txt").toURI()), Charsets.UTF_8);
    WeaveRunner runner = YarnTestSuite.getWeaveRunner();
    if (runner instanceof YarnWeaveRunnerService) {
        ((YarnWeaveRunnerService) runner).setJVMOptions("-verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails");
    }
    WeaveController controller = runner.prepare(new LocalFileApplication()).withApplicationArguments("local").withArguments("LocalFileSocketServer", "local2").addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))).start();
    if (runner instanceof YarnWeaveRunnerService) {
        ((YarnWeaveRunnerService) runner).setJVMOptions("");
    }
    Iterable<Discoverable> discoverables = controller.discoverService("local");
    Assert.assertTrue(YarnTestSuite.waitForSize(discoverables, 1, 60));
    InetSocketAddress socketAddress = discoverables.iterator().next().getSocketAddress();
    Socket socket = new Socket(socketAddress.getAddress(), socketAddress.getPort());
    try {
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
        LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
        String msg = "Local file test";
        writer.println(msg);
        Assert.assertEquals(header, reader.readLine());
        Assert.assertEquals(msg, reader.readLine());
    } finally {
        socket.close();
    }
    controller.stopAndWait();
    Assert.assertTrue(YarnTestSuite.waitForSize(discoverables, 0, 60));
    TimeUnit.SECONDS.sleep(2);
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) LineReader(com.google.common.io.LineReader) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 98 with OutputStreamWriter

use of java.io.OutputStreamWriter in project weave by continuuity.

the class SocketServer method run.

@Override
public void run() {
    try {
        runThread = Thread.currentThread();
        while (running) {
            try {
                Socket socket = serverSocket.accept();
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
                    handleRequest(reader, writer);
                } finally {
                    socket.close();
                }
            } catch (SocketException e) {
                LOG.info("Socket exception: " + e);
            }
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : SocketException(java.net.SocketException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) SocketException(java.net.SocketException) PrintWriter(java.io.PrintWriter)

Example 99 with OutputStreamWriter

use of java.io.OutputStreamWriter in project cryptomator by cryptomator.

the class SettingsProvider method save.

private void save(Settings settings) {
    assert settings != null : "method should only be invoked by #scheduleSave, which checks for null";
    final Path settingsPath = getSettingsPath();
    try {
        Files.createDirectories(settingsPath.getParent());
        try (//
        OutputStream out = Files.newOutputStream(settingsPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
            Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
            gson.toJson(settings, writer);
            LOG.info("Settings saved to " + settingsPath);
        }
    } catch (IOException e) {
        LOG.error("Failed to save settings.", e);
    }
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 100 with OutputStreamWriter

use of java.io.OutputStreamWriter in project checkstyle by checkstyle.

the class XMLLogger method setOutputStream.

/**
     * Sets the OutputStream.
     * @param outputStream the OutputStream to use
     **/
private void setOutputStream(OutputStream outputStream) {
    final OutputStreamWriter osw = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
    writer = new PrintWriter(osw);
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Aggregations

OutputStreamWriter (java.io.OutputStreamWriter)1644 IOException (java.io.IOException)625 BufferedWriter (java.io.BufferedWriter)596 FileOutputStream (java.io.FileOutputStream)594 Writer (java.io.Writer)443 File (java.io.File)365 PrintWriter (java.io.PrintWriter)272 InputStreamReader (java.io.InputStreamReader)222 ByteArrayOutputStream (java.io.ByteArrayOutputStream)202 OutputStream (java.io.OutputStream)194 BufferedReader (java.io.BufferedReader)189 Test (org.junit.Test)123 InputStream (java.io.InputStream)92 ArrayList (java.util.ArrayList)90 FileNotFoundException (java.io.FileNotFoundException)88 Path (org.apache.hadoop.fs.Path)86 UnsupportedEncodingException (java.io.UnsupportedEncodingException)78 URL (java.net.URL)72 Socket (java.net.Socket)70 HttpURLConnection (java.net.HttpURLConnection)65