summaryrefslogtreecommitdiff
path: root/hadoop-common-project
diff options
context:
space:
mode:
Diffstat (limited to 'hadoop-common-project')
-rw-r--r--hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java20
1 files changed, 20 insertions, 0 deletions
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java
index a4339e64aa..326020c342 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java
@@ -445,4 +445,24 @@ public class IOUtils {
throw ioe;
}
}
+
+ /**
+ * Reads a DataInput until EOF and returns a byte array. Make sure not to
+ * pass in an infinite DataInput or this will never return.
+ *
+ * @param in A DataInput
+ * @return a byte array containing the data from the DataInput
+ * @throws IOException on I/O error, other than EOF
+ */
+ public static byte[] readFullyToByteArray(DataInput in) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try {
+ while (true) {
+ baos.write(in.readByte());
+ }
+ } catch (EOFException eof) {
+ // finished reading, do nothing
+ }
+ return baos.toByteArray();
+ }
}