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

The following examples show how to use java.io.InputStream#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: MultiAuthTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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
Source File: MethodUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
protected Class<?> findClass(final String name)
    throws ClassNotFoundException
{
    if (!name.startsWith(MISC_PKG)) {
        throw new ClassNotFoundException(name);
    }
    String path = name.replace('.', '/').concat(".class");
    try {
        InputStream in = Object.class.getModule().getResourceAsStream(path);
        if (in != null) {
            try (in) {
                byte[] b = in.readAllBytes();
                return defineClass(name, b);
            }
        }
    } catch (IOException e) {
        throw new ClassNotFoundException(name, e);
    }

    throw new ClassNotFoundException(name);
}
 
Example 3
Source File: MethodUtil.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
protected Class<?> findClass(final String name)
    throws ClassNotFoundException
{
    if (!name.startsWith(MISC_PKG)) {
        throw new ClassNotFoundException(name);
    }
    String path = name.replace('.', '/').concat(".class");
    try {
        InputStream in = Object.class.getModule().getResourceAsStream(path);
        if (in != null) {
            try (in) {
                byte[] b = in.readAllBytes();
                return defineClass(name, b);
            }
        }
    } catch (IOException e) {
        throw new ClassNotFoundException(name, e);
    }

    throw new ClassNotFoundException(name);
}
 
Example 4
Source File: Element.java    From SikuliX1 with MIT License 6 votes vote down vote up
private static Mat getMatFromURL(URL url, boolean isMaskImage) {
  byte[] bytes = null;
  Mat content = new Mat();
  try {
    InputStream inputStream = url.openStream();
    bytes = inputStream.readAllBytes();
  } catch (IOException e) {
  }
  if (bytes != null) {
    MatOfByte matOfByte = new MatOfByte();
    matOfByte.fromArray(bytes);
    content = Imgcodecs.imdecode(matOfByte, -1);
    if (isMaskImage) {
      List<Mat> mats = SXOpenCV.extractMask(content, false);
      content = mats.get(1);
    }
  }
  return content;
}
 
Example 5
Source File: BodyTest.java    From cute-proxy with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
@Ignore
public void write() throws Exception {
    Body body = new Body(BodyType.binary, null, "");
    byte[] data = new byte[1024];
    for (int i = 0; i < data.length; i++) {
        data[i] = (byte) i;
    }
    for (int i = 0; i < 520; i++) {
        body.append(ByteBuffer.wrap(data));
    }
    body.finish();
    InputStream inputStream = body.getDecodedInputStream();
    byte[] bytes = inputStream.readAllBytes();
    assertEquals(520 * 1024, bytes.length);
}
 
Example 6
Source File: TestClassLoader.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] readTestClassBytes() {
    try {
        String classFileName = "jdk/jfr/event/oldobject/TestClassLoader$TestClass0000000.class";
        InputStream is = TestClassLoader.class.getClassLoader().getResourceAsStream(classFileName);
        if (is == null) {
            throw new RuntimeException("Culd not find class file " + classFileName);
        }
        byte[] b = is.readAllBytes();
        is.close();
        return b;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        throw new RuntimeException(ioe);
    }
}
 
Example 7
Source File: Loader.java    From java-almanac with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
private static byte[] downgrade(InputStream in) throws IOException {
	byte[] buffer = in.readAllBytes();
	int version = (((buffer[6] & 0xFF) << 8) | (buffer[7] & 0xFF));
	if (version > MAX_VERSION) {
		buffer[6] = (byte) (MAX_VERSION >>> 8);
		buffer[7] = (MAX_VERSION);
	}
	return buffer;
}
 
Example 8
Source File: ExecTest.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
static String resourceAsString(String name) {
    InputStream is = ExecTest.class.getResourceAsStream(name);
    try {
        return new String(is.readAllBytes(), StandardCharsets.UTF_8);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 9
Source File: TestUtils.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
static String resourceAsString(String name) {
    InputStream is = TestUtils.class.getResourceAsStream(name);
    try {
        return new String(is.readAllBytes(), StandardCharsets.UTF_8);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 10
Source File: GenesisFileTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testReadGenesisFile() throws IOException {
  InputStream input = GenesisFileTest.class.getResourceAsStream("/valid-genesis.json");
  byte[] contents = input.readAllBytes();
  GenesisFile file = GenesisFile.read(contents);
  assertNotNull(file.toBlock());
  assertEquals(0, file.toBlock().getBody().getTransactions().size());
}
 
Example 11
Source File: RedefineClassWithNativeMethodAgent.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static String readAllAsString(InputStream in) throws IOException {
    if (in == null)
        return null;
    try (in) {
        return new String(in.readAllBytes(), "UTF-8");
    }
}
 
Example 13
Source File: ModuleReaderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test ModuleReader#open
 */
void testOpen(ModuleReader reader, String name, byte[] expectedBytes)
    throws IOException
{
    Optional<InputStream> oin = reader.open(name);
    assertTrue(oin.isPresent());

    InputStream in = oin.get();
    try (in) {
        byte[] bytes = in.readAllBytes();
        assertTrue(Arrays.equals(bytes, expectedBytes));
    }
}
 
Example 14
Source File: FolderObserverTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean addOrRefreshModel(String name, InputStream inputStream) {
    calledFileName = name;
    isAddOrRefreshModelMethodCalled = true;
    try {
        fileContent = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
        inputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return true;
}
 
Example 15
Source File: JattachDebug.java    From andesite-node with MIT License 4 votes vote down vote up
private static String toUTF8(InputStream in) throws IOException {
    return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
 
Example 16
Source File: GenesisFileTest.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
@Test
void testMissingDifficulty() throws IOException {
  InputStream input = GenesisFileTest.class.getResourceAsStream("/missing-difficulty.json");
  byte[] contents = input.readAllBytes();
  assertThrows(IllegalArgumentException.class, () -> GenesisFile.read(contents));
}
 
Example 17
Source File: InputStreamToByteArrayUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenUsingPlainJava9_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
    final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });

    byte[] data = is.readAllBytes();
}
 
Example 18
Source File: BadPracticeFileReader.java    From blog-tutorials with MIT License 4 votes vote down vote up
@Override
public void run(String... args) throws Exception {
    InputStream in = this.getClass().getResourceAsStream("/message.txt");
    byte[] allBytes = in.readAllBytes();
    System.out.println(new String(allBytes));
}
 
Example 19
Source File: GenesisFileTest.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
@Test
void testMissingNonce() throws IOException {
  InputStream input = GenesisFileTest.class.getResourceAsStream("/missing-nonce.json");
  byte[] contents = input.readAllBytes();
  assertThrows(IllegalArgumentException.class, () -> GenesisFile.read(contents));
}
 
Example 20
Source File: Stream2JSONInputStreamTest.java    From openhab-core with Eclipse Public License 2.0 4 votes vote down vote up
private String inputStreamToString(InputStream in) throws IOException {
    return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}