Java Code Examples for java.io.InputStream.readAllBytes()

The following are Jave code examples for showing how to use readAllBytes() of the java.io.InputStream class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: BasicAuthTest.java   View Source Code Vote up 6 votes
@Override
public void handle(HttpExchange he) throws IOException {
    String method = he.getRequestMethod();
    InputStream is = he.getRequestBody();
    if (method.equalsIgnoreCase("POST")) {
        String requestBody = new String(is.readAllBytes(), US_ASCII);
        if (!requestBody.equals(POST_BODY)) {
            he.sendResponseHeaders(500, -1);
            ok = false;
        } else {
            he.sendResponseHeaders(200, -1);
            ok = true;
        }
    } else { // GET
        he.sendResponseHeaders(200, RESPONSE.length());
        OutputStream os = he.getResponseBody();
        os.write(RESPONSE.getBytes(US_ASCII));
        os.close();
        ok = true;
    }
}
 
Example 2
Project: openjdk-jdk10   File: MultiAuthTest.java   View Source Code Vote up 6 votes
@Override
public void handle(HttpExchange he) throws IOException {
    String method = he.getRequestMethod();
    InputStream is = he.getRequestBody();
    if (method.equalsIgnoreCase("POST")) {
        String requestBody = new String(is.readAllBytes(), US_ASCII);
        if (!requestBody.equals(POST_BODY)) {
            he.sendResponseHeaders(500, -1);
            ok = false;
        } else {
            he.sendResponseHeaders(200, -1);
            ok = true;
        }
    } else { // GET
        he.sendResponseHeaders(200, RESPONSE.length());
        OutputStream os = he.getResponseBody();
        os.write(RESPONSE.getBytes(US_ASCII));
        os.close();
        ok = true;
    }
}
 
Example 3
Project: flow-stages   File: Stages.java   View Source Code Vote up 6 votes
private ASL getASLResource(String resourceName) throws IOException {
    InputStream definition = getClass().getClassLoader().getResourceAsStream(resourceName);
    if(definition == null) {
        throw new RuntimeException("machine.json not found");
    }

    byte[] jsonData = definition.readAllBytes();
    return objectMapper.readValue(jsonData, ASL.class);
}
 
Example 4
Project: openjdk-jdk10   File: AppletClassLoader.java   View Source Code Vote up 5 votes
private static byte[] getBytes(URL url) throws IOException {
    URLConnection uc = url.openConnection();
    if (uc instanceof java.net.HttpURLConnection) {
        java.net.HttpURLConnection huc = (java.net.HttpURLConnection) uc;
        int code = huc.getResponseCode();
        if (code >= java.net.HttpURLConnection.HTTP_BAD_REQUEST) {
            throw new IOException("open HTTP connection failed.");
        }
    }
    int len = uc.getContentLength();

    // Fixed #4507227: Slow performance to load
    // class and resources. [stanleyh]
    //
    // Use buffered input stream [stanleyh]
    InputStream in = new BufferedInputStream(uc.getInputStream());

    byte[] b;
    try {
        b = in.readAllBytes();
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);
    } finally {
        in.close();
    }
    return b;
}
 
Example 5
Project: openjdk-jdk10   File: RedefineClassWithNativeMethodAgent.java   View Source Code Vote up 5 votes
public static void premain(String agentArgs, final Instrumentation inst) throws Exception {
    String s = agentArgs.substring(0, agentArgs.indexOf(".class"));
    clz = Class.forName(s.replace('/', '.'));
    InputStream in;
    Module m = clz.getModule();
    if (m != null) {
        in = m.getResourceAsStream(agentArgs);
    } else {
        ClassLoader loader =
            RedefineClassWithNativeMethodAgent.class.getClassLoader();
        in = loader.getResourceAsStream(agentArgs);
    }
    if (in == null) {
        throw new Exception("Cannot find class: " + agentArgs);
    }
    byte[] buffer = in.readAllBytes();

    new Timer(true).schedule(new TimerTask() {
        public void run() {
            try {
                System.out.println("Instrumenting");
                ClassDefinition cld = new ClassDefinition(clz, buffer);
                inst.redefineClasses(new ClassDefinition[] { cld });
            }
            catch (Exception e) { e.printStackTrace(); }
        }
    }, 500);
}