aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/system/BufferManager.java
blob: 75959de4963d3529071d7a83bcb70232b9f8b91e (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

// Seshadri
/*
 * This package is to be used for all Oeprating System related activities.
 * This file manages system buffers
 */

package org.apache.fop.system;


// FOP

import org.apache.fop.fo.FONode;


// Java

import java.io.*;
import java.util.Hashtable;


public class BufferManager {


    protected FileWriter fw;
    protected FileReader fr;
    protected char cache[];    // Cache
    protected long csize;      // Cache size


    protected File buff = null;

    protected long fp = 0;

    protected long markStart =
        0;                     // used to set the current point in the stream while reading
    protected long markEnd = 0;
    protected long curMark = 0;

    // Hash of objects and their offsets within

    Hashtable offSetTable = new Hashtable();

    private class Offset {

        long fp = 0;    // File Pointer
        int length;
        char[] data;    // when no buffer is specified

        Offset(long fp, int length, char data[]) {
            this.fp = fp;
            this.length = length;
            this.data = data;
        }

    }



    public void addBufferFile(File buff) {

        if (buff != null)
            try {
                fw = new FileWriter(buff);
                fr = new FileReader(buff);
                csize = 100000;
                this.buff = buff;
            } catch (Exception e) {
                System.out.println(e);
            }

    }

    public void writeBuffer(Object obj, char arr[]) {

        int length = arr.length;

        if (buff != null) {
            offSetTable.put(obj, new Offset(this.fp, length, null));
            try {
                fw.write(arr);

                this.fp += length;
            } catch (Exception e) {
                System.out.println(e);
            }
        } else {
            // Store the data in memory
            offSetTable.put(obj, new Offset(this.fp, length, arr));
        }


    }


    public void readComplete() {

        // An indication that manager can close the writable buffers and prepare
        // for reading..
        if (buff != null)
            try {

                fw.close();

                cache = new char[(int)csize];
                setupCache(curMark);

            } catch (Exception e) {
                System.out.println(e);
            }
    }




    public char[] readBuffer(Object obj) {

        Offset values = (Offset)offSetTable.get(obj);

        // Was buffering used?

        if (buff != null) {


            char ca[] = new char[values.length];

            // Check if csize is too small

            if (csize < values.length) {
                System.out.println("Cache size too small");
                System.exit(0);
            }


            // Is the data outside the cache?

            if (!(values.fp >= markStart
                    && values.fp + values.length <= markEnd)) {

                setupCache(values.fp);
            }


            for (long i = values.fp - markStart, j = 0; j < values.length;
                    ++i, ++j) {

                ca[(int)j] = cache[(int)i];
            }


            return ca;
        } else {
            return values.data;
        }
    }

    protected void setupCache(long curMark) {

        try {

            FileReader fr = new FileReader(buff);
            fr.skip(curMark);

            long rem = buff.length() - curMark;
            if (rem > csize) {

                rem = csize;
            }

            fr.read(cache, 0, (int)rem);


            markStart = curMark;
            markEnd = rem - 1;

        } catch (Exception e) {
            System.out.println(e);
        }


    }


}