下面列出了java.io.InputStream#readAllBytes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
}
@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);
}
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;
}
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);
}
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);
}
@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());
}
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);
}
}
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;
}
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);
}
}
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);
}
}
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);
}
static String readAllAsString(InputStream in) throws IOException {
if (in == null)
return null;
try (in) {
return new String(in.readAllBytes(), "UTF-8");
}
}
/**
* 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));
}
}
@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;
}
@Test
void testMissingNonce() throws IOException {
InputStream input = GenesisFileTest.class.getResourceAsStream("/missing-nonce.json");
byte[] contents = input.readAllBytes();
assertThrows(IllegalArgumentException.class, () -> GenesisFile.read(contents));
}
@Test
void testMissingDifficulty() throws IOException {
InputStream input = GenesisFileTest.class.getResourceAsStream("/missing-difficulty.json");
byte[] contents = input.readAllBytes();
assertThrows(IllegalArgumentException.class, () -> GenesisFile.read(contents));
}
private static String toUTF8(InputStream in) throws IOException {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
@Test
public void givenUsingPlainJava9_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
byte[] data = is.readAllBytes();
}
@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));
}
private String inputStreamToString(InputStream in) throws IOException {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}