1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.pdf.xref;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.apache.fop.pdf.PDFObjectNumber;
public class CrossReferenceStreamTestCase extends CrossReferenceObjectTest {
private List<Long> uncompressedObjectOffsets;
private List<CompressedObjectReference> compressedObjectReferences;
@Test
public void testWithNoOffset() throws IOException {
List<Long> emptyList = Collections.emptyList();
test(emptyList);
}
@Test
public void testWithOffsets() throws IOException {
test(new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L)));
}
@Test
public void testWithBigOffsets() throws IOException {
test(new ArrayList<Long>(Arrays.asList(0xffL, 0xffffL, 0xffffffffL, 0xffffffffffffffffL)));
}
@Test
public void testWithObjectStreams1() throws IOException {
List<CompressedObjectReference> compressedObjectReferences =
Arrays.asList(new CompressedObjectReference(new PDFObjectNumber(2), new PDFObjectNumber(1), 0));
test(Arrays.asList(0L, null), compressedObjectReferences);
}
@Test
public void testWithObjectStreams2() throws IOException {
int numIndirectObjects = 2;
int numCompressedObjects = 1;
List<Long> indirectObjectOffsets
= new ArrayList<Long>(numIndirectObjects + numCompressedObjects);
for (long i = 0; i < numIndirectObjects; i++) {
indirectObjectOffsets.add(i);
}
List<CompressedObjectReference> compressedObjectReferences
= new ArrayList<CompressedObjectReference>();
for (int index = 0; index < numCompressedObjects; index++) {
indirectObjectOffsets.add(null);
int obNum = numIndirectObjects + index + 1;
compressedObjectReferences.add(new CompressedObjectReference(new PDFObjectNumber(obNum),
new PDFObjectNumber(numIndirectObjects), index));
}
test(indirectObjectOffsets, compressedObjectReferences);
}
private void test(List<Long> indirectObjectOffsets) throws IOException {
List<CompressedObjectReference> compressedObjectReferences = Collections.emptyList();
test(indirectObjectOffsets, compressedObjectReferences);
}
private void test(List<Long> indirectObjectOffsets,
List<CompressedObjectReference> compressedObjectReferences) throws IOException {
this.uncompressedObjectOffsets = indirectObjectOffsets;
this.compressedObjectReferences = compressedObjectReferences;
runTest();
}
@Override
protected CrossReferenceObject createCrossReferenceObject() {
return new CrossReferenceStream(pdfDocument,
uncompressedObjectOffsets.size() + 1,
trailerDictionary,
STARTXREF,
uncompressedObjectOffsets,
compressedObjectReferences);
}
@Override
protected byte[] createExpectedCrossReferenceData() throws IOException {
List<ObjectReference> objectReferences
= new ArrayList<ObjectReference>(uncompressedObjectOffsets.size());
for (Long offset : uncompressedObjectOffsets) {
objectReferences.add(offset == null ? null : new UncompressedObjectReference(offset));
}
for (CompressedObjectReference ref : compressedObjectReferences) {
objectReferences.set(ref.getObjectNumber().getNumber() - 1, ref);
}
int maxObjectNumber = objectReferences.size() + 1;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
StringBuilder expected = new StringBuilder(256);
expected.append(maxObjectNumber + " 0 obj\n")
.append("<<\n")
.append(" /Root 1 0 R\n")
.append(" /Info 2 0 R\n")
.append(" /ID [<0123456789ABCDEF> <0123456789ABCDEF>]\n")
.append(" /Type /XRef\n")
.append(" /Size ").append(Integer.toString(maxObjectNumber + 1)).append('\n')
.append(" /W [1 8 2]\n")
.append(" /Length ").append(Integer.toString((maxObjectNumber + 1) * 11)).append('\n')
.append(">>\n")
.append("stream\n");
stream.write(getBytes(expected));
DataOutputStream data = new DataOutputStream(stream);
data.write(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xff, (byte) 0xff});
for (ObjectReference objectReference : objectReferences) {
objectReference.output(data);
}
data.write(1);
data.writeLong(STARTXREF);
data.write(0);
data.write(0);
data.close();
stream.write(getBytes("\nendstream\nendobj\n"));
return stream.toByteArray();
}
}
|