Java Code Examples for sun.misc.IOUtils#readAllBytes()

The following examples show how to use sun.misc.IOUtils#readAllBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimestampCheck.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
static void checkTimestamp(String file, String policyId, String digestAlg)
        throws Exception {
    try (JarFile jf = new JarFile(file)) {
        JarEntry je = jf.getJarEntry("META-INF/SIGNER.RSA");
        try (InputStream is = jf.getInputStream(je)) {
            byte[] content = IOUtils.readAllBytes(is);
            PKCS7 p7 = new PKCS7(content);
            SignerInfo[] si = p7.getSignerInfos();
            if (si == null || si.length == 0) {
                throw new Exception("Not signed");
            }
            PKCS9Attribute p9 = si[0].getUnauthenticatedAttributes()
                    .getAttribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
            PKCS7 tsToken = new PKCS7((byte[]) p9.getValue());
            TimestampToken tt =
                    new TimestampToken(tsToken.getContentInfo().getData());
            if (!tt.getHashAlgorithm().toString().equals(digestAlg)) {
                throw new Exception("Digest alg different");
            }
            if (!tt.getPolicyID().equals(policyId)) {
                throw new Exception("policyId different");
            }
        }
    }
}
 
Example 2
Source File: MethodUtil.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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();
    try (InputStream in = new BufferedInputStream(uc.getInputStream())) {
        byte[] b = IOUtils.readAllBytes(in);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);
        return b;
    }
}
 
Example 3
Source File: ReadAllBytes.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static void test(byte[] expectedBytes) throws IOException {
    int expectedLength = expectedBytes.length;
    WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));
    byte[] readBytes = IOUtils.readAllBytes(in);

    int x;
    byte[] tmp = new byte[10];
    check((x = in.read()) == -1,
          "Expected end of stream from read(), got " + x);
    check((x = in.read(tmp)) == -1,
          "Expected end of stream from read(byte[]), got " + x);
    check((x = in.read(tmp, 0, tmp.length)) == -1,
          "Expected end of stream from read(byte[], int, int), got " + x);
    check(IOUtils.readAllBytes(in).length == 0,
          "Expected readAllBytes to return empty byte array");
    check(expectedLength == readBytes.length,
          "Expected length " + expectedLength + ", got " + readBytes.length);
    check(Arrays.equals(expectedBytes, readBytes),
          "Expected[" + expectedBytes + "], got:[" + readBytes + "]");
    check(!in.isClosed(), "Stream unexpectedly closed");
}
 
Example 4
Source File: TimestampCheck.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static void checkTimestamp(String file, String policyId, String digestAlg)
        throws Exception {
    try (JarFile jf = new JarFile(file)) {
        JarEntry je = jf.getJarEntry("META-INF/SIGNER.RSA");
        try (InputStream is = jf.getInputStream(je)) {
            byte[] content = IOUtils.readAllBytes(is);
            PKCS7 p7 = new PKCS7(content);
            SignerInfo[] si = p7.getSignerInfos();
            if (si == null || si.length == 0) {
                throw new Exception("Not signed");
            }
            PKCS9Attribute p9 = si[0].getUnauthenticatedAttributes()
                    .getAttribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
            PKCS7 tsToken = new PKCS7((byte[]) p9.getValue());
            TimestampToken tt =
                    new TimestampToken(tsToken.getContentInfo().getData());
            if (!tt.getHashAlgorithm().toString().equals(digestAlg)) {
                throw new Exception("Digest alg different");
            }
            if (!tt.getPolicyID().equals(policyId)) {
                throw new Exception("policyId different");
            }
        }
    }
}
 
Example 5
Source File: MethodUtil.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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();
    try (InputStream in = new BufferedInputStream(uc.getInputStream())) {
        byte[] b = IOUtils.readAllBytes(in);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);
        return b;
    }
}
 
Example 6
Source File: ReadAllBytes.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void test(byte[] expectedBytes) throws IOException {
    int expectedLength = expectedBytes.length;
    WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));
    byte[] readBytes = IOUtils.readAllBytes(in);

    int x;
    byte[] tmp = new byte[10];
    check((x = in.read()) == -1,
          "Expected end of stream from read(), got " + x);
    check((x = in.read(tmp)) == -1,
          "Expected end of stream from read(byte[]), got " + x);
    check((x = in.read(tmp, 0, tmp.length)) == -1,
          "Expected end of stream from read(byte[], int, int), got " + x);
    check(IOUtils.readAllBytes(in).length == 0,
          "Expected readAllBytes to return empty byte array");
    check(expectedLength == readBytes.length,
          "Expected length " + expectedLength + ", got " + readBytes.length);
    check(Arrays.equals(expectedBytes, readBytes),
          "Expected[" + expectedBytes + "], got:[" + readBytes + "]");
    check(!in.isClosed(), "Stream unexpectedly closed");
}
 
Example 7
Source File: TimestampCheck.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void checkTimestamp(String file, String policyId, String digestAlg)
        throws Exception {
    try (JarFile jf = new JarFile(file)) {
        JarEntry je = jf.getJarEntry("META-INF/SIGNER.RSA");
        try (InputStream is = jf.getInputStream(je)) {
            byte[] content = IOUtils.readAllBytes(is);
            PKCS7 p7 = new PKCS7(content);
            SignerInfo[] si = p7.getSignerInfos();
            if (si == null || si.length == 0) {
                throw new Exception("Not signed");
            }
            PKCS9Attribute p9 = si[0].getUnauthenticatedAttributes()
                    .getAttribute(PKCS9Attribute.SIGNATURE_TIMESTAMP_TOKEN_OID);
            PKCS7 tsToken = new PKCS7((byte[]) p9.getValue());
            TimestampToken tt =
                    new TimestampToken(tsToken.getContentInfo().getData());
            if (!tt.getHashAlgorithm().toString().equals(digestAlg)) {
                throw new Exception("Digest alg different");
            }
            if (!tt.getPolicyID().equals(policyId)) {
                throw new Exception("policyId different");
            }
        }
    }
}
 
