2010-12-18 10:18:43 +00:00
|
|
|
/* ====================================================================
|
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
|
The ASF licenses this file to You 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 org.apache.poi.poifs.nio;
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A POIFS {@link DataSource} backed by a byte array.
|
|
|
|
|
*/
|
|
|
|
|
public class ByteArrayBackedDataSource extends DataSource {
|
|
|
|
|
private byte[] buffer;
|
|
|
|
|
private long size;
|
|
|
|
|
|
|
|
|
|
public ByteArrayBackedDataSource(byte[] data) {
|
|
|
|
|
this.buffer = data;
|
|
|
|
|
this.size = data.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void read(ByteBuffer dst, long position) {
|
2010-12-19 04:59:49 +00:00
|
|
|
if(position >= size) {
|
2010-12-18 10:18:43 +00:00
|
|
|
throw new IndexOutOfBoundsException(
|
|
|
|
|
"Unable to read " + dst.capacity() + " bytes from " +
|
|
|
|
|
position + " in stream of length " + size
|
|
|
|
|
);
|
|
|
|
|
}
|
2010-12-19 04:59:49 +00:00
|
|
|
|
|
|
|
|
int toRead = (int)Math.min(dst.capacity(), size - position);
|
|
|
|
|
dst.put(buffer, (int)position, toRead);
|
2010-12-18 10:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void write(ByteBuffer src, long position) {
|
|
|
|
|
// Extend if needed
|
|
|
|
|
long endPosition = position + src.capacity();
|
|
|
|
|
if(endPosition > buffer.length) {
|
|
|
|
|
extend(endPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now copy
|
|
|
|
|
src.get(buffer, (int)position, src.capacity());
|
|
|
|
|
|
|
|
|
|
// Update size if needed
|
|
|
|
|
if(endPosition > size) {
|
|
|
|
|
size = endPosition;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void extend(long length) {
|
|
|
|
|
// Consider extending by a bit more than requested
|
|
|
|
|
long difference = length - buffer.length;
|
|
|
|
|
if(difference < buffer.length*0.25) {
|
|
|
|
|
difference = (long)(buffer.length*0.25);
|
|
|
|
|
}
|
|
|
|
|
if(difference < 4096) {
|
|
|
|
|
difference = 4096;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] nb = new byte[(int)(difference+buffer.length)];
|
|
|
|
|
System.arraycopy(buffer, 0, nb, 0, (int)size);
|
|
|
|
|
buffer = nb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long size() {
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
|
buffer = null;
|
|
|
|
|
size = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|