PDFDocument.java 35.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.agl.util.ULocale
 *  com.adobe.internal.io.ByteReader
 *  com.adobe.internal.io.ByteWriter
 *  com.adobe.internal.io.stream.StreamManager
 *  com.adobe.internal.pdftoolkit.core.cos.CosArray
 *  com.adobe.internal.pdftoolkit.core.cos.CosDictionary
 *  com.adobe.internal.pdftoolkit.core.cos.CosDocument
 *  com.adobe.internal.pdftoolkit.core.cos.CosEncryption
 *  com.adobe.internal.pdftoolkit.core.cos.CosObject
 *  com.adobe.internal.pdftoolkit.core.cos.CosObjectRefAdapter
 *  com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions
 *  com.adobe.internal.pdftoolkit.core.cos.CosSaveParams
 *  com.adobe.internal.pdftoolkit.core.cos.CosString
 *  com.adobe.internal.pdftoolkit.core.cos.PDFCore
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidXMLException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFUnableToCompleteOperationException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFUnsupportedFeatureException
 *  com.adobe.internal.pdftoolkit.core.securityframework.DecryptedState
 *  com.adobe.internal.pdftoolkit.core.securityframework.SecurityHandler
 *  com.adobe.internal.pdftoolkit.core.securityframework.SecurityKey
 *  com.adobe.internal.pdftoolkit.core.securityframework.SecurityLock
 *  com.adobe.internal.pdftoolkit.core.securityframework.SecurityManager
 *  com.adobe.internal.pdftoolkit.core.securityframework.impl.EnableEncryption
 *  com.adobe.internal.pdftoolkit.core.types.ASHexString
 *  com.adobe.internal.pdftoolkit.core.types.ASName
 *  com.adobe.internal.pdftoolkit.core.types.ASRectangle
 */
package com.adobe.internal.pdftoolkit.pdf.document;

