1 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 16 public class PdfStream 17 extends PdfObject { 18 19 23 protected ByteBuffer _bb; 24 25 28 protected PdfDictionary _d; 29 30 34 protected static final PdfName PDFNAME_LENGTH = new PdfName("Length"); 35 36 41 protected PdfStream() { 42 } 43 44 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 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 84 public PdfDictionary getDictionary() { 85 synchronized (this) { 86 return _d; 87 } 88 } 89 90 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 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 |