Skip to content

Commit

Permalink
ref #217: stop loading stuff into byte arrays and be happy
Browse files Browse the repository at this point in the history
  • Loading branch information
torakiki committed Feb 4, 2016
1 parent 9f834b4 commit 7f6bb05
Showing 1 changed file with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import org.sejda.impl.sambox.component.ReadOnlyFilteredCOSStream;
Expand All @@ -32,12 +33,12 @@
import org.sejda.sambox.cos.COSBase;
import org.sejda.sambox.cos.COSDictionary;
import org.sejda.sambox.cos.COSName;
import org.sejda.sambox.cos.COSStream;
import org.sejda.sambox.pdmodel.MissingResourceException;
import org.sejda.sambox.pdmodel.PDPage;
import org.sejda.sambox.pdmodel.graphics.PDXObject;
import org.sejda.sambox.pdmodel.graphics.form.PDFormXObject;
import org.sejda.sambox.pdmodel.graphics.form.PDTransparencyGroup;
import org.sejda.sambox.pdmodel.graphics.image.PDImageXObject;
import org.sejda.sambox.pdmodel.interactive.annotation.PDAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,32 +68,35 @@ public void process(Operator operator, List<COSBase> operands) throws IOExceptio
if (operand instanceof COSName) {

COSName objectName = (COSName) operand;
COSBase existing = ofNullable(
Optional<COSDictionary> xobjects = ofNullable(
context.getResources().getCOSObject().getDictionaryObject(COSName.XOBJECT))
.filter(d -> d instanceof COSDictionary).map(d -> (COSDictionary) d)
.map(d -> d.getDictionaryObject(objectName)).orElseThrow(
() -> new MissingResourceException("Missing XObject: " + objectName.getName()));
.filter(d -> d instanceof COSDictionary).map(d -> (COSDictionary) d);
COSBase existing = xobjects.map(d -> d.getDictionaryObject(objectName))
.orElseThrow(() -> new MissingResourceException("Missing XObject: " + objectName.getName()));

if (!(existing instanceof ReadOnlyFilteredCOSStream)) {
PDXObject xobject = PDXObject.createXObject(existing.getCOSObject(), context.getResources());
if (xobject instanceof PDImageXObject) {
PDImageXObject image = (PDImageXObject) xobject;
COSStream imageStream = ofNullable(existing).filter(e -> e instanceof COSStream)
.map(e -> (COSStream) e)
.orElseThrow(() -> new IllegalArgumentException("External object unexpected type"));

String subtype = imageStream.getNameAsString(COSName.SUBTYPE);
if (COSName.IMAGE.getName().equals(subtype)) {
LOG.trace("Hit image with name {}", objectName.getName());
// we wrap the existing so we can identify it later as "in use" and already processed
ReadOnlyFilteredCOSStream optimizedImage = ReadOnlyFilteredCOSStream
.readOnly(image.getCOSStream());
ReadOnlyFilteredCOSStream optimizedImage = ReadOnlyFilteredCOSStream.readOnly(imageStream);
COSDictionary resources = context.getResources().getCOSObject();
COSDictionary xobjects = ofNullable(resources.getDictionaryObject(COSName.XOBJECT))
.filter(b -> b instanceof COSDictionary).map(b -> (COSDictionary) b).orElseGet(() -> {
COSDictionary ret = new COSDictionary();
resources.setItem(COSName.XOBJECT, ret);
return ret;
});
xobjects.setItem(objectName, optimizedImage);
} else if (xobject instanceof PDTransparencyGroup) {
context.showTransparencyGroup((PDTransparencyGroup) xobject);
} else if (xobject instanceof PDFormXObject) {
context.showForm((PDFormXObject) xobject);
xobjects.orElseGet(() -> {
COSDictionary ret = new COSDictionary();
resources.setItem(COSName.XOBJECT, ret);
return ret;
}).setItem(objectName, optimizedImage);
} else if (COSName.FORM.getName().equals(subtype)) {
PDXObject xobject = PDXObject.createXObject(imageStream, context.getResources());
if (xobject instanceof PDTransparencyGroup) {
context.showTransparencyGroup((PDTransparencyGroup) xobject);
} else if (xobject instanceof PDFormXObject) {
context.showForm((PDFormXObject) xobject);
}
}
}
}
Expand Down

0 comments on commit 7f6bb05

Please sign in to comment.