com.lowagie.text.pdf
public class: PdfStream [javadoc |
source]
java.lang.Object
com.lowagie.text.pdf.PdfObject
com.lowagie.text.pdf.PdfDictionary
com.lowagie.text.pdf.PdfStream
Direct Known Subclasses:
PdfImage, PdfContents, StreamFont, PdfFormXObject, PdfEFStream, PdfPattern, PdfICCBased, PRStream
PdfStream is the Pdf stream object.
A stream, like a string, is a sequence of characters. However, an application can
read a small portion of a stream at a time, while a string must be read in its entirety.
For this reason, objects with potentially large amounts of data, such as images and
page descriptions, are represented as streams.
A stream consists of a dictionary that describes a sequence of characters, followed by
the keyword stream, followed by zero or more lines of characters, followed by
the keyword endstream.
All streams must be PdfIndirectObjects. The stream dictionary must be a direct
object. The keyword stream that follows the stream dictionary should be followed by
a carriage return and linefeed or just a linefeed.
Remark: In this version only the FLATEDECODE-filter is supported.
This object is described in the 'Portable Document Format Reference Manual version 1.7'
section 3.2.7 (page 60-63).
| Field Summary |
|---|
| public static final int | DEFAULT_COMPRESSION | A possible compression level. |
| public static final int | NO_COMPRESSION | A possible compression level. |
| public static final int | BEST_SPEED | A possible compression level. |
| public static final int | BEST_COMPRESSION | A possible compression level. |
| protected boolean | compressed | is the stream compressed? |
| protected int | compressionLevel | The level of compression. |
| protected ByteArrayOutputStream | streamBytes | |
| protected InputStream | inputStream | |
| protected PdfIndirectReference | ref | |
| protected int | inputStreamLength | |
| protected PdfWriter | writer | |
| protected int | rawLength | |
| static final byte[] | STARTSTREAM | |
| static final byte[] | ENDSTREAM | |
| static final int | SIZESTREAM | |
| Fields inherited from com.lowagie.text.pdf.PdfObject: |
|---|
| BOOLEAN, NUMBER, STRING, NAME, ARRAY, DICTIONARY, STREAM, NULL, INDIRECT, NOTHING, TEXT_PDFDOCENCODING, TEXT_UNICODE, bytes, type, indRef |
| Methods from com.lowagie.text.pdf.PdfDictionary: |
|---|
|
contains, get, getAsArray, getAsBoolean, getAsDict, getAsIndirectObject, getAsName, getAsNumber, getAsStream, getAsString, getDirectObject, getKeys, isCatalog, isFont, isOutlineTree, isPage, isPages, merge, mergeDifferent, put, putAll, putEx, remove, size, toPdf, toString |
| Methods from com.lowagie.text.pdf.PdfObject: |
|---|
|
canBeInObjStm, getBytes, getIndRef, isArray, isBoolean, isDictionary, isIndirect, isName, isNull, isNumber, isStream, isString, length, setContent, setIndRef, toPdf, toString, type |
| Method from com.lowagie.text.pdf.PdfStream Detail: |
public void flateCompress() {
flateCompress(DEFAULT_COMPRESSION);
}
|
public void flateCompress(int compressionLevel) {
if (!Document.compress)
return;
// check if the flateCompress-method has already been
if (compressed) {
return;
}
this.compressionLevel = compressionLevel;
if (inputStream != null) {
compressed = true;
return;
}
// check if a filter already exists
PdfObject filter = PdfReader.getPdfObject(get(PdfName.FILTER));
if (filter != null) {
if (filter.isName()) {
if (PdfName.FLATEDECODE.equals(filter))
return;
}
else if (filter.isArray()) {
if (((PdfArray) filter).contains(PdfName.FLATEDECODE))
return;
}
else {
throw new RuntimeException("Stream could not be compressed: filter is not a name or array.");
}
}
try {
// compress
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DeflaterOutputStream zip = new DeflaterOutputStream(stream, new Deflater(compressionLevel));
if (streamBytes != null)
streamBytes.writeTo(zip);
else
zip.write(bytes);
zip.close();
// update the object
streamBytes = stream;
bytes = null;
put(PdfName.LENGTH, new PdfNumber(streamBytes.size()));
if (filter == null) {
put(PdfName.FILTER, PdfName.FLATEDECODE);
}
else {
PdfArray filters = new PdfArray(filter);
filters.add(PdfName.FLATEDECODE);
put(PdfName.FILTER, filters);
}
compressed = true;
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
|
public int getRawLength() {
return rawLength;
}
Gets the raw length of the stream. |
protected void superToPdf(PdfWriter writer,
OutputStream os) throws IOException {
super.toPdf(writer, os);
}
|
public void toPdf(PdfWriter writer,
OutputStream os) throws IOException {
if (inputStream != null && compressed)
put(PdfName.FILTER, PdfName.FLATEDECODE);
PdfEncryption crypto = null;
if (writer != null)
crypto = writer.getEncryption();
if (crypto != null) {
PdfObject filter = get(PdfName.FILTER);
if (filter != null) {
if (PdfName.CRYPT.equals(filter))
crypto = null;
else if (filter.isArray()) {
ArrayList af = ((PdfArray)filter).getArrayList();
if (!af.isEmpty() && PdfName.CRYPT.equals(af.get(0)))
crypto = null;
}
}
}
PdfObject nn = get(PdfName.LENGTH);
if (crypto != null && nn != null && nn.isNumber()) {
int sz = ((PdfNumber)nn).intValue();
put(PdfName.LENGTH, new PdfNumber(crypto.calculateStreamSize(sz)));
superToPdf(writer, os);
put(PdfName.LENGTH, nn);
}
else
superToPdf(writer, os);
os.write(STARTSTREAM);
if (inputStream != null) {
rawLength = 0;
DeflaterOutputStream def = null;
OutputStreamCounter osc = new OutputStreamCounter(os);
OutputStreamEncryption ose = null;
OutputStream fout = osc;
if (crypto != null && !crypto.isEmbeddedFilesOnly())
fout = ose = crypto.getEncryptionStream(fout);
if (compressed)
fout = def = new DeflaterOutputStream(fout, new Deflater(compressionLevel), 0x8000);
byte buf[] = new byte[4192];
while (true) {
int n = inputStream.read(buf);
if (n < = 0)
break;
fout.write(buf, 0, n);
rawLength += n;
}
if (def != null)
def.finish();
if (ose != null)
ose.finish();
inputStreamLength = osc.getCounter();
}
else {
if (crypto != null && !crypto.isEmbeddedFilesOnly()) {
byte b[];
if (streamBytes != null) {
b = crypto.encryptByteArray(streamBytes.toByteArray());
}
else {
b = crypto.encryptByteArray(bytes);
}
os.write(b);
}
else {
if (streamBytes != null)
streamBytes.writeTo(os);
else
os.write(bytes);
}
}
os.write(ENDSTREAM);
}
|
public String toString() {
if (get(PdfName.TYPE) == null) return "Stream";
return "Stream of type: " + get(PdfName.TYPE);
}
|
public void writeContent(OutputStream os) throws IOException {
if (streamBytes != null)
streamBytes.writeTo(os);
else if (bytes != null)
os.write(bytes);
}
Writes the data content to an OutputStream. |
public void writeLength() throws IOException {
if (inputStream == null)
throw new UnsupportedOperationException("writeLength() can only be called in a contructed PdfStream(InputStream,PdfWriter).");
if (inputStreamLength == -1)
throw new IOException("writeLength() can only be called after output of the stream body.");
writer.addToBody(new PdfNumber(inputStreamLength), ref, false);
}
Writes the stream length to the PdfWriter.
This method must be called and can only be called if the constructor #PdfStream(InputStream,PdfWriter)
is used to create the stream. |