001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.st;
031:
032: import java.io.IOException;
033: import de.intarsys.pdf.cos.COSBasedObject;
034: import de.intarsys.pdf.cos.COSName;
035: import de.intarsys.pdf.cos.COSObject;
036: import de.intarsys.pdf.cos.COSStream;
037: import de.intarsys.pdf.parser.COSDocumentParser;
038: import de.intarsys.pdf.parser.COSLoadException;
039: import de.intarsys.tools.randomaccess.IRandomAccess;
040: import de.intarsys.tools.randomaccess.RandomAccessByteArray;
041:
042: /**
043: * A COSStream containing other COSObjects.
044: */
045: public class COSObjectStream extends COSBasedObject {
046: /**
047: * The meta class implementation
048: */
049: public static class MetaClass extends COSBasedObject.MetaClass {
050: protected MetaClass(Class instanceClass) {
051: super (instanceClass);
052: }
053: }
054:
055: /** The meta class instance */
056: static public final MetaClass META = new MetaClass(MetaClass.class
057: .getDeclaringClass());
058:
059: public static final COSName CN_Type_ObjStm = COSName
060: .constant("ObjStm"); //$NON-NLS-1$
061:
062: public static final COSName DK_First = COSName.constant("First"); //$NON-NLS-1$
063:
064: public static final COSName DK_N = COSName.constant("N"); //$NON-NLS-1$
065:
066: public static final COSName DK_Extends = COSName
067: .constant("Extends"); //$NON-NLS-1$
068:
069: private COSStream stream;
070:
071: private int[][] objectTable;
072:
073: private IRandomAccess randomAccess;
074:
075: protected COSObjectStream(COSObject stream) {
076: super (((COSStream) stream).getDict());
077: this .stream = (COSStream) stream;
078: }
079:
080: public int getFirst() {
081: return getFieldInt(DK_First, -1);
082: }
083:
084: public int getN() {
085: return getFieldInt(DK_N, 0);
086: }
087:
088: private int getOffsetByIndex(int index, COSDocumentParser parser)
089: throws IOException {
090: if (objectTable == null) {
091: objectTable = new int[getN()][2];
092: getRandomAccess().seek(0);
093: for (int i = 0; i < objectTable.length; i++) {
094: objectTable[i][0] = parser.readInteger(
095: getRandomAccess(), true);
096: objectTable[i][1] = parser.readInteger(
097: getRandomAccess(), true);
098: }
099: }
100: return objectTable[index][1];
101: }
102:
103: protected IRandomAccess getRandomAccess() throws IOException {
104: if (randomAccess == null) {
105: randomAccess = new RandomAccessByteArray(stream
106: .getDecodedBytes());
107: }
108: return randomAccess;
109: }
110:
111: public COSObject loadObject(int index, COSDocumentParser parser)
112: throws IOException, COSLoadException {
113: if (index >= getN()) {
114: return null;
115: }
116: int offset = getOffsetByIndex(index, parser);
117: getRandomAccess().seek(getFirst() + offset);
118: return (COSObject) parser.parseElement(getRandomAccess());
119: }
120:
121: public void parse(int index, COSDocumentParser parser)
122: throws IOException, COSLoadException {
123: if (index >= getN()) {
124: return;
125: }
126: int offset = getOffsetByIndex(index, parser);
127: getRandomAccess().seek(getFirst() + offset);
128: parser.parseElement(getRandomAccess());
129: }
130: }
|