-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
DataUtil.java
115 lines (102 loc) · 4.05 KB
/
DataUtil.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright 2015-2016 Austin Keener & Michael Ritter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.utils;
import org.apache.http.util.Args;
import java.io.*;
public class DataUtil
{
/**
* Used as an alternate to Java's nio Files.readAllBytes.
* <p>
* This customized version for File is provide (instead of just using {@link #readFully(java.io.InputStream)} with a FileInputStream)
* because
* with a File we can determine the total size of the array and do not need to have a buffer. This results
* in a memory footprint that is half the size of {@link #readFully(java.io.InputStream)}
* <p>
* Code provided from <a href="http://stackoverflow.com/a/6276139">http://stackoverflow.com/a/6276139</a>
*
* @param file
* The file from which we should retrieve the bytes from
* @return
* A byte[] containing all of the file's data
* @throws IOException
* Thrown if there is a problem while reading the file.
*/
public static byte[] readFully(File file) throws IOException
{
Args.notNull(file, "File");
Args.check(file.exists(), "Provided file does not exist!");
try (InputStream is = new FileInputStream(file))
{
// Get the size of the file
long 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)
{
throw new IOException("Cannot read the file into memory completely due to it being too large!");
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int 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 in
if (offset < bytes.length)
{
throw new IOException("Could not completely read file " + file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
}
/**
* Provided as a simple way to fully read an InputStream into a byte[].
* <p>
* This method will block until the InputStream has been fully read, so if you provide an InputStream that is
* non-finite, you're gonna have a bad time.
*
* @param stream
* The Stream to be read.
* @return
* A byte[] containing all of the data provided by the InputStream
* @throws IOException
* If the first byte cannot be read for any reason other than the end of the file,
* if the input stream has been closed, or if some other I/O error occurs.
*/
public static byte[] readFully(InputStream stream) throws IOException
{
Args.notNull(stream, "InputStream");
byte[] buffer = new byte[1024];
try (ByteArrayOutputStream bos = new ByteArrayOutputStream())
{
int readAmount = 0;
while ((readAmount = stream.read(buffer)) != -1)
{
bos.write(buffer, 0, readAmount);
}
return bos.toByteArray();
}
}
}