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

The following examples show how to use java.io.InputStream#readAllBytes() . These examples are extracted from open source projects. 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
@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
@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 3
Source Project: SikuliX1   File: Element.java    License: 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 4
Source Project: Bytecoder   File: MethodUtil.java    License: 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 5
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 6
@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 7
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 8
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 9
Source Project: helidon-build-tools   File: ExecTest.java    License: 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 10
Source Project: helidon-build-tools   File: TestUtils.java    License: 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 11
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 Project: openjdk-jdk9   File: Main.java    License: 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
/**
 * 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
@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
@Test
void testMissingNonce() throws IOException {
  InputStream input = GenesisFileTest.class.getResourceAsStream("/missing-nonce.json");
  byte[] contents = input.readAllBytes();
  assertThrows(IllegalArgumentException.class, () -> GenesisFile.read(contents));
}
 
Example 16
@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 Project: andesite-node   File: JattachDebug.java    License: 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 18
@Test
public void givenUsingPlainJava9_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
    final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });

    byte[] data = is.readAllBytes();
}
 
Example 19
Source Project: blog-tutorials   File: BadPracticeFileReader.java    License: 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 20
private String inputStreamToString(InputStream in) throws IOException {
    return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}