Datasets:

ArXiv:
License:
File size: 2,533 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package httpcli.io;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public final class IOUtils {

  public static final ByteArrayPool POOL = new ByteArrayPool(4096);

  private IOUtils() {
  }
  
  public static void copy(File file, OutputStream out) throws IOException {
    FileInputStream in = null;
    byte[] buffer = null;
    try {
      in = new FileInputStream(file);
      int bytesAvailable = in.available();
      
      int maxBufferSize = 1024 * 1024;
      int bufferSize = Math.min(bytesAvailable, maxBufferSize);
      buffer = POOL.getBuf(bufferSize);
      
      int count = in.read(buffer, 0, bufferSize);
      while (count > 0) {
        out.write(buffer, 0, bufferSize);
        bytesAvailable = in.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        count = in.read(buffer, 0, bufferSize);
      }
    } finally {
      POOL.returnBuf(buffer);
      closeQuietly(in);
    }
  }

  public static void copy(InputStream source, OutputStream out) throws IOException {
    if (source == null) throw new IOException("source == null");
    byte[] buffer = POOL.getBuf(1024);
    try {
      int count;
      while ((count = source.read(buffer)) != -1) {
        out.write(buffer, 0, count);
      }
    } finally {
      POOL.returnBuf(buffer);
    }
  }
  
  public static PoolingByteArrayOutputStream arrayOutputStream() {
    return new PoolingByteArrayOutputStream(POOL);
  }
  public static PoolingByteArrayOutputStream arrayOutputStream(int size) {
    return new PoolingByteArrayOutputStream(POOL, size);
  }
  
  public static byte[] toByteArray(InputStream source) throws IOException {
    return toByteArray(source, 1024);
  }
  public static byte[] toByteArray(InputStream source, int size) throws IOException {
    if (source == null) throw new IOException("source == null");
    PoolingByteArrayOutputStream bytes
            = new PoolingByteArrayOutputStream(POOL, source.available());
    byte[] buffer = POOL.getBuf(size);
    try {
      int count;
      while ((count = source.read(buffer)) != -1) {
        bytes.write(buffer, 0, count);
      }
      return bytes.toByteArray();
    } finally {
      POOL.returnBuf(buffer);
      bytes.close();
    }
  }

  public static void closeQuietly(Closeable closeable) {
    if (closeable == null) return;
    try {
      closeable.close();
    } catch (IOException ignore) {
      // empty
    }
  }
}