/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.pdf;

public class PdfFile extends PdfObject implements AutoCloseable {

    public PdfDictionary rootObject = null;

    public PdfDictionary info = null;

    public PdfDictionary encrypt = null;

    public java.util.ArrayList<PdfDictionary> pages = new java.util.ArrayList<PdfDictionary>();

    public java.util.ArrayList<PdfDictionary> allResources = new java.util.ArrayList<PdfDictionary>();

    public java.util.ArrayList<PdfObject> objs = null;

    public PdfDictionary trailer;

    public void open(String fileName, byte[] fileContent) throws java.io.FileNotFoundException, java.io.IOException, Exception {
        rootObject = null;
        info = null;
        if (m_Stream != null) 
            m_Stream.close();
        m_Stream = new PdfStream(fileName, fileContent);
        m_Head = m_Stream.readHead();
        int hpos = m_Stream.getPosition();
        if (this._readXRefs()) {
        }
        else {
            int i;
            PdfObject rootObj = null;
            PdfObject infoObj = null;
            m_Stream.setPosition(hpos);
            while (true) {
                i = m_Stream.peekSolidByte();
                if (i < 0) 
                    break;
                byte ch = (byte)i;
                int p0 = m_Stream.getPosition();
                int _id = 0;
                short v = (short)0;
                boolean isObj = false;
                String str;
                PdfObject obj;
                if (((char)ch) >= '0' && ((char)ch) <= '9') {
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapid179 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                    com.pullenti.unisharp.Outargwrapper<Short> wrapv180 = new com.pullenti.unisharp.Outargwrapper<Short>();
                    isObj = m_Stream.tryReadIdAndVersion(false, wrapid179, wrapv180);
                    _id = (wrapid179.value != null ? wrapid179.value : 0);
                    v = (wrapv180.value != null ? wrapv180.value : 0);
                }
                if (isObj) {
                    obj = null;
                    try {
                        obj = m_Stream.parseObject(this, false);
                    } catch (Exception ex) {
                    }
                    if (obj == null) {
                        m_Stream.setPosition(m_Stream.getPosition() + 1);
                        continue;
                    }
                    obj.id = _id;
                    obj.version = v;
                    obj.sourceFile = this;
                    this._addObj(_id, obj);
                    str = m_Stream.readWord(false);
                    continue;
                }
                str = m_Stream.readWord(false);
                if (com.pullenti.unisharp.Utils.stringsEq(str, "xref")) {
                    int i1 = 0;
                    str = m_Stream.readWord(false);
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapi1183 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                    boolean inoutres184 = com.pullenti.unisharp.Utils.parseInteger(str, 0, null, wrapi1183);
                    i1 = (wrapi1183.value != null ? wrapi1183.value : 0);
                    if (inoutres184) {
                        int i2 = 0;
                        str = m_Stream.readWord(false);
                        com.pullenti.unisharp.Outargwrapper<Integer> wrapi2181 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                        boolean inoutres182 = com.pullenti.unisharp.Utils.parseInteger(str, 0, null, wrapi2181);
                        i2 = (wrapi2181.value != null ? wrapi2181.value : 0);
                        if (inoutres182) {
                            if (i2 > i1) 
                                m_Stream.setPosition(m_Stream.getPosition() + (((i2 - i1)) * 20));
                        }
                    }
                    while (true) {
                        str = m_Stream.readWord(false);
                        if (com.pullenti.unisharp.Utils.isNullOrEmpty(str) || com.pullenti.unisharp.Utils.stringsEq(str, "trailer")) 
                            break;
                    }
                }
                if (com.pullenti.unisharp.Utils.stringsEq(str, "trailer")) {
                    if ((((i = m_Stream.peekSolidByte()))) < 0) 
                        break;
                    ch = (byte)i;
                    if (((char)ch) != '<') 
                        continue;
                    m_Stream.setPosition(m_Stream.getPosition() + 1);
                    if ((((i = m_Stream.readByte()))) < 0) 
                        break;
                    ch = (byte)i;
                    if (((char)ch) != '<') 
                        break;
                    trailer = new PdfDictionary();
                    trailer.sourceFile = this;
                    trailer.postParse(m_Stream);
                    if (rootObj == null) 
                        rootObj = trailer.getObject("Root", true);
                    if (infoObj == null) 
                        infoObj = trailer.getObject("Info", true);
                    if (trailer.getObject("Encrypt", true) != null) 
                        encrypt = trailer.getDictionary("Encrypt", null);
                    continue;
                }
                int pos = m_Stream.getPosition();
                obj = m_Stream.parseObject(this, false);
                if (pos == (m_Stream.getPosition())) 
                    m_Stream.setPosition(m_Stream.getPosition() + 1);
            }
            if (rootObj != null) 
                rootObject = (PdfDictionary)com.pullenti.unisharp.Utils.cast(this.getObject(rootObj.id), PdfDictionary.class);
            if (infoObj != null) 
                info = (PdfDictionary)com.pullenti.unisharp.Utils.cast(this.getObject(infoObj.id), PdfDictionary.class);
        }
        for (int i = 1; i < m_IndirectObjs.size(); i++) {
            if (m_IndirectObjs.get(i) instanceof PdfDictionary) {
                if (((PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class)).isTypeItem("ObjStm")) {
                    if (!this._readObjStm((PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class))) {
                        if (this._checkEncrypt()) 
                            return;
                    }
                }
            }
        }
        boolean isLinearized = false;
        if (rootObject == null) {
            for (int i = 1; i < m_IndirectObjs.size(); i++) {
                if (m_IndirectObjs.get(i) instanceof PdfDictionary) {
                    if (((PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class)).getObject("Linearized", false) != null) {
                        isLinearized = true;
                        break;
                    }
                }
            }
        }
        this._readAllResources();
        if (isLinearized) {
            PdfDictionary firstPage = null;
            int pos0 = m_Stream.getLength();
            for (int i = 1; i < m_IndirectObjs.size(); i++) {
                if (m_IndirectObjs.get(i) instanceof PdfDictionary) {
                    PdfDictionary dic = (PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class);
                    if (dic.isTypeItem("Page")) {
                        pages.add(dic);
                        dic.pageNum = pages.size();
                        if (dic.m_FilePos > 0 && (dic.m_FilePos < pos0)) {
                            firstPage = dic;
                            pos0 = dic.m_FilePos;
                        }
                    }
                    else if (dic.isTypeItem("Catalog")) 
                        rootObject = dic;
                    else if (dic.isTypeItem("Info")) 
                        info = dic;
                }
            }
            if (rootObject != null) {
                pages.clear();
                rootObject.getAllPages(pages);
            }
            else if (firstPage != null && pages.get(0) != firstPage) {
                pages.remove(firstPage);
                pages.add(0, firstPage);
            }
            return;
        }
        if (rootObject == null) {
            for (int i = 1; i < m_IndirectObjs.size(); i++) {
                if (m_IndirectObjs.get(i) instanceof PdfDictionary) {
                    if (((PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class)).isTypeItem("Catalog")) {
                        rootObject = (PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class);
                        break;
                    }
                }
            }
        }
        if (info == null) {
            for (int i = 1; i < m_IndirectObjs.size(); i++) {
                if (m_IndirectObjs.get(i) instanceof PdfDictionary) {
                    if (((PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class)).isTypeItem("Info")) {
                        info = (PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class);
                        break;
                    }
                }
            }
        }
        if (rootObject == null) {
            for (int i = 1; i < m_IndirectObjs.size(); i++) {
                if (m_IndirectObjs.get(i) instanceof PdfDictionary) {
                    if (((PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class)).isTypeItem("XRef")) {
                        objs = new java.util.ArrayList<PdfObject>();
                        for (PdfObject v : m_IndirectObjs) {
                            if (v != null) 
                                objs.add(v);
                        }
                        break;
                    }
                }
            }
        }
        if (rootObject != null) 
            rootObject.getAllPages(pages);
        for (int i = 0; i < pages.size(); i++) {
            pages.get(i).pageNum = i + 1;
        }
    }

