Search in sources :

Example 41 with InputStreamReader

use of java.io.InputStreamReader in project cordova-android-chromeview by thedracle.

the class FileTransfer method createFileTransferError.

private static JSONObject createFileTransferError(int errorCode, String source, String target, URLConnection connection) {
    int httpStatus = 0;
    StringBuilder bodyBuilder = new StringBuilder();
    String body = null;
    if (connection != null) {
        try {
            if (connection instanceof HttpURLConnection) {
                httpStatus = ((HttpURLConnection) connection).getResponseCode();
                InputStream err = ((HttpURLConnection) connection).getErrorStream();
                if (err != null) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(err, "UTF-8"));
                    String line = reader.readLine();
                    while (line != null) {
                        bodyBuilder.append(line);
                        line = reader.readLine();
                        if (line != null)
                            bodyBuilder.append('\n');
                    }
                    body = bodyBuilder.toString();
                }
            }
        } catch (IOException e) {
            Log.w(LOG_TAG, "Error getting HTTP status code from connection.", e);
        }
    }
    return createFileTransferError(errorCode, source, target, body, httpStatus);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 42 with InputStreamReader

use of java.io.InputStreamReader in project bazel by bazelbuild.

the class JarFilter method parseSrcJars.

static List<String> parseSrcJars(List<Path> srcJars) throws IOException {
    List<String> result = Lists.newArrayList();
    for (Path srcJar : srcJars) {
        try (ZipFile sourceZipFile = new ZipFile(srcJar.toFile())) {
            Enumeration<? extends ZipEntry> entries = sourceZipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (!entry.getName().endsWith(".java")) {
                    continue;
                }
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(sourceZipFile.getInputStream(entry), UTF_8))) {
                    String packageString = parseDeclaredPackage(reader);
                    if (packageString != null) {
                        String archiveFileNamePrefix = getArchiveFileNamePrefix(entry.getName(), packageString);
                        result.add(archiveFileNamePrefix);
                    }
                }
            }
        }
    }
    return result;
}
Also used : Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) InputStreamReader(java.io.InputStreamReader) ZipEntry(java.util.zip.ZipEntry) BufferedReader(java.io.BufferedReader)

Example 43 with InputStreamReader

use of java.io.InputStreamReader in project jvm-tools by aragozin.

the class JarBuilderTool method toLines.

private static Collection<String> toLines(InputStream is) throws IOException {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        List<String> result = new ArrayList<String>();
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            result.add(line);
        }
        return result;
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        // ignore
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 44 with InputStreamReader

use of java.io.InputStreamReader in project bazel by bazelbuild.

the class HttpConnectorTest method serverError_retriesConnect.

@Test
public void serverError_retriesConnect() throws Exception {
    try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) {
        @SuppressWarnings("unused") Future<?> possiblyIgnoredError = executor.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                try (Socket socket = server.accept()) {
                    readHttpRequest(socket.getInputStream());
                    sendLines(socket, "HTTP/1.1 500 Incredible Catastrophe", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 8", "", "nononono");
                }
                try (Socket socket = server.accept()) {
                    readHttpRequest(socket.getInputStream());
                    sendLines(socket, "HTTP/1.1 200 OK", "Date: Fri, 31 Dec 1999 23:59:59 GMT", "Connection: close", "Content-Type: text/plain", "Content-Length: 5", "", "hello");
                }
                return null;
            }
        });
        try (Reader payload = new InputStreamReader(connector.connect(new URL(String.format("http://127.0.0.1:%d", server.getLocalPort())), ImmutableMap.<String, String>of()).getInputStream(), ISO_8859_1)) {
            assertThat(CharStreams.toString(payload)).isEqualTo("hello");
            assertThat(clock.currentTimeMillis()).isEqualTo(100L);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ServerSocket(java.net.ServerSocket) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) URL(java.net.URL) Test(org.junit.Test)

Example 45 with InputStreamReader

use of java.io.InputStreamReader in project bazel by bazelbuild.

the class SkylarkRepositoryContextTest method testOutputFile.

private void testOutputFile(Path path, String content) throws IOException {
    assertThat(path.exists()).isTrue();
    assertThat(CharStreams.toString(new InputStreamReader(path.getInputStream(), StandardCharsets.UTF_8))).isEqualTo(content);
}
Also used : InputStreamReader(java.io.InputStreamReader)

Aggregations

InputStreamReader (java.io.InputStreamReader)4884 BufferedReader (java.io.BufferedReader)3422 IOException (java.io.IOException)2119 InputStream (java.io.InputStream)1277 FileInputStream (java.io.FileInputStream)859 URL (java.net.URL)611 ArrayList (java.util.ArrayList)560 File (java.io.File)520 Reader (java.io.Reader)518 Test (org.junit.Test)452 HttpURLConnection (java.net.HttpURLConnection)294 ByteArrayInputStream (java.io.ByteArrayInputStream)282 FileNotFoundException (java.io.FileNotFoundException)241 OutputStreamWriter (java.io.OutputStreamWriter)241 URLConnection (java.net.URLConnection)227 HashMap (java.util.HashMap)192 Socket (java.net.Socket)179 OutputStream (java.io.OutputStream)178 StringWriter (java.io.StringWriter)148 PrintWriter (java.io.PrintWriter)140