Java/File Input Output/ByteArrayInputStream
Содержание
ByteArrayInputStream Reset
<source lang="java">
import java.io.ByteArrayInputStream; import java.io.IOException; class MainClass {
public static void main(String args[]) throws IOException { String tmp = "abc"; byte b[] = tmp.getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(b); for (int i = 0; i < 2; i++) { int c; while ((c = in.read()) != -1) { if (i == 0) { System.out.print((char) c); } else { System.out.print(Character.toUpperCase((char) c)); } } System.out.println(); in.reset(); } }
}
</source>
Byte Array IO
<source lang="java">
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class ByteArrayIOApp {
public static void main(String args[]) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); String s = "This is a test."; for (int i = 0; i < s.length(); ++i) outStream.write(s.charAt(i)); System.out.println("outstream: " + outStream); System.out.println("size: " + outStream.size()); ByteArrayInputStream inStream; inStream = new ByteArrayInputStream(outStream.toByteArray()); int inBytes = inStream.available(); System.out.println("inStream has " + inBytes + " available bytes"); byte inBuf[] = new byte[inBytes]; int bytesRead = inStream.read(inBuf, 0, inBytes); System.out.println(bytesRead + " bytes were read"); System.out.println("They are: " + new String(inBuf)); }
}
</source>
Convert string into InputStream using ByteArrayInputStream class.
<source lang="java">
import java.io.ByteArrayInputStream; import java.io.InputStream; public class Main {
public static void main(String[] args)throws Exception { String text = "Converting String to InputStream Example"; InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8")); }
}
</source>
Demonstrate ByteArrayInputStream
<source lang="java">
import java.io.ByteArrayInputStream; import java.io.IOException; class ByteArrayInputStreamDemo {
public static void main(String args[]) throws IOException { String tmp = "abcdefghijklmnopqrstuvwxyz"; byte b[] = tmp.getBytes(); ByteArrayInputStream input1 = new ByteArrayInputStream(b); ByteArrayInputStream input2 = new ByteArrayInputStream(b, 0, 3); }
}
</source>
Read ByteArrayInputStream to String
<source lang="java">
import java.io.ByteArrayInputStream; /*
* The contents of this file are subject to the terms * of the Common Development and Distribution License * (the "License"). You may not use this file except * in compliance with the License. * * You can obtain a copy of the license at * glassfish/bootstrap/legal/CDDLv1.0.txt or * https://glassfish.dev.java.net/public/CDDLv1.0.html. * See the License for the specific language governing * permissions and limitations under the License. * * When distributing Covered Code, include this CDDL * HEADER in each file and include the License file at * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable, * add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your * own identifying information: Portions Copyright [yyyy] * [name of copyright owner] */
/*
* @(#)ASCIIUtility.java 1.10 05/08/29 * * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. */
public class Utils {
public static String toString(ByteArrayInputStream is) { int size = is.available(); char[] theChars = new char[size]; byte[] bytes = new byte[size]; is.read(bytes, 0, size); for (int i = 0; i < size;) theChars[i] = (char)(bytes[i++]&0xff); return new String(theChars); }
}
</source>