Example 8
Source File: MethodUtil.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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();
    try (InputStream in = new BufferedInputStream(uc.getInputStream())) {
        byte[] b = IOUtils.readAllBytes(in);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);
        return b;
    }
}
 
Example 9
Source File: ReadAllBytes.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static void test(byte[] expectedBytes) throws IOException {
    int expectedLength = expectedBytes.length;
    WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));
    byte[] readBytes = IOUtils.readAllBytes(in);

    int x;
    byte[] tmp = new byte[10];
    check((x = in.read()) == -1,
          "Expected end of stream from read(), got " + x);
    check((x = in.read(tmp)) == -1,
          "Expected end of stream from read(byte[]), got " + x);
    check((x = in.read(tmp, 0, tmp.length)) == -1,
          "Expected end of stream from read(byte[], int, int), got " + x);
    check(IOUtils.readAllBytes(in).length == 0,
          "Expected readAllBytes to return empty byte array");
    check(expectedLength == readBytes.length,
          "Expected length " + expectedLength + ", got " + readBytes.length);
    check(Arrays.equals(expectedBytes, readBytes),
          "Expected[" + expectedBytes + "], got:[" + readBytes + "]");
    check(!in.isClosed(), "Stream unexpectedly closed");
}
 
Example 10
Source File: ReadAllBytes.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
static void test(byte[] expectedBytes) throws IOException {
    int expectedLength = expectedBytes.length;
    WrapperInputStream in = new WrapperInputStream(new ByteArrayInputStream(expectedBytes));
    byte[] readBytes = IOUtils.readAllBytes(in);

    int x;
    byte[] tmp = new byte[10];
    check((x = in.read()) == -1,
          "Expected end of stream from read(), got " + x);
    check((x = in.read(tmp)) == -1,
          "Expected end of stream from read(byte[]), got " + x);
    check((x = in.read(tmp, 0, tmp.length)) == -1,
          "Expected end of stream from read(byte[], int, int), got " + x);
    check(IOUtils.readAllBytes(in).length == 0,
          "Expected readAllBytes to return empty byte array");
    check(expectedLength == readBytes.length,
          "Expected length " + expectedLength + ", got " + readBytes.length);
    check(Arrays.equals(expectedBytes, readBytes),
          "Expected[" + expectedBytes + "], got:[" + readBytes + "]");
    check(!in.isClosed(), "Stream unexpectedly closed");
}
 
Example 11
Source File: AppletClassLoader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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 = IOUtils.readAllBytes(in);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);
    } finally {
        in.close();
    }
    return b;
}
 
Example 12
Source File: JarFile.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getBytes(ZipEntry ze) throws IOException {
    try (InputStream is = super.getInputStream(ze)) {
        int len = (int)ze.getSize();
        byte[] b = IOUtils.readAllBytes(is);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);

        return b;
    }
}
 
Example 13
Source File: JarFile.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getBytes(ZipEntry ze) throws IOException {
    try (InputStream is = super.getInputStream(ze)) {
        int len = (int)ze.getSize();
        byte[] b = IOUtils.readAllBytes(is);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);

        return b;
    }
}
 
Example 14
Source File: AppletClassLoader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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 = IOUtils.readAllBytes(in);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);
    } finally {
        in.close();
    }
    return b;
}
 
Example 15
Source File: AppletClassLoader.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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 = IOUtils.readAllBytes(in);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);
    } finally {
        in.close();
    }
    return b;
}
 
Example 16
Source File: JarFile.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getBytes(ZipEntry ze) throws IOException {
    try (InputStream is = super.getInputStream(ze)) {
        int len = (int)ze.getSize();
        byte[] b = IOUtils.readAllBytes(is);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);

        return b;
    }
}
 
Example 17
Source File: JarFile.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getBytes(ZipEntry ze) throws IOException {
    try (InputStream is = super.getInputStream(ze)) {
        int len = (int)ze.getSize();
        byte[] b = IOUtils.readAllBytes(is);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);

        return b;
    }
}
 
Example 18
Source File: AppletClassLoader.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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 = IOUtils.readAllBytes(in);
        if (len != -1 && b.length != len)
            throw new EOFException("Expected:" + len + ", read:" + b.length);
    } finally {
        in.close();
    }
    return b;
}
 
Example 19
Source File: VictimClassLoader.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
static byte[] readFully(java.io.InputStream in, int len) throws java.io.IOException {
    byte[] b = IOUtils.readAllBytes(in);
    if (len != -1 && b.length != len)
        throw new java.io.IOException("Expected:" + len + ", actual:" + b.length);
    return b;
}
 
Example 20
Source File: VictimClassLoader.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static byte[] readFully(java.io.InputStream in, int len) throws java.io.IOException {
    byte[] b = IOUtils.readAllBytes(in);
    if (len != -1 && b.length != len)
        throw new java.io.IOException("Expected:" + len + ", actual:" + b.length);
    return b;
}