ioutil.java

来自「一个java工作流引擎」· Java 代码 · 共 47 行

JAVA
47
字号
package org.jbpm.util.io;

import java.io.*;

public class IoUtil {
  
  private static final int BUFFER_SIZE = 512;

	public static int transfer(InputStream in, OutputStream out) throws IOException {
    int total = 0;
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = in.read( buffer );
    while ( bytesRead != -1 ) {
      out.write( buffer, 0, bytesRead );
      total += bytesRead;
      bytesRead = in.read( buffer );
    }
    return total;
	}
  
  public static byte[] readStream(InputStream in) throws IOException {
    byte[] fileContents = null;
    int fileSize = 0;

    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = in.read(buffer);

    while (bytesRead != -1) {

      byte[] newFileContents = new byte[fileSize + bytesRead];

      if (fileSize > 0) {
        System.arraycopy(fileContents, 0, newFileContents, 0, fileSize);
      }
      System.arraycopy(buffer, 0, newFileContents, fileSize, bytesRead);

      fileContents = newFileContents;
      fileSize += bytesRead;

      bytesRead = in.read(buffer);
    }

    return fileContents;
  }
  
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?