Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IOUtils.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.nio.ByteBuffer;
  21. import java.nio.channels.ReadableByteChannel;
  22. import java.util.zip.CRC32;
  23. import java.util.zip.Checksum;
  24. public final class IOUtils {
  25. private IOUtils() {
  26. // no instances of this class
  27. }
  28. /**
  29. * Reads all the data from the input stream, and returns the bytes read.
  30. */
  31. public static byte[] toByteArray(InputStream stream) throws IOException {
  32. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  33. byte[] buffer = new byte[4096];
  34. int read = 0;
  35. while (read != -1) {
  36. read = stream.read(buffer);
  37. if (read > 0) {
  38. baos.write(buffer, 0, read);
  39. }
  40. }
  41. return baos.toByteArray();
  42. }
  43. /**
  44. * Returns an array (that shouldn't be written to!) of the
  45. * ByteBuffer. Will be of the requested length, or possibly
  46. * longer if that's easier.
  47. */
  48. public static byte[] toByteArray(ByteBuffer buffer, int length) {
  49. if(buffer.hasArray() && buffer.arrayOffset() == 0) {
  50. // The backing array should work out fine for us
  51. return buffer.array();
  52. }
  53. byte[] data = new byte[length];
  54. buffer.get(data);
  55. return data;
  56. }
  57. /**
  58. * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
  59. */
  60. public static int readFully(InputStream in, byte[] b) throws IOException {
  61. return readFully(in, b, 0, b.length);
  62. }
  63. /**
  64. * Same as the normal <tt>in.read(b, off, len)</tt>, but tries to ensure
  65. * that the entire len number of bytes is read.
  66. * <p>
  67. * If the end of file is reached before any bytes are read, returns -1. If
  68. * the end of the file is reached after some bytes are read, returns the
  69. * number of bytes read. If the end of the file isn't reached before len
  70. * bytes have been read, will return len bytes.
  71. */
  72. public static int readFully(InputStream in, byte[] b, int off, int len) throws IOException {
  73. int total = 0;
  74. while (true) {
  75. int got = in.read(b, off + total, len - total);
  76. if (got < 0) {
  77. return (total == 0) ? -1 : total;
  78. }
  79. total += got;
  80. if (total == len) {
  81. return total;
  82. }
  83. }
  84. }
  85. /**
  86. * Same as the normal <tt>channel.read(b)</tt>, but tries to ensure
  87. * that the entire len number of bytes is read.
  88. * <p>
  89. * If the end of file is reached before any bytes are read, returns -1. If
  90. * the end of the file is reached after some bytes are read, returns the
  91. * number of bytes read. If the end of the file isn't reached before len
  92. * bytes have been read, will return len bytes.
  93. */
  94. public static int readFully(ReadableByteChannel channel, ByteBuffer b) throws IOException {
  95. int total = 0;
  96. while (true) {
  97. int got = channel.read(b);
  98. if (got < 0) {
  99. return (total == 0) ? -1 : total;
  100. }
  101. total += got;
  102. if (total == b.capacity() || b.position() == b.capacity()) {
  103. return total;
  104. }
  105. }
  106. }
  107. /**
  108. * Copies all the data from the given InputStream to the OutputStream. It
  109. * leaves both streams open, so you will still need to close them once done.
  110. */
  111. public static void copy(InputStream inp, OutputStream out) throws IOException {
  112. byte[] buff = new byte[4096];
  113. int count;
  114. while ((count = inp.read(buff)) != -1) {
  115. if (count > 0) {
  116. out.write(buff, 0, count);
  117. }
  118. }
  119. }
  120. public static long calculateChecksum(byte[] data) {
  121. Checksum sum = new CRC32();
  122. sum.update(data, 0, data.length);
  123. return sum.getValue();
  124. }
  125. }