import com.adobe.agl.util.ULocale;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.stream.StreamManager;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosEncryption;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectRefAdapter;
import com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions;
import com.adobe.internal.pdftoolkit.core.cos.CosSaveParams;
import com.adobe.internal.pdftoolkit.core.cos.CosString;
import com.adobe.internal.pdftoolkit.core.cos.PDFCore;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidXMLException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFUnableToCompleteOperationException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFUnsupportedFeatureException;
import com.adobe.internal.pdftoolkit.core.securityframework.DecryptedState;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityHandler;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityKey;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityLock;
import com.adobe.internal.pdftoolkit.core.securityframework.SecurityManager;
import com.adobe.internal.pdftoolkit.core.securityframework.impl.EnableEncryption;
import com.adobe.internal.pdftoolkit.core.types.ASHexString;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASRectangle;
import com.adobe.internal.pdftoolkit.pdf.document.DocumentListenerRegistryImpl;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocumentInfo;
import com.adobe.internal.pdftoolkit.pdf.document.PDFEmbeddedFile;
import com.adobe.internal.pdftoolkit.pdf.document.PDFEncryption;
import com.adobe.internal.pdftoolkit.pdf.document.PDFEncryptionType;
import com.adobe.internal.pdftoolkit.pdf.document.PDFExtensions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFFileSpecification;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNameDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFNamedEmbeddedFiles;
import com.adobe.internal.pdftoolkit.pdf.document.PDFOpenOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFPermissions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveFullOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveIncrementalOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveLinearOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFSaveOptions;
import com.adobe.internal.pdftoolkit.pdf.document.PDFTree;
import com.adobe.internal.pdftoolkit.pdf.document.PDFVersion;
import com.adobe.internal.pdftoolkit.pdf.document.listener.DocumentListenerRegistry;
import com.adobe.internal.pdftoolkit.pdf.document.listener.DocumentMessage;
import com.adobe.internal.pdftoolkit.pdf.filters.PDFFilter;
import com.adobe.internal.pdftoolkit.pdf.filters.PDFFilterCrypt;
import com.adobe.internal.pdftoolkit.pdf.filters.PDFFilterList;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFRectangle;
import com.adobe.internal.pdftoolkit.pdf.interactive.PDFViewerPreferences;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotation;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationFileAttachment;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationIterator;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFField;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFFieldSignature;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import com.adobe.internal.pdftoolkit.pdf.interchange.metadata.PDFMetadata;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageTree;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.WeakHashMap;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class PDFDocument {
    protected CosDocument mCosDocument;
    protected DocumentListenerRegistryImpl listenerRegistry;
    protected PDFEncryption mEncryption;
    protected boolean newDocument;
    private static final int INIT_PDFOBJECT_CACHE_SIZE = 1024;
    public final Map<CosObjectRefAdapter, Object> pdfObjectCache;
    public static final Locale ROOT_LOCALE = ULocale.ROOT.toLocale();
    private PDFDocumentType pdfDocType;

    public static PDFDocument newInstance(PDFOpenOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return new PDFDocument(options);
    }

    public static PDFDocument newInstance(ASRectangle mediaBox, PDFOpenOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        if (mediaBox == null) {
            throw new PDFInvalidParameterException("A valid media box must be provided");
        }
        PDFDocument doc = new PDFDocument(options);
        PDFRectangle mediaBoxRect = PDFRectangle.newInstance(doc, mediaBox);
        PDFPage firstPage = PDFPage.newInstance(doc, mediaBoxRect);
        PDFPageTree.newInstance(doc, firstPage);
        return doc;
    }

    public static PDFDocument newInstance(ByteReader src, PDFOpenOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return new PDFDocument(src, options);
    }

    public static PDFDocument newInstance(PDFCore core) throws PDFCosParseException, PDFIOException, PDFSecurityException, PDFUnsupportedFeatureException {
        return new PDFDocument(core);
    }

    protected PDFDocument(ByteReader src, PDFOpenOptions options) throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
        this.listenerRegistry = new DocumentListenerRegistryImpl(this);
        this.mEncryption = null;
        this.pdfObjectCache = new WeakHashMap<CosObjectRefAdapter, Object>(1024);
        PDFOpenOptions ourOptions = new PDFOpenOptions(options);
        this.mCosDocument = CosDocument.newDocument((ByteReader)src, (CosOpenOptions)ourOptions);
        this.mCosDocument.setPDFDocument((Object)this);
        this.newDocument = false;
    }

    protected PDFDocument(PDFOpenOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.listenerRegistry = new DocumentListenerRegistryImpl(this);
        this.mEncryption = null;
        this.pdfObjectCache = new WeakHashMap<CosObjectRefAdapter, Object>(1024);
        PDFOpenOptions ourOptions = new PDFOpenOptions(options);
        this.mCosDocument = CosDocument.newDocument((CosOpenOptions)ourOptions);
        this.mCosDocument.setPDFDocument((Object)this);
        this.newDocument = true;
    }

    protected PDFDocument(PDFCore core) throws PDFCosParseException, PDFIOException, PDFSecurityException, PDFUnsupportedFeatureException {
        this.listenerRegistry = new DocumentListenerRegistryImpl(this);
        this.mEncryption = null;
        this.pdfObjectCache = new WeakHashMap<CosObjectRefAdapter, Object>(1024);
        this.mCosDocument = CosDocument.newDocument((PDFCore)core);
        if (this.mCosDocument.getPdfDocument() != null) {
            throw new PDFUnsupportedFeatureException("A PDFDocument has been instantiated from this PDFCore object. Cannot instantiate another one.");
        }
        this.mCosDocument.setPDFDocument((Object)this);
        this.newDocument = false;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public PDFCore finish() throws PDFUnableToCompleteOperationException {
        PDFUnableToCompleteOperationException[] exceptions = null;
        try {
            exceptions = this.listenerRegistry.sendMessage(new DocumentMessage(DocumentMessage.FINISH, this), true);
        }
        catch (PDFException e) {
            // empty catch block
        }
        try {
            this.mCosDocument.setPDFDocument((Object)null);
            PDFCore e = this.mCosDocument.finish();
            return e;
        }
        finally {
            if (exceptions != null) {
                try {
                    this.mCosDocument.close();
                }
                catch (PDFException e) {
                    throw new PDFUnableToCompleteOperationException("Error within an error during the closing of document listeners.", (Throwable)e);
                }
                this.internalClose();
                throw new PDFUnableToCompleteOperationException("Error during the closing of document listeners.", (Throwable)exceptions[0]);
            }
            this.internalClose();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void close() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFUnableToCompleteOperationException {
        PDFUnableToCompleteOperationException[] exceptions = null;
        try {
            exceptions = this.listenerRegistry.sendMessage(new DocumentMessage(DocumentMessage.CLOSE, this), true);
        }
        catch (PDFException e) {
            // empty catch block
        }
        try {
            if (this.mCosDocument != null) {
                this.mCosDocument.close();
            }
        }
        finally {
            this.internalClose();
            if (exceptions != null) {
                throw new PDFUnableToCompleteOperationException("Error during the closing of document listeners.", (Throwable)exceptions[0]);
            }
        }
    }

    private void internalClose() {
        this.mCosDocument = null;
    }

    public DocumentListenerRegistry getListenerRegistry() {
        return this.listenerRegistry;
    }

    public int getNumRevisions() {
        return this.mCosDocument.getNumRevisions();
    }

    public PDFCatalog requireCatalog() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        try {
            return PDFCatalog.requireInstance((CosObject)this.mCosDocument.getRoot());
        }
        catch (PDFInvalidParameterException e) {
            throw new PDFInvalidDocumentException("Could not get document catalog", (Throwable)e);
        }
        catch (PDFCosParseException e) {
            throw new PDFInvalidDocumentException((Throwable)e);
        }
    }

    public PDFDocumentInfo getDocumentInfo() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return PDFDocumentInfo.getInstance((CosObject)this.mCosDocument.getInfo());
    }

    public byte[] getDocCreationID() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        return this.getID(0);
    }

    public byte[] getDocModificationID() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        return this.getID(1);
    }

    private byte[] getID(int which) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        CosArray array = this.getCosDocument().getTrailer().getCosArray(ASName.k_ID);
        if (array == null) {
            return null;
        }
        CosString creationID = (CosString)array.get(which);
        return creationID.byteArrayValue();
    }

    public PDFPageTree requirePages() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.requireCatalog().requirePages();
    }

    public PDFInteractiveForm getInteractiveForm() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.requireCatalog().getInteractiveForm();
    }

    public void setInteractiveForm(PDFInteractiveForm pdfInteractiveForm) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.requireCatalog().setInteractiveForm(pdfInteractiveForm);
    }

    public void setDocumentInfo(PDFDocumentInfo info) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        if (info == null) {
            this.mCosDocument.getTrailer().remove(ASName.k_Info);
        } else {
            this.mCosDocument.getTrailer().put(ASName.k_Info, info.getCosObject());
        }
    }

    public boolean exportXMP(OutputStream metadata) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFMetadata pdfMetadata;
        if (metadata != null && (pdfMetadata = this.requireCatalog().getMetadata()) != null) {
            pdfMetadata.getXMLData(metadata);
            return true;
        }
        return false;
    }

    public void importXMP(InputStream metadata) throws PDFInvalidXMLException, PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
        PDFMetadata pdfMetadata = this.requireCatalog().getMetadata();
        if (pdfMetadata == null) {
            pdfMetadata = PDFMetadata.newInstance(this, metadata, 2048);
            this.requireCatalog().setMetadata(pdfMetadata);
        } else {
            pdfMetadata.setData(metadata, 2048);
        }
    }

    public boolean hasXMP() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.requireCatalog().dictionaryContains(ASName.k_Metadata);
    }

    public boolean hasInfo() {
        boolean result = this.getCosDocument().getTrailer().containsKey((Object)ASName.k_Info);
        return result;
    }

    public PDFViewerPreferences getViewerPreferences() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.requireCatalog().getViewerPreferences();
    }

    public void setViewerPreferences(PDFViewerPreferences pdfViewerPreferences) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.requireCatalog().setViewerPreferences(pdfViewerPreferences);
    }

    public PDFViewerPreferences procureViewerPreferences() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.requireCatalog().procureViewerPreferences();
    }

    public void freeDuplicateResources() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        this.getCosDocument().freeDuplicateResources();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void saveAndClose(ByteWriter byteWriter, PDFSaveOptions options) throws PDFInvalidParameterException, PDFIOException, PDFInvalidDocumentException, PDFSecurityException, PDFUnableToCompleteOperationException {
        boolean oldCloseAfterSave = options.getCloseAfterSave();
        try {
            try {
                options.setCloseAfterSave(true);
                this.save(byteWriter, options);
            }
            finally {
                this.close();
            }
        }
        finally {
            options.setCloseAfterSave(oldCloseAfterSave);
        }
    }

    public void save(ByteWriter byteWriter, PDFSaveOptions options) throws PDFInvalidParameterException, PDFIOException, PDFInvalidDocumentException, PDFSecurityException, PDFUnableToCompleteOperationException {
        this.preSave(options);
        this.doSave(byteWriter, options);
        this.postSave(options);
    }

    protected void preSave(PDFSaveOptions options) throws PDFUnableToCompleteOperationException {
        this.listenerRegistry.sendMessage(new DocumentMessage(DocumentMessage.SAVE, this), false);
        if (options != null && options.getCloseAfterSave()) {
            this.listenerRegistry.sendMessage(new DocumentMessage(DocumentMessage.CLOSE, this), false);
        }
    }

    protected void postSave(PDFSaveOptions options) {
        if (options != null && options.getCloseAfterSave()) {
            this.internalClose();
        }
    }

    protected void doSave(ByteWriter byteWriter, PDFSaveOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
        this.setExtensions(options);
        if (options instanceof PDFSaveFullOptions) {
            this.saveFull(byteWriter, (PDFSaveFullOptions)options);
        } else if (options instanceof PDFSaveIncrementalOptions) {
            this.saveIncremental(byteWriter, (PDFSaveIncrementalOptions)options);
        } else if (options instanceof PDFSaveLinearOptions) {
            this.saveLinear(byteWriter, (PDFSaveLinearOptions)options);
        } else {
            throw new PDFInvalidParameterException("Unrecognized save options.");
        }
    }

    private void saveFull(ByteWriter byteWriter, PDFSaveFullOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
        this.requirePages();
        CosSaveParams cosParams = null;
        if (options == null) {
            cosParams = new CosSaveParams(2);
        } else {
            cosParams = options.getCosSaveParams();
            this.handleEncryption(options);
        }
        this.handleDigSigEncryption(options);
        this.getCosDocument().save(byteWriter, cosParams);
    }

    public boolean canIncrementallySaveInPlace() {
        return this.mCosDocument.getInPlaceByteWriter() != null;
    }

    private void saveIncremental(ByteWriter byteWriter, PDFSaveIncrementalOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
        if (this.newDocument) {
            throw new UnsupportedOperationException("Cannot incrementally save new document");
        }
        this.requirePages();
        CosSaveParams cosParams = null;
        cosParams = options == null ? new CosSaveParams(1) : options.getCosSaveParams();
        if (this.isEncrypted()) {
            Map securityParams = this.getEncryption().getEncryptParameters();
            if (securityParams != null) {
                this.handleEFF(securityParams);
            }
            if (!this.isUnlocked()) {
                throw new UnsupportedOperationException("Cannot incrementally save non-unlocked, encrypted document");
            }
        }
        try {
            this.getCosDocument().save(byteWriter, cosParams);
        }
        catch (PDFInvalidParameterException e) {
            if (options == null) {
                throw new RuntimeException("Invalid internal default save options.", (Throwable)e);
            }
            throw e;
        }
    }

    private void saveLinear(ByteWriter byteWriter, PDFSaveLinearOptions options) throws PDFInvalidDocumentException, PDFInvalidParameterException, PDFIOException, PDFSecurityException {
        this.requirePages();
        ByteWriter tempWriter = null;
        CosSaveParams cosParams = null;
        if (options == null) {
            cosParams = new CosSaveParams(3);
        } else {
            cosParams = options.getCosSaveParams();
            tempWriter = options.getTempByteWriter();
            this.handleEncryption(options);
        }
        this.handleDigSigEncryption(options);
        cosParams.setTempByteWriter(tempWriter);
        this.getCosDocument().save(byteWriter, cosParams);
    }

    public CosDocument getCosDocument() {
        return this.mCosDocument;
    }

    public StreamManager getStreamManager() {
        return this.getCosDocument().getStreamManager();
    }

    public boolean unlock(SecurityKey key) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.isEncrypted()) {
            if (this.isUnlocked()) {
                return false;
            }
            this.getCosDocument().getEncryption().setDecryptionSecurityManager(key.getSecurityManager());
            this.getCosDocument().getEncryption().authenticateDecryption(null);
        }
        return true;
    }

    public boolean unlock(DecryptedState decryptedState) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.isEncrypted()) {
            if (this.isUnlocked()) {
                return false;
            }
            this.getCosDocument().getEncryption().setDecryptionSecurityManager(decryptedState.getSecurityManager(this.getCosDocument()));
            this.getCosDocument().getEncryption().authenticateDecryption(decryptedState);
        }
        return true;
    }

    public void resetUnlock() {
        this.getCosDocument().getEncryption().resetDecryptionSecurityManager();
    }

    public boolean isUnlocked() {
        boolean bRet = false;
        if (this.isEncrypted()) {
            SecurityManager mgr = this.getCosDocument().getEncryption().getDecryptionSecurityManager();
            if (mgr != null) {
                bRet = true;
            }
        } else {
            bRet = true;
        }
        return bRet;
    }

    public boolean isEncrypted() {
        return this.getCosDocument().isEncrypted();
    }

    public PDFEncryptionType getEncryptionType() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFEncryption encryption = this.getEncryption();
        if (encryption == null) {
            return PDFEncryptionType.None;
        }
        return encryption.getEncryptionType();
    }

    public PDFEncryption getEncryption() throws PDFInvalidDocumentException {
        return this.isEncrypted() ? PDFEncryption.getInstance((CosObject)this.getCosDocument().getEncryption().getEncryption()) : null;
    }

    private void handleDigSigEncryption(PDFSaveOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        int sigFlags;
        SecurityLock securityLock;
        PDFPermissions perms;
        SecurityLock securityLock2 = securityLock = options != null ? options.getSecurityLock() : null;
        if (!(this.isEncrypted() || securityLock != null && securityLock.shouldEncrypt())) {
            return;
        }
        PDFInteractiveForm acroForm = this.getInteractiveForm();
        if (acroForm != null && ((sigFlags = acroForm.getSigFlags()) & 2) != 2) {
            return;
        }
        if (acroForm != null) {
            Iterator<PDFField> fieldIter = acroForm.iterator();
            while (fieldIter.hasNext()) {
                PDFField curField = fieldIter.next();
                if (!(curField instanceof PDFFieldSignature)) continue;
                this.setDigSigEncryption(curField.getDictionaryCosObjectValue(ASName.k_V));
            }
        }
        if ((perms = this.requireCatalog().getPermissions()) != null) {
            this.setDigSigEncryption(perms.getDictionaryCosObjectValue(ASName.k_DocMDP));
            this.setDigSigEncryption(perms.getDictionaryCosObjectValue(ASName.k_UR3));
        }
    }

    private void setDigSigEncryption(CosObject sig) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (sig == null) {
            return;
        }
        if (sig.getType() == 6) {
            CosObject contents = null;
            try {
                contents = ((CosDictionary)sig).get(ASName.k_Contents);
                this.protectDigSigKeysFromEncryption(contents);
            }
            catch (PDFCosParseException e) {
                throw new PDFInvalidDocumentException((Throwable)e);
            }
            CosArray referenceCosArray = ((CosDictionary)sig).getCosArray(ASName.k_Reference);
            if (referenceCosArray != null) {
                Iterator iter = referenceCosArray.iterator();
                while (iter.hasNext()) {
                    CosObject digestValue = null;
                    try {
                        CosDictionary referenceDict = (CosDictionary)iter.next();
                        digestValue = referenceDict.get(ASName.k_DigestValue);
                        this.protectDigSigKeysFromEncryption(digestValue);
                        continue;
                    }
                    catch (PDFCosParseException e) {
                        throw new PDFInvalidDocumentException((Throwable)e);
                    }
                }
            }
        }
    }

    private void protectDigSigKeysFromEncryption(CosObject cosObject) {
        if (cosObject != null && cosObject.getType() == 4) {
            ((CosString)cosObject).setIsEncrypted(false);
            ((CosString)cosObject).setToEncrypt(false);
            ((CosString)cosObject).setWriteHex(true);
        }
    }

    private void handleEncryption(PDFSaveOptions options) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        SecurityLock securityLock = options.getSecurityLock();
        Map securityParams = null;
        if (securityLock != null) {
            securityParams = securityLock.getEncryptParameters();
        }
        if (securityParams != null) {
            this.handleEFF(securityParams);
        }
        if (securityLock != null) {
            if (!securityLock.shouldEncrypt()) {
                this.getCosDocument().getEncryption().resetEncryptionSecurityManager();
                return;
            }
            this.getCosDocument().getEncryption().setEncryptionSecurityManager(securityLock.getSecurityManager(), securityLock.getEncryptParameters());
        }
    }

    private void handleEFF(Map securityParams) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFFileSpecification fileSpec;
        PDFEmbeddedFile file;
        String effKey = "EFF";
        if (!securityParams.containsKey(effKey)) {
            return;
        }
        String filterVal = (String)securityParams.get(effKey);
        if (filterVal == null || filterVal.length() == 0) {
            return;
        }
        PDFFilterCrypt effFilter = PDFFilterCrypt.newInstance(this, filterVal);
        PDFNameDictionary namedDict = this.requireCatalog().getNameDictionary();
        if (namedDict == null) {
            return;
        }
        PDFNamedEmbeddedFiles efTree = namedDict.getNamedEmbeddedFiles();
        if (efTree != null) {
            Iterator efTreeIter = efTree.iterator();
            while (efTreeIter.hasNext()) {
                PDFTree.Entry entry = (PDFTree.Entry)((Object)efTreeIter.next());
                fileSpec = (PDFFileSpecification)entry.getValue();
                if (fileSpec == null || (file = fileSpec.getEmbeddedFile()) == null) continue;
                this.createEFFOutputFilterList(file, effFilter);
            }
        }
        PDFAnnotationIterator annotIter = this.requirePages().getAnnotationsIterator();
        while (annotIter.hasNext()) {
            PDFAnnotationFileAttachment fileAnnot;
            PDFAnnotation annot = annotIter.next();
            if (!(annot instanceof PDFAnnotationFileAttachment) || (fileSpec = (fileAnnot = (PDFAnnotationFileAttachment)annot).getFileSpecification()) == null || (file = fileSpec.getEmbeddedFile()) == null) continue;
            this.createEFFOutputFilterList(file, effFilter);
        }
    }

    private void createEFFOutputFilterList(PDFEmbeddedFile file, PDFFilterCrypt effFilter) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (file == null) {
            return;
        }
        PDFFilterList outputFilters = file.procureOutputFilters();
        if (outputFilters.hasNamedCryptFilter(effFilter)) {
            return;
        }
        outputFilters.remove("Crypt");
        outputFilters.add(0, effFilter);
        file.setOutputFilters(outputFilters);
    }

    public PDFVersion getToSaveVersion() {
        String version = this.getCosDocument().getToSaveVersion();
        if (version == null) {
            return null;
        }
        return PDFVersion.getInstance(version, this.getCosDocument().getToSaveExtensions());
    }

    public PDFVersion procureToSaveVersion() throws PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
        String version = this.getCosDocument().procureToSaveVersion();
        PDFVersion cosVersion = PDFVersion.getInstance(version);
        Map extensions = this.getCosDocument().isToSaveExtensionsInitialized() ? this.getCosDocument().getToSaveExtensions() : this.extensionsMap();
        return PDFVersion.newInstance(cosVersion.getMajorversion(), cosVersion.getMinorversion(), extensions);
    }

    public void setToSaveVersion(PDFVersion version) {
        if (version == null) {
            return;
        }
        this.getCosDocument().setToSaveVersion(version.getVersionValue());
        this.getCosDocument().setToSaveExtensions(version.getExtensions());
    }

    private Map extensionsMap() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.requireCatalog().hasExtensions() ? this.requireCatalog().getExtensions().asMap() : null;
    }

    private void setExtensions(PDFSaveOptions options) throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
        Map toSaveExtensions = this.getCosDocument().getToSaveExtensions();
        PDFVersion toSaveVersion = options.getVersion();
        if (toSaveExtensions == null && toSaveVersion != null) {
            toSaveExtensions = toSaveVersion.getExtensions();
        }
        if (toSaveExtensions != null) {
            this.requireCatalog().setExtensions(PDFExtensions.newInstance(this, toSaveExtensions));
        } else if (toSaveVersion != null) {
            this.requireCatalog().setExtensions(null);
        }
    }

    public PDFVersion getOriginalVersion() throws PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
        String version = this.getCosDocument().getOriginalVersion();
        if (version == null) {
            return null;
        }
        return PDFVersion.getInstance(version, this.extensionsMap());
    }

    Map<CosObjectRefAdapter, Object> getPDFObjectCache() {
        return this.pdfObjectCache;
    }

    public SecurityHandler getEncryptionSecurityHandler() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        CosEncryption cosEncryption = this.getCosDocument().getEncryption();
        if (cosEncryption == null) {
            return null;
        }
        SecurityManager secMgr = cosEncryption.getEncryptionSecurityManager();
        if (secMgr == null) {
            return null;
        }
        return secMgr.getSecurityHandler(null, cosEncryption.getEncryptionMap());
    }

    public long getFileSize() throws PDFIOException {
        return this.getCosDocument().getFileSize();
    }

    public String[] getDocID() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        String[] arr = new String[2];
        CosArray docIdArr = this.getCosDocument().getTrailer().getCosArray(ASName.k_ID);
        if (docIdArr != null && docIdArr.size() > 1) {
            arr[0] = docIdArr.getHexString(0).asString();
            arr[1] = docIdArr.getHexString(1).asString();
            return arr;
        }
        return null;
    }

    public boolean isDirty() {
        return this.isInternalStateDirty() || this.isExternalStateDirty();
    }

    public boolean wasRepaired() {
        return this.getCosDocument().wasRepaired();
    }

    public boolean isInternalStateDirty() {
        CosDocument cosDocument = this.getCosDocument();
        if (cosDocument == null) {
            return false;
        }
        return cosDocument.isDirty();
    }

    public boolean isExternalStateDirty() {
        return this.getListenerRegistry().isAnyListenerDirty();
    }

    public void markDirty(boolean dirty) {
        CosDocument cosDocument = this.getCosDocument();
        if (dirty) {
            cosDocument.markDirty();
        } else {
            cosDocument.markNotDirty();
        }
    }

    public boolean requiresFullSave() {
        return !this.canIncrementallySaveInPlace();
    }

    public Locale getDocumentLocale() {
        Locale docLocale = this.getCosDocument().getOptions().getDocLocale();
        if (docLocale == null) {
            return this.getSystemLocale();
        }
        return docLocale;
    }

    private Locale getSystemLocale() {
        return new Locale(Locale.getDefault().toString());
    }

    public PDFDocumentType getPDFDocumentType() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return this.pdfDocType;
    }

    public void setPDFDocumentType(PDFDocumentType pdfDocType) {
        this.pdfDocType = pdfDocType;
    }

    public DecryptedState getDecryptedState() {
        SecurityHandler handler;
        if (this.isEncrypted() && this.isUnlocked() && (handler = this.getCosDocument().getEncryption().getEncryptionImpl().getSecurityHandler()) != null) {
            return handler.getDecryptedState();
        }
        return null;
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    public static enum PDFDocumentType {
        Flat(false, false, false),
        Acroform(false, false, false),
        StaticShellXFA(false, true, true),
        StaticNonShellXFA(false, true, false),
        DynamicShellXFA(true, false, true),
        DynamicNonShellXFA(true, false, false);
        
        private final boolean isDynamic;
        private final boolean isStatic;
        private final boolean isShell;

        private PDFDocumentType(boolean isDynamic, boolean isStatic, boolean isShell) {
            this.isDynamic = isDynamic;
            this.isStatic = isStatic;
            this.isShell = isShell;
        }

        public boolean isXFA() {
            return this.isDynamic || this.isStatic;
        }

        public boolean isDynamic() {
            return this.isDynamic;
        }

        public boolean isStatic() {
            return this.isStatic;
        }

        public boolean isShell() {
            return this.isShell;
        }
    }

}