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 java.text.DecimalFormat;
034: import java.text.NumberFormat;
035: import de.intarsys.pdf.writer.COSWriter;
036: import de.intarsys.tools.string.StringTools;
037:
038: /**
039: * A XRef serializer to the classical XRef format.
040: */
041: public class XRefTrailerWriter extends AbstractXRefWriter {
042: public static final byte[] TYPE_FREE = "f".getBytes();
043:
044: public static final byte[] TYPE_OCCUPIED = "n".getBytes();
045:
046: public static final NumberFormat FORMAT_XREF_GENERATION = new DecimalFormat(
047: "00000");
048:
049: public static final NumberFormat FORMAT_XREF_OFFSET = new DecimalFormat(
050: "0000000000");
051:
052: public static final byte[] XREF = "xref".getBytes();
053:
054: public XRefTrailerWriter(COSWriter cosWriter) {
055: super (cosWriter);
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see de.intarsys.pdf.storage.AbstractXRefWriter#initialize()
062: */
063: protected void initialize(STXRefSection xRefSection)
064: throws IOException {
065: super .initialize(xRefSection);
066: setRandomAccess(getCosWriter().getRandomAccess());
067: getRandomAccess().write(XRefTrailerWriter.XREF);
068: getRandomAccess().write(COSWriter.EOL);
069: }
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see de.intarsys.pdf.storage.AbstractXRefWriter#finish()
075: */
076: protected void finish(STXRefSection xRefSection) throws IOException {
077: getCosWriter().write(COSWriter.TRAILER);
078: getCosWriter().writeEOL();
079: getCosWriter().writeObject(xRefSection.cosGetDict());
080: super .finish(xRefSection);
081: }
082:
083: /*
084: * (non-Javadoc)
085: *
086: * @see de.intarsys.pdf.storage.IXRefEntryVisitor#visitFromCompressed(de.intarsys.pdf.storage.STXRefEntryCompressed)
087: */
088: public void visitFromCompressed(STXRefEntryCompressed entry)
089: throws XRefEntryVisitorException {
090: // not supported, so write a free entry
091: try {
092: write(entry.getObjectNumber(), 65535, getTypeFree());
093: } catch (IOException e) {
094: throw new XRefEntryVisitorException(e);
095: }
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see de.intarsys.pdf.storage.AbstractXRefWriter#write(int, int, byte[])
102: */
103: protected void write(int col1, int col2, byte[] type)
104: throws IOException {
105: String stCol1 = XRefTrailerWriter.FORMAT_XREF_OFFSET
106: .format(col1);
107: String stCol2 = XRefTrailerWriter.FORMAT_XREF_GENERATION
108: .format(col2);
109: getRandomAccess().write(StringTools.toByteArray(stCol1));
110: getRandomAccess().write(COSWriter.SPACE);
111: getRandomAccess().write(StringTools.toByteArray(stCol2));
112: getRandomAccess().write(COSWriter.SPACE);
113: getRandomAccess().write(type);
114: getRandomAccess().write(COSWriter.CRLF);
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see de.intarsys.pdf.storage.AbstractXRefWriter#visitFromSection(de.intarsys.pdf.storage.STXRefSubsection)
121: */
122: protected void visitFromSubsection(STXRefSubsection section)
123: throws IOException {
124: getRandomAccess().write(
125: StringTools.toByteArray(Integer.toString(section
126: .getStart())));
127: getRandomAccess().write(COSWriter.SPACE);
128: getRandomAccess().write(
129: StringTools.toByteArray(Integer.toString(section
130: .getSize())));
131: getRandomAccess().write(COSWriter.EOL);
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see de.intarsys.pdf.storage.AbstractXRefWriter#getTypeCompressed()
138: */
139: protected byte[] getTypeCompressed() {
140: return TYPE_FREE;
141: }
142:
143: /*
144: * (non-Javadoc)
145: *
146: * @see de.intarsys.pdf.storage.AbstractXRefWriter#getTypeFree()
147: */
148: protected byte[] getTypeFree() {
149: return TYPE_FREE;
150: }
151:
152: /*
153: * (non-Javadoc)
154: *
155: * @see de.intarsys.pdf.storage.AbstractXRefWriter#getTypeOccupied()
156: */
157: protected byte[] getTypeOccupied() {
158: return TYPE_OCCUPIED;
159: }
160: }
|