KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > PdfStream


1 /*
2   Copyright (C) Etymon Systems, Inc. <http://www.etymon.com/>
3 */

4
5 package com.etymon.pjx;
6
7 import java.io.*;
8 import java.nio.*;
9 import java.nio.channels.*;
10 import java.util.*;
11
12 /**
13    Represents the PDF stream object. This class is synchronized.
14    @author Nassib Nassar
15 */

16 public class PdfStream
17     extends PdfObject {
18
19     /**
20        The byte sequence contained by the stream. The position is
21        maintained at 0, and the limit is maintained at capacity().
22     */

23     protected ByteBuffer _bb;
24
25     /**
26        The stream dictionary.
27     */

28     protected PdfDictionary _d;
29
30     /**
31            A <code>PdfName</code> object representing the name
32            <code>Length</code>.
33     */

34     protected static final PdfName PDFNAME_LENGTH = new PdfName("Length");
35
36     /**
37        A protected constructor intended to be called only from
38        {@link #wrap(PdfDictionary, ByteBuffer) wrap(PdfDictionary,
39        ByteByffer)}.
40      */

41     protected PdfStream() {
42     }
43
44     /**
45        Constructs a stream object from a PDF dictionary and a
46        <code>ByteBuffer</code>. The stream's byte sequence is
47        read from the <code>ByteBuffer</code> starting at its
48        current position and ending at its limit.
49        @param d the PDF dictionary.
50        @param bb the <code>ByteBuffer</code>.
51      */

52     public PdfStream(PdfDictionary d, ByteBuffer bb) {
53
54         int streamLength = bb.remaining();
55         
56         Map map = new HashMap(d.getMap());
57         map.put(PDFNAME_LENGTH, new PdfInteger(streamLength));
58         _d = new PdfDictionary(map);
59         
60         _bb = ByteBuffer.allocateDirect(streamLength);
61         _bb.put(bb);
62         _bb.position(0);
63
64     }
65
66     /**
67        Returns the byte sequence contained in this stream.
68        @return the byte sequence. The returned
69        <code>ByteBuffer</code> object is read-only.
70      */

71     public ByteBuffer getBuffer() {
72         synchronized (this) {
73             ByteBuffer bb = _bb.asReadOnlyBuffer();
74             bb.position(0);
75             bb.limit(bb.capacity());
76             return bb;
77         }
78     }
79
80     /**
81        Returns this stream's dictionary.
82        @return the stream dictionary.
83      */

84     public PdfDictionary getDictionary() {
85         synchronized (this) {
86             return _d;
87         }
88     }
89
90     /**
91        A factory for fast construction of this class. The
92        constructed object will be a wrapper around the specified
93        <code>PdfDictionary</code> and <code>ByteBuffer</code>.
94        The entire capacity of <code>ByteBuffer</code> is assumed
95        to represent the stream's byte sequence. The Length value
96        in the stream dictionary is assumed to be correct. The
97        calling method must ensure that the <code>ByteBuffer</code>
98        is never externally modified or accessed, in order to meet
99        the immutability requirement of {@link PdfObject
100        PdfObject}.
101        @param m the <code>PdfDictionary</code> and
102        <code>ByteBuffer</code> to be used to back this stream.
103        @return the constructed object.
104      */

105     protected static PdfStream wrap(PdfDictionary d, ByteBuffer bb) {
106         PdfStream ps = new PdfStream();
107         ps._d = d;
108         bb.position(0);
109         bb.limit(bb.capacity());
110         ps._bb = bb;
111         return ps;
112     }
113     
114     protected int writePdf(PdfWriter w, boolean spacing) throws IOException {
115         synchronized (this) {
116
117             DataOutputStream dos = w.getDataOutputStream();
118             FileChannel fc = w.getFileChannel();
119             
120             int count = _d.writePdf(w, false);
121             
122             dos.writeBytes("\nstream\n");
123             count += 8;
124
125             ByteBuffer bb = _bb;
126             int bbcap = bb.capacity();
127             if (fc != null) {
128                 dos.flush();
129                 fc.write(bb);
130             } else {
131                 byte[] b = new byte[bbcap];
132                 bb.get(b);
133                 dos.write(b);
134             }
135             count += bbcap;
136             bb.position(0);
137             
138             dos.writeBytes("endstream");
139             return count + 9;
140             
141         }
142     }
143
144     public boolean equals(Object JavaDoc obj) {
145         synchronized (this) {
146
147             if ( (obj == null) || ( !(obj instanceof PdfStream) ) ) {
148                 return false;
149             }
150
151             PdfStream s = (PdfStream)obj;
152
153             return ( ( _d.equals(s._d) ) &&
154                  ( _bb.equals(s._bb) ) );
155
156         }
157     }
158
159     public int hashCode() {
160         synchronized (this) {
161             return _d.hashCode() ^ _bb.hashCode();
162         }
163     }
164
165 }
166
Popular Tags