You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ByteArrayBackedDataSource.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.poifs.nio;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.nio.ByteBuffer;
  19. /**
  20. * A POIFS {@link DataSource} backed by a byte array.
  21. */
  22. public class ByteArrayBackedDataSource extends DataSource {
  23. private byte[] buffer;
  24. private long size;
  25. public ByteArrayBackedDataSource(byte[] data, int size) {
  26. this.buffer = data;
  27. this.size = size;
  28. }
  29. public ByteArrayBackedDataSource(byte[] data) {
  30. this(data, data.length);
  31. }
  32. public ByteBuffer read(int length, long position) {
  33. if(position >= size) {
  34. throw new IndexOutOfBoundsException(
  35. "Unable to read " + length + " bytes from " +
  36. position + " in stream of length " + size
  37. );
  38. }
  39. int toRead = (int)Math.min(length, size - position);
  40. return ByteBuffer.wrap(buffer, (int)position, toRead);
  41. }
  42. public void write(ByteBuffer src, long position) {
  43. // Extend if needed
  44. long endPosition = position + src.capacity();
  45. if(endPosition > buffer.length) {
  46. extend(endPosition);
  47. }
  48. // Now copy
  49. src.get(buffer, (int)position, src.capacity());
  50. // Update size if needed
  51. if(endPosition > size) {
  52. size = endPosition;
  53. }
  54. }
  55. private void extend(long length) {
  56. // Consider extending by a bit more than requested
  57. long difference = length - buffer.length;
  58. if(difference < buffer.length*0.25) {
  59. difference = (long)(buffer.length*0.25);
  60. }
  61. if(difference < 4096) {
  62. difference = 4096;
  63. }
  64. byte[] nb = new byte[(int)(difference+buffer.length)];
  65. System.arraycopy(buffer, 0, nb, 0, (int)size);
  66. buffer = nb;
  67. }
  68. public void copyTo(OutputStream stream) throws IOException {
  69. stream.write(buffer, 0, (int)size);
  70. }
  71. public long size() {
  72. return size;
  73. }
  74. public void close() {
  75. buffer = null;
  76. size = -1;
  77. }
  78. }