File: FCGIOutputStream.java

package info (click to toggle)
iipimage 1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 7,376 kB
  • sloc: sh: 8,127; cpp: 7,791; ansic: 3,983; java: 872; makefile: 200; perl: 2
file content (335 lines) | stat: -rw-r--r-- 9,141 bytes parent folder | download | duplicates (15)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * @(#)FCGIOutputStream.java
 *
 *      FastCGi compatibility package Interface
 *
 *  Copyright (c) 1996 Open Market, Inc.
 *
 * See the file "LICENSE.TERMS" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * $Id: FCGIOutputStream.java,v 1.3 2000/03/21 12:12:26 robs Exp $
 */
package com.fastcgi;

import java.io.*;

/**
 * This stream understands FCGI prototcol.
 */

public class FCGIOutputStream 
    extends OutputStream 
{
    private static final String RCSID = "$Id: FCGIOutputStream.java,v 1.3 2000/03/21 12:12:26 robs Exp $";

    /* Stream vars */

    public int wrNext;
    public int stop;
    public boolean isClosed;

    /* require methods to set, get and clear */
    private int errno;
    private Exception errex;

    /* data vars */

    public byte buff[];
    public int buffLen;
    public int buffStop;
    public int type;
    public boolean isAnythingWritten;
    public boolean rawWrite;
    public FCGIRequest request;

    public FileOutputStream out;

    /**
    * Creates a new output stream to manage fcgi prototcol stuff
    * @param out the output stream  buflen  length of buffer streamType
    */
    public FCGIOutputStream(FileOutputStream outStream,
        int bufLen, int streamType,
        FCGIRequest inreq) {
        out = outStream;
        buffLen = Math.min(bufLen, FCGIGlobalDefs.def_FCGIMaxLen);
        buff = new byte[buffLen];
        type = streamType;
        stop = buffStop = buffLen;
        isAnythingWritten = false;
        rawWrite = false;
        wrNext = FCGIGlobalDefs.def_FCGIHeaderLen;
        isClosed = false;
        request = inreq;
    }

    /**
    * Writes a byte to the output stream.
    */
    public void  write(int c) throws IOException {
        if(wrNext != stop) {
            buff[wrNext++] = (byte)c;
            return;
        }
        if(isClosed) {
            throw new EOFException();
        }
        empty(false);
        if(wrNext != stop) {
            buff[wrNext++] = (byte)c;
            return;
        }
        /* NOTE: ASSERT(stream->isClosed); */
        /* bug in emptyBuffProc if not */
        throw new EOFException();
    }

    /**
    * Writes an array of bytes. This method will block until the bytes
    * are actually written.
    * @param b  the data to be written
    */
    public  void write(byte b[]) throws IOException{
        write(b, 0, b.length);
    }

    /**
    * Writes len consecutive bytes from off in the array b
    * into the output stream.  Performs no interpretation
    * of the output bytes. Making the user convert the string to
    * bytes is in line with current Java practice.
    */
    public void write(byte b[], int off, int len) throws IOException {
        int m, bytesMoved;
        /*
        * Fast path: room for n bytes in the buffer
        */
        if(len <= (stop - wrNext)) {
            System.arraycopy(b, off, buff, wrNext, len);
            wrNext += len;
            return;
        }
        /*
        * General case: stream is closed or buffer empty procedure
        * needs to be called
        */
        bytesMoved = 0;
        for (;;) {
            if(wrNext != stop) {
                m = Math.min(len - bytesMoved, stop - wrNext);
                System.arraycopy(b, off, buff, wrNext, m);
                bytesMoved += m;
                wrNext += m;
                if(bytesMoved == len) {
                    return;
                }
                off += m;
            }
            if(isClosed) {
                throw new EOFException();
            }
            empty(false);
        }
    }

    /**
    * Encapsulates any buffered stream content in a FastCGI
    * record.  If !doClose, writes the data, making the buffer
    * empty.
    */
    public void empty(boolean doClose) throws IOException {
        int cLen;
        /*
        * Alignment padding omitted in Java
        */
        if (!rawWrite) {
            cLen = wrNext - FCGIGlobalDefs.def_FCGIHeaderLen;
            if(cLen > 0) {
                System.arraycopy(new FCGIMessage().makeHeader(type,
                    request.requestID, cLen, 0),
                    0, buff, 0,
                    FCGIGlobalDefs.def_FCGIHeaderLen);
            }
            else {
                wrNext = 0;
            }
        }
        if (doClose) {
            writeCloseRecords();
        }
        if (wrNext != 0) {
            isAnythingWritten = true;
            try {
                out.write(buff, 0, wrNext);
            } catch (IOException e) {
                setException(e);
                return;
            }
            wrNext = 0;
        }
        /*
        * The buffer is empty.
        */
        if(!rawWrite) {
            wrNext += FCGIGlobalDefs.def_FCGIHeaderLen;
        }
    }

    /**
    * Close the stream.
    */
    public void  close() throws IOException {
        if (isClosed) {
            return;
        }
        empty(true);
        /*
        * if isClosed, will return with EOFException from write.
        */
        isClosed = true;
        stop = wrNext;
        return;
    }

    /**
    * Flushes any buffered output.
    * Server-push is a legitimate application of flush.
    * Otherwise, it is not very useful, since FCGIAccept
    * does it implicitly.  flush may reduce performance
    * by increasing the total number of operating system calls
    * the application makes.
    */
    public void flush() throws IOException {
        if (isClosed) {
            return;
        }
        empty(false);
        /*
        * if isClosed, will return with EOFException from write.
        */
        return;
    }

    /**
    * An FCGI error has occurred. Save the error code in the stream
    * for diagnostic purposes and set the stream state so that
    * reads return EOF
    */
    public void setFCGIError(int errnum) {
        /*
        * Preserve only the first error.
        */
        if (errno == 0) {
            errno = errnum;
        }
        isClosed = true;
    }

    /**
    * An Exception has occurred. Save the Exception in the stream
    * for diagnostic purposes and set the stream state so that
    * reads return EOF
    */
    public void setException(Exception errexpt) {
        /*
        * Preserve only the first error.
        */
        if (errex == null) {
            errex = errexpt;
        }
        isClosed = true;
    }

    /**
    * Clear the stream error code and end-of-file indication.
    */
    public void clearFCGIError() {
        errno = 0;
        /*
        * isClosed = false;
        * XXX: should clear isClosed but work is needed to make it safe
        * to do so.
        */
    }

    /**
    * Clear the stream error code and end-of-file indication.
    */
    public void clearException() {
        errex = null;
        /*
        * isClosed = false;
        * XXX: should clear isClosed but work is needed to make it safe
        * to do so.
        */
    }

    /**
    * accessor method since var is private
    */
    public int etFCGIError() {
        return errno;
    }

    /**
    * accessor method since var is private
    */
    public Exception getException() {
        return errex;
    }

    /**
    * Writes an EOF record for the stream content if necessary.
    * If this is the last writer to close, writes an FCGI_END_REQUEST
    * record.
    */
    public void writeCloseRecords() throws IOException {
        FCGIMessage msg = new FCGIMessage();
        /*
        * Enter rawWrite mode so final records won't be
        * encapsulated as
        * stream data.
        */
        rawWrite = true;
        /*
        * Generate EOF for stream content if needed.
        */
        if(!(type == FCGIGlobalDefs.def_FCGIStderr
            && wrNext == 0
            && !isAnythingWritten)) {
            byte hdr[] =
                new byte[FCGIGlobalDefs.def_FCGIHeaderLen];
            System.arraycopy(msg.makeHeader(type,
                request.requestID,
                0, 0),
                0, hdr,0,
                FCGIGlobalDefs.def_FCGIHeaderLen);
            write(hdr, 0, hdr.length);
        }
        /*
        * Generate FCGI_END_REQUEST record if needed.
        */
        if(request.numWriters == 1) {
            byte endReq[] =
                new byte[FCGIGlobalDefs.def_FCGIHeaderLen
                + FCGIGlobalDefs.def_FCGIEndReqBodyLen];
            System.arraycopy(msg.makeHeader(
                FCGIGlobalDefs.def_FCGIEndRequest,
                request.requestID,
                FCGIGlobalDefs.def_FCGIEndReqBodyLen,0),
                0, endReq, 0,
                FCGIGlobalDefs.def_FCGIHeaderLen);
            System.arraycopy(msg.makeEndrequestBody(
                request.appStatus,
                FCGIGlobalDefs.def_FCGIRequestComplete),
                0,endReq,
                FCGIGlobalDefs.def_FCGIHeaderLen,
                FCGIGlobalDefs.def_FCGIEndReqBodyLen);
            write(endReq,0, FCGIGlobalDefs.def_FCGIHeaderLen
                + FCGIGlobalDefs.def_FCGIEndReqBodyLen);
        }
        request.numWriters--;
    }
}