    public void writeOnlyOnePage(com.pullenti.unisharp.Stream s, int pageNum) throws java.io.IOException {
        PdfDictionary pdic = rootObject.getDictionary("Pages", null);
        PdfIntValue count = (PdfIntValue)com.pullenti.unisharp.Utils.cast(pdic.getObject("Count", false), PdfIntValue.class);
        if (count != null) {
            count.val = 1;
            count.id = 0;
        }
        PdfArray kids = (PdfArray)com.pullenti.unisharp.Utils.cast(pdic.getObject("Kids", false), PdfArray.class);
        kids.clear();
        java.util.ArrayList<PdfObject> _objs = new java.util.ArrayList<PdfObject>();
        int _id = 1;
        PdfDictionary curPage = null;
        for (int i = 1; i < m_IndirectObjDisp.size(); i++) {
            PdfObject obj = this.getObject(i);
            if (obj == null) 
                continue;
            obj.deleted = true;
            if (obj == pages.get(pageNum) || obj == rootObject || obj == info) {
                obj.deleted = false;
                _objs.add(obj);
                obj.setWriteId(_id++);
                if (obj == pages.get(pageNum)) {
                    curPage = (PdfDictionary)com.pullenti.unisharp.Utils.cast(obj, PdfDictionary.class);
                    kids.add(PdfReference._new185(obj.getWriteId(), obj.id));
                }
            }
        }
        if (kids.id > 0) {
            kids.deleted = false;
            _objs.add(kids);
            kids.setWriteId(_id++);
        }
        pdic.deleted = false;
        _objs.add(pdic);
        pdic.setWriteId(_id++);
        curPage.addObject("Parent", PdfReference._new185(pdic.getWriteId(), pdic.id));
        java.util.ArrayList<PdfReference> refs = new java.util.ArrayList<PdfReference>();
        java.util.ArrayList<Integer> tmp = new java.util.ArrayList<Integer>();
        int k;
        int cou0 = 0;
        for (k = 0; ; k++) {
            int cou = _objs.size();
            for (int i = cou0; i < cou; i++) {
                PdfObject obj0 = _objs.get(i);
                if (obj0.id == 378 || obj0.id == 276) {
                }
                PdfDictionary dic = (PdfDictionary)com.pullenti.unisharp.Utils.cast(obj0, PdfDictionary.class);
                if (dic != null) {
                    for (String key : dic.getKeys()) {
                        if (com.pullenti.unisharp.Utils.stringsEq(key, "StructTreeRoot")) 
                            continue;
                        PdfObject obj = dic.getObject(key, false);
                        if (obj == null) 
                            continue;
                        if (obj.deleted) {
                            if (obj.id == 94) {
                            }
                            if ((obj instanceof PdfDictionary) && ((PdfDictionary)com.pullenti.unisharp.Utils.cast(obj, PdfDictionary.class)).isTypeItem("Page")) 
                                continue;
                            obj.deleted = false;
                            _objs.add(obj);
                            obj.setWriteId(_id++);
                            PdfReference re = (PdfReference)com.pullenti.unisharp.Utils.cast(dic.getObject(key, true), PdfReference.class);
                            if (re != null) 
                                re.setWriteId(obj.getWriteId());
                        }
                        else if (obj.id == 0 && (((obj instanceof PdfDictionary) || (obj instanceof PdfArray)))) 
                            _objs.add(obj);
                    }
                }
                PdfArray arr = (PdfArray)com.pullenti.unisharp.Utils.cast(obj0, PdfArray.class);
                if (arr != null) {
                    for (int j = 0; j < arr.getItemsCount(); j++) {
                        PdfObject obj = arr.getItem(j, false);
                        if (obj == null) 
                            continue;
                        if (obj.deleted) {
                            if (obj.id == 94) {
                            }
                            if ((obj instanceof PdfDictionary) && ((PdfDictionary)com.pullenti.unisharp.Utils.cast(obj, PdfDictionary.class)).isTypeItem("Page")) 
                                continue;
                            obj.deleted = false;
                            _objs.add(obj);
                            obj.setWriteId(_id++);
                            PdfReference re = (PdfReference)com.pullenti.unisharp.Utils.cast(arr.getItem(j, false), PdfReference.class);
                            if (re != null) 
                                re.setWriteId(obj.getWriteId());
                        }
                        else if (obj.id == 0 && (((obj instanceof PdfDictionary) || (obj instanceof PdfArray)))) 
                            _objs.add(obj);
                    }
                }
            }
            if (_objs.size() == cou) 
                break;
            cou0 = cou;
        }
        byte[] buf;
        int ii;
        for (ii = 0; ii < m_Head.length; ii++) {
            if (m_Head[ii] == ((byte)0xA)) {
                ii++;
                break;
            }
        }
        s.write(m_Head, 0, ii);
        int objsCount = 0;
        for (PdfObject obj : _objs) {
            if (obj.id == 0) 
                continue;
            obj.writePos = s.getPosition();
            objsCount++;
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), (((Integer)obj.getWriteId()).toString() + " 0 obj\r\n"));
            s.write(buf, 0, buf.length);
            if (obj.deleted) {
                buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), "null");
                s.write(buf, 0, buf.length);
            }
            else 
                obj.write(s);
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), "\r\nendobj\r\n");
            s.write(buf, 0, buf.length);
        }
        long xrefpos = s.getPosition();
        buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), ("xref\r\n0 " + (objsCount + 1) + "\r\n0000000000 65535 f\r\n"));
        s.write(buf, 0, buf.length);
        for (PdfObject obj : _objs) {
            if (obj.getWriteId() > 0) {
                buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), (String.format("%01d", obj.writePos) + " 00000 n\r\n"));
                s.write(buf, 0, buf.length);
            }
        }
        if (trailer == null) {
            String _trailer = ("trailer\r\n<</Size " + objsCount);
            if (info != null) 
                _trailer = (_trailer + "/Info " + info.getWriteId() + " 0 R");
            if (rootObject != null) 
                _trailer = (_trailer + "/Root " + rootObject.getWriteId() + " 0 R");
            _trailer = (_trailer + ">>\r\nstartxref\r\n" + xrefpos + "\r\n%%EOF");
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), _trailer);
            s.write(buf, 0, buf.length);
        }
        else {
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), "trailer\r\n");
            s.write(buf, 0, buf.length);
            PdfIntValue iii = (PdfIntValue)com.pullenti.unisharp.Utils.cast(trailer.getObject("Size", false), PdfIntValue.class);
            if (iii != null) 
                iii.val = _objs.size();
            if (trailer.getObject("Root", false) == null && rootObject != null) 
                trailer.addObject("Root", PdfReference._new187(rootObject.getWriteId()));
            if (trailer.getObject("Info", false) == null && info != null) 
                trailer.addObject("Info", PdfReference._new187(info.getWriteId()));
            trailer.write(s);
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), ("\r\nstartxref\r\n" + xrefpos + "\r\n%%EOF"));
            s.write(buf, 0, buf.length);
        }
    }

    @Override
    public void write(com.pullenti.unisharp.Stream s) throws java.io.IOException {
        byte[] buf;
        int ii;
        for (ii = 0; ii < m_Head.length; ii++) {
            if (m_Head[ii] == ((byte)0xA)) {
                ii++;
                break;
            }
        }
        s.write(m_Head, 0, ii);
        java.util.ArrayList<PdfReference> refs = new java.util.ArrayList<PdfReference>();
        java.util.ArrayList<PdfObject> _objs = new java.util.ArrayList<PdfObject>();
        java.util.ArrayList<Integer> tmp = new java.util.ArrayList<Integer>();
        for (int i = 1; i < m_IndirectObjDisp.size(); i++) {
            PdfObject obj = this.getObject(i);
            if (obj == null) 
                continue;
            if (obj instanceof PdfDictionary) {
                if (((PdfDictionary)com.pullenti.unisharp.Utils.cast(obj, PdfDictionary.class)).isTypeItem("XRef")) 
                    continue;
            }
            _objs.add(obj);
            obj.setWriteId(_objs.size());
            tmp.clear();
            obj.getAllReferences(refs, tmp);
        }
        for (PdfReference r : refs) {
            PdfObject obj = this.getObject(r.id);
            if (obj != null) 
                r.setWriteId(obj.getWriteId());
        }
        for (PdfObject obj : _objs) {
            obj.writePos = s.getPosition();
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), (((Integer)obj.getWriteId()).toString() + " 0 obj\r\n"));
            s.write(buf, 0, buf.length);
            if (obj.deleted) {
                buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), "null");
                s.write(buf, 0, buf.length);
            }
            else 
                obj.write(s);
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), "\r\nendobj\r\n");
            s.write(buf, 0, buf.length);
        }
        long xrefpos = s.getPosition();
        buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), ("xref\r\n0 " + (_objs.size() + 1) + "\r\n0000000000 65535 f\r\n"));
        s.write(buf, 0, buf.length);
        for (PdfObject obj : _objs) {
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), (String.format("%01d", obj.writePos) + " 00000 n\r\n"));
            s.write(buf, 0, buf.length);
        }
        if (trailer == null) {
            String _trailer = ("trailer\r\n<</Size " + _objs.size());
            if (info != null) 
                _trailer = (_trailer + "/Info " + info.getWriteId() + " 0 R");
            if (rootObject != null) 
                _trailer = (_trailer + "/Root " + rootObject.getWriteId() + " 0 R");
            _trailer = (_trailer + ">>\r\nstartxref\r\n" + xrefpos + "\r\n%%EOF");
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), _trailer);
            s.write(buf, 0, buf.length);
        }
        else {
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), "trailer\r\n");
            s.write(buf, 0, buf.length);
            PdfIntValue iii = (PdfIntValue)com.pullenti.unisharp.Utils.cast(trailer.getObject("Size", false), PdfIntValue.class);
            if (iii != null) 
                iii.val = _objs.size();
            if (trailer.getObject("Root", false) == null && rootObject != null) 
                trailer.addObject("Root", PdfReference._new187(rootObject.getWriteId()));
            if (trailer.getObject("Info", false) == null && info != null) 
                trailer.addObject("Info", PdfReference._new187(info.getWriteId()));
            trailer.write(s);
            buf = com.pullenti.unisharp.Utils.encodeCharset(java.nio.charset.Charset.forName("ASCII"), ("\r\nstartxref\r\n" + xrefpos + "\r\n%%EOF"));
            s.write(buf, 0, buf.length);
        }
        refs.clear();
        _objs.clear();
    }

    private void _readAllResources() throws java.io.IOException {
        for (int i = 1; i < m_IndirectObjs.size(); i++) {
            if (m_IndirectObjs.get(i) instanceof PdfDictionary) {
                PdfDictionary dic = (PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class);
                String sub = dic.getStringItem("Subtype");
                if (com.pullenti.unisharp.Utils.stringsEq(sub, "Image") || com.pullenti.unisharp.Utils.stringsEq(sub, "Font")) 
                    allResources.add(dic);
            }
        }
    }

    private boolean _checkEncrypt() throws java.io.IOException {
        for (int i = 1; i < m_IndirectObjs.size(); i++) {
            if (m_IndirectObjs.get(i) instanceof PdfDictionary) {
                PdfDictionary dic = (PdfDictionary)com.pullenti.unisharp.Utils.cast(m_IndirectObjs.get(i), PdfDictionary.class);
                if (dic.isTypeItem("Encrypt")) {
                    encrypt = dic;
                    return true;
                }
                encrypt = dic.getDictionary("Encrypt", null);
                if (encrypt != null) 
                    return true;
            }
        }
        return false;
    }

    @Override
    public void close() {
        if (m_Stream != null) 
            m_Stream.close();
        m_Stream = null;
    }

    public int getMaxObjsCount() {
        return m_IndirectObjs.size();
    }


    public PdfObject getObject(int _id) throws java.io.IOException {
        if ((_id < 1) || _id >= m_IndirectObjs.size()) 
            return null;
        if (m_IndirectObjs.get(_id) != null) 
            return m_IndirectObjs.get(_id);
        if (m_IndirectObjDisp.get(_id) == 0) 
            return null;
        if (_id == 6) {
        }
        m_Stream.setPosition(m_IndirectObjDisp.get(_id));
        int uid = 0;
        short v = (short)0;
        com.pullenti.unisharp.Outargwrapper<Integer> wrapuid191 = new com.pullenti.unisharp.Outargwrapper<Integer>();
        com.pullenti.unisharp.Outargwrapper<Short> wrapv192 = new com.pullenti.unisharp.Outargwrapper<Short>();
        boolean inoutres193 = m_Stream.tryReadIdAndVersion(false, wrapuid191, wrapv192);
        uid = (wrapuid191.value != null ? wrapuid191.value : 0);
        v = (wrapv192.value != null ? wrapv192.value : 0);
        if (!inoutres193) 
            return null;
        if (uid != _id) 
            return null;
        PdfObject obj = null;
        try {
            obj = m_Stream.parseObject(this, false);
        } catch (Exception ex) {
        }
        if (obj == null) 
            return null;
        obj.id = _id;
        obj.version = v;
        com.pullenti.unisharp.Utils.putArrayValue(m_IndirectObjs, _id, obj);
        return obj;
    }

    private PdfStream m_Stream = null;

    private byte[] m_Head;

    private java.util.ArrayList<PdfObject> m_IndirectObjs = new java.util.ArrayList<PdfObject>();

    private java.util.ArrayList<Integer> m_IndirectObjDisp = new java.util.ArrayList<Integer>();

    private void _addObj(int _id, PdfObject obj) {
        while (_id >= m_IndirectObjs.size()) {
            m_IndirectObjs.add(null);
            m_IndirectObjDisp.add(0);
        }
        com.pullenti.unisharp.Utils.putArrayValue(m_IndirectObjs, _id, obj);
    }

    private void _addIndir(int _id, int disp) {
        while (_id >= m_IndirectObjs.size()) {
            m_IndirectObjs.add(null);
            m_IndirectObjDisp.add(0);
        }
        com.pullenti.unisharp.Utils.putArrayValue(m_IndirectObjDisp, _id, disp);
    }

    private boolean _readXRefs() throws java.io.IOException {
        if (m_Stream.getLength() < 100) 
            return false;
        m_Stream.setPosition(m_Stream.getLength() - 100);
        String str;
        while ((((str = m_Stream.readWord(true)))) != null) {
            if (com.pullenti.unisharp.Utils.stringsEq(str, "startxref")) 
                break;
        }
        if (str == null) 
            return false;
        str = m_Stream.readWord(true);
        int disp;
        com.pullenti.unisharp.Outargwrapper<Integer> wrapdisp200 = new com.pullenti.unisharp.Outargwrapper<Integer>();
        boolean inoutres201 = com.pullenti.unisharp.Utils.parseInteger(str, 0, null, wrapdisp200);
        disp = (wrapdisp200.value != null ? wrapdisp200.value : 0);
        if (!inoutres201) 
            return false;
        PdfObject infObj = null;
        while (true) {
            if ((disp < 0) || disp >= m_Stream.getLength()) 
                return false;
            m_Stream.setPosition(disp);
            str = m_Stream.readWord(false);
            if (com.pullenti.unisharp.Utils.stringsNe(str, "xref")) 
                return false;
            while (true) {
                int id0;
                int count;
                int i;
                str = m_Stream.readWord(true);
                com.pullenti.unisharp.Outargwrapper<Integer> wrapid0198 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres199 = com.pullenti.unisharp.Utils.parseInteger(str, 0, null, wrapid0198);
                id0 = (wrapid0198.value != null ? wrapid0198.value : 0);
                if (!inoutres199) 
                    break;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcount196 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres197 = com.pullenti.unisharp.Utils.parseInteger(m_Stream.readWord(true), 0, null, wrapcount196);
                count = (wrapcount196.value != null ? wrapcount196.value : 0);
                if (!inoutres197) 
                    break;
                for (i = 0; i < count; i++,id0++) {
                    String w = m_Stream.readWord(true);
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapdisp194 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                    boolean inoutres195 = com.pullenti.unisharp.Utils.parseInteger(w, 0, null, wrapdisp194);
                    disp = (wrapdisp194.value != null ? wrapdisp194.value : 0);
                    if (!inoutres195) 
                        break;
                    m_Stream.readWord(true);
                    str = m_Stream.readWord(true);
                    if (str == null || str.length() != 1) 
                        break;
                    if (com.pullenti.unisharp.Utils.stringsEq(str, "n")) 
                        this._addIndir(id0, disp);
                }
                if (i < count) 
                    break;
            }
            if (com.pullenti.unisharp.Utils.stringsNe(str, "trailer")) 
                return false;
            if (com.pullenti.unisharp.Utils.stringsNe(m_Stream.readWord(true), "<")) 
                return false;
            if (com.pullenti.unisharp.Utils.stringsNe(m_Stream.readWord(true), "<")) 
                return false;
            trailer = new PdfDictionary();
            trailer.sourceFile = this;
            trailer.postParse(m_Stream);
            if (trailer.getObject("Encrypt", false) != null) 
                encrypt = trailer.getDictionary("Encrypt", null);
            if (rootObject == null) {
                PdfObject oo = trailer.getObject("Root", false);
                if (oo != null) 
                    rootObject = (PdfDictionary)com.pullenti.unisharp.Utils.cast(this.getObject(oo.id), PdfDictionary.class);
            }
            if (infObj == null) 
                infObj = trailer.getObject("Info", true);
            double d = 0.0;
            PdfIntValue prev = (PdfIntValue)com.pullenti.unisharp.Utils.cast(trailer.getObject("Prev", false), PdfIntValue.class);
            if (prev == null) 
                break;
            disp = prev.val;
        }
        if (infObj != null) 
            info = (PdfDictionary)com.pullenti.unisharp.Utils.cast(this.getObject(infObj.id), PdfDictionary.class);
        return rootObject != null;
    }

    private boolean _readObjStm(PdfDictionary dic) throws Exception, java.io.FileNotFoundException, java.io.IOException {
        byte[] dat = dic.extractData();
        if (dat == null || (dat.length < 1)) 
            return false;
        int count = dic.getIntItem("N");
        if (count < 1) 
            return false;
        try (PdfStream pstr = new PdfStream(null, dat)) {
            int[] ids = new int[count];
            int[] disps = new int[count];
            for (int i = 0; i < count; i++) {
                PdfIntValue _id = (PdfIntValue)com.pullenti.unisharp.Utils.cast(pstr.parseObject(this, false), PdfIntValue.class);
                if (_id == null) 
                    break;
                PdfIntValue disp = (PdfIntValue)com.pullenti.unisharp.Utils.cast(pstr.parseObject(this, false), PdfIntValue.class);
                if (disp == null) 
                    break;
                ids[i] = _id.val;
                disps[i] = disp.val;
            }
            int n0 = pstr.getPosition();
            for (int i = 0; i < count; i++) {
                pstr.setPosition(n0 + disps[i]);
                PdfObject obj = pstr.parseObject(this, false);
                if (obj != null) {
                    obj.id = ids[i];
                    obj.sourceFile = this;
                    this._addObj(obj.id, obj);
                    if (obj instanceof PdfDictionary) {
                        if (((PdfDictionary)com.pullenti.unisharp.Utils.cast(obj, PdfDictionary.class)).isTypeItem("ObjStm")) {
                        }
                    }
                }
            }
        }
        return true;
    }

    public java.util.ArrayList<PdfObject> getAllWhoRefObject0(int objId) throws java.io.IOException {
        java.util.ArrayList<PdfObject> res = new java.util.ArrayList<PdfObject>();
        java.util.ArrayList<Integer> ids = new java.util.ArrayList<Integer>();
        for (int i = 1; i < getMaxObjsCount(); i++) {
            PdfObject o = this.getObject(i);
            if (o == null || o.id == objId) 
                continue;
            ids.clear();
            o.getAllWhoRefObject(objId, o, res, ids);
        }
        return res;
    }
    public PdfFile() {
        super();
    }
}
