2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05: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
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
==================================================================== */
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
package org.apache.poi.poifs.storage;
|
|
|
|
|
2002-03-14 21:47:56 -05:00
|
|
|
import org.apache.poi.poifs.common.POIFSConstants;
|
2003-07-31 15:35:19 -04:00
|
|
|
import org.apache.poi.util.IOUtils;
|
2008-03-06 10:54:06 -05:00
|
|
|
import org.apache.poi.util.POILogFactory;
|
|
|
|
import org.apache.poi.util.POILogger;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A big block created from an InputStream, holding the raw data
|
|
|
|
*
|
|
|
|
* @author Marc Johnson (mjohnson at apache dot org
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class RawDataBlock
|
|
|
|
implements ListManagedBlock
|
|
|
|
{
|
|
|
|
private byte[] _data;
|
|
|
|
private boolean _eof;
|
2008-03-06 10:54:06 -05:00
|
|
|
private static POILogger log = POILogFactory.getLogger(RawDataBlock.class);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor RawDataBlock
|
|
|
|
*
|
|
|
|
* @param stream the InputStream from which the data will be read
|
|
|
|
*
|
|
|
|
* @exception IOException on I/O errors, and if an insufficient
|
2008-01-09 11:31:25 -05:00
|
|
|
* amount of data is read (the InputStream must
|
|
|
|
* be an exact multiple of the block size)
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
|
|
|
public RawDataBlock(final InputStream stream)
|
2008-01-09 11:31:25 -05:00
|
|
|
throws IOException {
|
|
|
|
this(stream, POIFSConstants.BIG_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Constructor RawDataBlock
|
|
|
|
*
|
|
|
|
* @param stream the InputStream from which the data will be read
|
|
|
|
* @param blockSize the size of the POIFS blocks, normally 512 bytes {@link POIFSConstants#BIG_BLOCK_SIZE}
|
|
|
|
*
|
|
|
|
* @exception IOException on I/O errors, and if an insufficient
|
|
|
|
* amount of data is read (the InputStream must
|
|
|
|
* be an exact multiple of the block size)
|
|
|
|
*/
|
|
|
|
public RawDataBlock(final InputStream stream, int blockSize)
|
|
|
|
throws IOException {
|
|
|
|
_data = new byte[ blockSize ];
|
2003-07-31 15:35:19 -04:00
|
|
|
int count = IOUtils.readFully(stream, _data);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2008-01-09 11:31:25 -05:00
|
|
|
if (count == -1) {
|
2002-01-30 21:22:28 -05:00
|
|
|
_eof = true;
|
|
|
|
}
|
2008-01-09 11:31:25 -05:00
|
|
|
else if (count != blockSize) {
|
|
|
|
// IOUtils.readFully will always read the
|
|
|
|
// requested number of bytes, unless it hits
|
|
|
|
// an EOF
|
|
|
|
_eof = true;
|
2002-01-30 21:22:28 -05:00
|
|
|
String type = " byte" + ((count == 1) ? ("")
|
|
|
|
: ("s"));
|
|
|
|
|
2008-03-06 10:54:06 -05:00
|
|
|
log.log(POILogger.ERROR,
|
|
|
|
"Unable to read entire block; " + count
|
|
|
|
+ type + " read before EOF; expected "
|
|
|
|
+ blockSize + " bytes. Your document"
|
|
|
|
+ " has probably been truncated!"
|
|
|
|
);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2008-01-09 11:31:25 -05:00
|
|
|
else {
|
2002-01-30 21:22:28 -05:00
|
|
|
_eof = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When we read the data, did we hit end of file?
|
|
|
|
*
|
|
|
|
* @return true if no data was read because we were at the end of
|
|
|
|
* the file, else false
|
|
|
|
*
|
|
|
|
* @exception IOException
|
|
|
|
*/
|
|
|
|
public boolean eof()
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
return _eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ********** START implementation of ListManagedBlock ********** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the data from the block
|
|
|
|
*
|
|
|
|
* @return the block's data as a byte array
|
|
|
|
*
|
|
|
|
* @exception IOException if there is no data
|
|
|
|
*/
|
|
|
|
public byte [] getData()
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
if (eof())
|
|
|
|
{
|
|
|
|
throw new IOException("Cannot return empty data");
|
|
|
|
}
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ********** END implementation of ListManagedBlock ********** */
|
|
|
|
} // end public class RawDataBlock
|
|
|
|
|