InputStream is = ...ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {buffer.write(data, 0, nRead);}
return buffer.toByteArray();
Input Stream is ...ByteArrayOutputStream bos = new ByteArrayOutputStream();int next = in.read();while (next > -1) {bos.write(next);next = in.read();}bos.flush();byte[] result = bos.toByteArray();bos.close();
/*InputStream class_InputStream = null;I am reading class from DBclass_InputStream = rs.getBinaryStream(1);Your Input stream could be from any source*/int thisLine;ByteArrayOutputStream bos = new ByteArrayOutputStream();while ((thisLine = class_InputStream.read()) != -1) {bos.write(thisLine);}bos.flush();byte [] yourBytes = bos.toByteArray();
/*Don't forget in the finally block to close ByteArrayOutputStream & InputStreamIn my case the IS is from resultset so just closing the rs will do it*/
if (bos != null){bos.close();}
// Returns the contents of the file in a byte array.public static byte[] getBytesFromFile(File file) throws IOException {InputStream is = new FileInputStream(file);
// Get the size of the filelong length = file.length();
// You cannot create an array using a long type.// It needs to be an int type.// Before converting to an int type, check// to ensure that file is not larger than Integer.MAX_VALUE.if (length > Integer.MAX_VALUE) {// File is too large}
// Create the byte array to hold the databyte[] bytes = new byte[(int)length];
// Read in the bytesint offset = 0;int numRead = 0;while (offset < bytes.length&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {offset += numRead;}
// Ensure all the bytes have been read inif (offset < bytes.length) {throw new IOException("Could not completely read file "+file.getName());}
// Close the input stream and return bytesis.close();return bytes;}
ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buffer = new byte[1024]; // you can configure the buffer sizeint length;
while ((length = in.read(buffer)) != -1) out.write(buffer, 0, length); //copy streamsin.close(); // call this in a finally block
byte[] result = out.toByteArray();
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {ByteArrayOutputStream os = new ByteArrayOutputStream();byte[] buffer = new byte[0xFFFF];for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {os.write(buffer, 0, len);}return os.toByteArray();}
ByteArrayOutputStream out = new ByteArrayOutputStream();byte[] buffer = new byte[1024];while (true) {int r = in.read(buffer);if (r == -1) break;out.write(buffer, 0, r);}
byte[] ret = out.toByteArray();
/*** method converts {@link InputStream} Object into byte[] array.** @param stream the {@link InputStream} Object.* @return the byte[] array representation of received {@link InputStream} Object.* @throws IOException if an error occurs.*/public static byte[] streamToByteArray(InputStream stream) throws IOException {
byte[] buffer = new byte[1024];ByteArrayOutputStream os = new ByteArrayOutputStream();
int line = 0;// read bytes from stream, and store them in bufferwhile ((line = stream.read(buffer)) != -1) {// Writes bytes from byte array (buffer) into output stream.os.write(buffer, 0, line);}stream.close();os.flush();os.close();return os.toByteArray();}
/*** Begin setup TCP connection to PC app* to open integrate connection between mobile app and pc app (or mobile app)*/mSocket = new Socket(IP, port);// mSocket.setSoTimeout(30000);
DataOutputStream mDos = new DataOutputStream(mSocket.getOutputStream());
String str = "MobileRequest#" + params[0] + "#<EOF>";
mDos.write(str.getBytes());
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
/* Since data are accepted as byte, all of them will be collected in thefollowing byte array which initialised with accepted data length. */DataInputStream mDis = new DataInputStream(mSocket.getInputStream());byte[] data = new byte[mDis.available()];
// Collecting data into byte arrayfor (int i = 0; i < data.length; i++)data[i] = mDis.readByte();
// Converting collected data in byte array into String.String RESPONSE = new String(data);
// Read the file contents into a byte[] arraybyte[] buf = new byte[inputStreamLength];int bytesRead = Math.max(0, inputStream.read(buf));
// If needed: for safety, truncate the array if the file may somehow get// truncated during the read operationbyte[] contents = bytesRead == inputStreamLength ? buf: Arrays.copyOf(buf, bytesRead);
@SuppressWarnings("empty-statement")public static byte[] inputStreamToByte(InputStream is) throws IOException {if (is == null) {return null;}// Define a size if you have an idea of it.ByteArrayOutputStream r = new ByteArrayOutputStream(2048);byte[] read = new byte[512]; // Your buffer size.for (int i; -1 != (i = is.read(read)); r.write(read, 0, i));is.close();return r.toByteArray();}
byte[] data = new byte[(int) file.length()];DataInputStream dis = new DataInputStream(new FileInputStream(file));dis.readFully(data);dis.close();
字节数组输出流
InputStream is = new FileInputStream(file);ByteArrayOutputStream buffer = new ByteArrayOutputStream();int nRead;byte[] data = new byte[(int) file.length()];while ((nRead = is.read(data, 0, data.length)) != -1) {buffer.write(data, 0, nRead);}
随机访问文件类型
RandomAccessFile raf = new RandomAccessFile(file, "r");byte[] data = new byte[(int) raf.length()];raf.readFully(data);
fun getStreamLengthFromUri(context: Context, uri: Uri): Long {context.contentResolver.query(uri, arrayOf(MediaStore.MediaColumns.SIZE), null, null, null)?.use {if (!it.moveToNext())return@useval fileSize = it.getLong(it.getColumnIndex(MediaStore.MediaColumns.SIZE))if (fileSize > 0)return fileSize}//if you wish, you can also get the file-path from the uri here, and then try to get its size, using this: https://stackoverflow.com/a/61835665/878126FileUtilEx.getFilePathFromUri(context, uri, false)?.use {val file = it.fileval fileSize = file.length()if (fileSize > 0)return fileSize}context.contentResolver.openInputStream(uri)?.use { inputStream ->if (inputStream is FileInputStream)return inputStream.channel.size()else {var bytesCount = 0Lwhile (true) {val available = inputStream.available()if (available == 0)breakval skip = inputStream.skip(available.toLong())if (skip < 0)breakbytesCount += skip}if (bytesCount > 0L)return bytesCount}}return -1L}