001    package railo.runtime.writer;
002    
003    import java.io.IOException;
004    import java.io.OutputStream;
005    import java.io.Writer;
006    
007    import javax.servlet.ServletOutputStream;
008    import javax.servlet.http.HttpServletResponse;
009    
010    import railo.runtime.net.http.ReqRspUtil;
011    
012    /**
013     * Implementation of a JSpWriter
014     */
015    public class JspWriterImplStringBuffer extends CFMLWriter { 
016         
017            private static final int BUFFER_SIZE = 1024; 
018        
019        private Writer out;
020    
021        private HttpServletResponse response;
022    
023        private boolean flushed;
024    
025        //private boolean closed;
026    
027        private String headData;
028        
029        private StringBuffer buffer=new StringBuffer(BUFFER_SIZE);
030        //private CharBuffer buffer=new CharBuffer(BUFFER_SIZE);
031        
032        private boolean closed=false;
033        
034        /**
035         * constructor of the class
036         * @param response Response Object
037         * @param bufferSize buffer Size
038         * @param autoFlush do auto flush Content
039         */
040        public JspWriterImplStringBuffer(HttpServletResponse response, int bufferSize, boolean autoFlush) {
041            super(bufferSize, autoFlush);
042            this.response=response;
043            this.autoFlush=autoFlush;
044            this.bufferSize=bufferSize;
045            //initBuffer(response.getCharacterEncoding());
046        }
047    
048        /**
049         * constructor of the class
050         * @param response Response Object
051         */
052        public JspWriterImplStringBuffer(HttpServletResponse response) {
053            this(response, BUFFER_SIZE, false);
054        }
055        
056        private void _check() throws IOException {
057            if(autoFlush && buffer.length()>bufferSize)  {
058                _flush();
059            }
060        }
061    
062        /**
063         * @throws IOException
064         */
065        protected void initOut() throws IOException {
066            if (out == null) {
067                out=response.getWriter();
068            }
069        }
070    
071            /*public void splitForCacheTo(Resource res) throws IOException {
072                    initOut();
073                    out=new CacheWriter(out,res);
074            } 
075            
076            public void clearCacheSplit() {
077                    if(out instanceof CacheWriter) {
078                            CacheWriter cw = (CacheWriter)out;
079                            out=cw.getOut();
080                            if(cw.getCacheFile().exists())cw.getCacheFile().delete();
081                    }
082            } */
083        
084    
085        /**
086         * @see javax.servlet.jsp.JspWriter#print(char[]) 
087         */ 
088        public void print(char[] arg) throws IOException { 
089            buffer.append(arg);
090            _check();
091        }
092        
093        /**
094         * reset configuration of buffer
095         * @param bufferSize size of the buffer
096         * @param autoFlush does the buffer autoflush
097         * @throws IOException
098         */
099        public void setBufferConfig(int bufferSize, boolean autoFlush) throws IOException {
100            this.bufferSize=bufferSize;
101            this.autoFlush=autoFlush;
102            _check();
103        }
104        
105        /**
106         * 
107         * @param headData
108         * @throws IOException
109         */
110        public void appendHTMLHead(String headData) throws IOException {
111            if(!flushed) {
112                if(this.headData==null)this.headData=headData;
113                else this.headData+=headData;
114            }
115            else throw new IOException("page already flushed");
116        }
117        
118    
119        
120        /**
121         * @see railo.runtime.writer.CFMLWriter#writeHTMLHead(java.lang.String)
122         */
123        public void writeHTMLHead(String headData) throws IOException {
124            if(!flushed) {
125                    this.headData=headData;
126            }
127            else throw new IOException("page already flushed");
128        }
129        
130    
131        
132        /** 
133         * @see railo.runtime.writer.CFMLWriter#getHTMLHead()
134         */
135        public String getHTMLHead() throws IOException {
136            if(flushed) throw new IOException("page already flushed");
137            return headData==null?"":headData;
138        }
139        
140        /** 
141         * @see railo.runtime.writer.CFMLWriter#resetHTMLHead()
142         */
143        public void resetHTMLHead() throws IOException {
144            if(flushed) throw new IOException("page already flushed");
145            headData=null;
146        }    
147        
148        /**
149         * just a wrapper function for ACF
150         * @throws IOException 
151         */
152        public void initHeaderBuffer() throws IOException{
153            resetHTMLHead();
154        }
155    
156    
157        /** 
158         * @see java.io.Writer#write(char[], int, int) 
159         */ 
160        public void write(char[] cbuf, int off, int len) throws IOException { 
161            buffer.append(cbuf,off,len);
162            _check();
163        }
164    
165        /** 
166         * @see javax.servlet.jsp.JspWriter#clear() 
167         */ 
168        public void clear() throws IOException { 
169            if (flushed)  throw new IOException("jsp writer is already flushed");
170            clearBuffer();
171        } 
172    
173        /** 
174         * @see javax.servlet.jsp.JspWriter#clearBuffer() 
175         */ 
176        public void clearBuffer() { 
177            //buffer.clear();
178            buffer=new StringBuffer(BUFFER_SIZE);
179        } 
180    
181        /** 
182         * @see java.io.Writer#flush() 
183         */ 
184        public void flush() throws IOException { 
185            flushBuffer();
186            // weil flushbuffer das out erstellt muss ich nicht mehr checken
187            out.flush();
188        } 
189        /** 
190         * @see java.io.Writer#flush() 
191         */ 
192        private void _flush() throws IOException { 
193            flushBuffer();
194            // weil flushbuffer das out erstellt muss ich nicht mehr checken
195            out.flush();
196        } 
197    
198        /**
199         * Flush the output buffer to the underlying character stream, without
200         * flushing the stream itself.  This method is non-private only so that it
201         * may be invoked by PrintStream.
202         * @throws IOException
203         */
204        protected final void flushBuffer() throws IOException {
205            flushed = true;
206            initOut();
207            out.write(_toString());
208            //_writeOut(out);
209            clearBuffer();
210        } 
211        
212        
213    
214        private String _toString() {
215            if(headData==null) {
216                headData=null;
217                return buffer.toString();
218            }
219            String str=buffer.toString();
220            int index=str.indexOf("</head>");
221            if(index==-1) {
222                str= headData.concat(str);
223                headData=null;
224                return str;
225            }
226            str= str.substring(0,index).concat(headData).concat(str.substring(index));
227            headData=null;
228            return str;
229        }
230        
231        
232    
233        /** 
234         * @see java.io.Writer#close() 
235         */ 
236        public void close() throws IOException { 
237            if (response == null || closed) return;
238            if(out==null) { 
239                    out=response.getWriter();
240                    byte[] barr = _toString().getBytes(response.getCharacterEncoding());
241                    ReqRspUtil.setContentLength(response,barr.length);
242                ServletOutputStream os = response.getOutputStream();
243                os.write(barr);
244                out.flush();
245                out.close();
246            }
247            else {
248                flush();
249                out.close();
250                out = null;
251            }
252            closed = true;
253        } 
254    
255        /** 
256         * @see javax.servlet.jsp.JspWriter#getRemaining() 
257         */ 
258        public int getRemaining() { 
259            return bufferSize - buffer.length();
260        }
261    
262        /** 
263         * @see javax.servlet.jsp.JspWriter#newLine() 
264         */ 
265        public void newLine() throws IOException { 
266            println();
267        } 
268    
269        /** 
270         * @see javax.servlet.jsp.JspWriter#print(boolean) 
271         */ 
272        public void print(boolean arg) throws IOException { 
273            print(arg?new char[]{'t','r','u','e'}:new char[]{'f','a','l','s','e'}); 
274        }
275    
276        /** 
277         * @see javax.servlet.jsp.JspWriter#print(char) 
278         */ 
279        public void print(char arg) throws IOException { 
280            buffer.append(arg);
281            _check();
282        } 
283    
284        /** 
285         * @see javax.servlet.jsp.JspWriter#print(int) 
286         */ 
287        public void print(int arg) throws IOException { 
288            _print(String.valueOf(arg)); 
289        } 
290    
291        /** 
292         * @see javax.servlet.jsp.JspWriter#print(long) 
293         */ 
294        public void print(long arg) throws IOException { 
295            _print(String.valueOf(arg)); 
296    
297        } 
298    
299        /** 
300         * @see javax.servlet.jsp.JspWriter#print(float) 
301         */ 
302        public void print(float arg) throws IOException { 
303            _print(String.valueOf(arg)); 
304        } 
305    
306        /** 
307         * @see javax.servlet.jsp.JspWriter#print(double) 
308         */ 
309        public void print(double arg) throws IOException { 
310            _print(String.valueOf(arg)); 
311        } 
312    
313        /** 
314         * @see javax.servlet.jsp.JspWriter#print(java.lang.String) 
315         */ 
316        public void print(String arg) throws IOException { 
317            buffer.append(arg);
318            _check();
319        } 
320    
321        /** 
322         * @see javax.servlet.jsp.JspWriter#print(java.lang.Object) 
323         */ 
324        public void print(Object arg) throws IOException { 
325            _print(String.valueOf(arg)); 
326        } 
327    
328        /** 
329         * @see javax.servlet.jsp.JspWriter#println() 
330         */ 
331        public void println() throws IOException { 
332            _print("\n"); 
333    
334        } 
335    
336        /** 
337         * @see javax.servlet.jsp.JspWriter#println(boolean) 
338         */ 
339        public void println(boolean arg) throws IOException { 
340            print(arg?new char[]{'t','r','u','e','\n'}:new char[]{'f','a','l','s','e','\n'}); 
341        } 
342    
343        /** 
344         * @see javax.servlet.jsp.JspWriter#println(char) 
345         */ 
346        public void println(char arg) throws IOException { 
347            print(new char[]{arg,'\n'}); 
348        } 
349    
350        /** 
351         * @see javax.servlet.jsp.JspWriter#println(int) 
352         */ 
353        public void println(int arg) throws IOException { 
354            print(arg); 
355            println(); 
356        } 
357    
358        /** 
359         * @see javax.servlet.jsp.JspWriter#println(long) 
360         */ 
361        public void println(long arg) throws IOException { 
362            print(arg); 
363            println(); 
364        } 
365    
366        /** 
367         * @see javax.servlet.jsp.JspWriter#println(float) 
368         */ 
369        public void println(float arg) throws IOException { 
370            print(arg); 
371            println(); 
372        } 
373    
374        /** 
375         * @see javax.servlet.jsp.JspWriter#println(double) 
376         */ 
377        public void println(double arg) throws IOException { 
378            print(arg); 
379            println(); 
380        } 
381    
382        /** 
383         * @see javax.servlet.jsp.JspWriter#println(char[]) 
384         */ 
385        public void println(char[] arg) throws IOException { 
386            print(arg); 
387            println(); 
388        } 
389    
390        /** 
391         * @see javax.servlet.jsp.JspWriter#println(java.lang.String) 
392         */ 
393        public void println(String arg) throws IOException { 
394            _print(arg); 
395            println(); 
396        } 
397    
398        /** 
399         * @see javax.servlet.jsp.JspWriter#println(java.lang.Object) 
400         */ 
401        public void println(Object arg) throws IOException { 
402            print(arg); 
403            println(); 
404        }
405        
406        /** 
407         * @see java.io.Writer#write(char[]) 
408         */ 
409        public void write(char[] cbuf) throws IOException { 
410            print(cbuf); 
411        } 
412    
413        /** 
414         * @see java.io.Writer#write(int) 
415         */ 
416        public void write(int c) throws IOException { 
417            print(c); 
418        } 
419    
420        /** 
421         * @see java.io.Writer#write(java.lang.String, int, int) 
422         */ 
423        public void write(String str, int off, int len) throws IOException { 
424            write(str.toCharArray(),off,len);
425        } 
426    
427        /** 
428         * @see java.io.Writer#write(java.lang.String) 
429         */ 
430        public void write(String str) throws IOException { 
431            _print(str);
432        }
433    
434        /**
435         * @return Returns the flushed.
436         */
437        public boolean isFlushed() {
438            return flushed;
439        }
440    
441        public void setClosed(boolean closed) {
442            this.closed=closed;
443        }
444    
445        private void _print(String arg) throws IOException { 
446            buffer.append(arg);
447            _check();
448        }
449    
450        
451        /**
452             * @see railo.runtime.writer.CFMLWriter#getResponseStream()
453             */
454            public OutputStream getResponseStream() throws IOException {
455                    return response.getOutputStream();
456            }
457        
458        /**
459             * @see railo.runtime.writer.CFMLWriter#writeRaw(java.lang.String)
460             */
461            public void writeRaw(String str) throws IOException {
462                    write(str);
463            }
464    }