Skip to main content
summaryrefslogtreecommitdiffstats
blob: bd4b2a4d851414ccf0f9dad5b03e82de0785b04a (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.webapp.servlet;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class ServletPrintWriter extends PrintWriter{

	private StringBuffer buffer;

	public ServletPrintWriter() {
		super(new ByteArrayOutputStream());
		buffer = new StringBuffer();
	}

    /**
     * Writes a single character.
     * @param c int specifying a character to be written.
     */
    @Override
	public void write(int c) {
	    synchronized (lock) {
	    	buffer.append((char)(c));
	    }
    }

    /**
     * Writes A Portion of an array of characters.
     * @param buf Array of characters
     * @param off Offset from which to start writing characters
     * @param len Number of characters to write
     */
    @Override
	public void write(char buf[], int off, int len) {
	    synchronized (lock) {
	    	buffer.append(buf, off, len);
	    }
    }

    /**
     * Writes an array of characters.  This method cannot be inherited from the
     * Writer class because it must suppress I/O exceptions.
     * @param buf Array of characters to be written
     */
    @Override
	public void write(char buf[]) {
    	write(buf, 0, buf.length);
    }

    /**
     * Writes a portion of a string.
     * @param s A String
     * @param off Offset from which to start writing characters
     * @param len Number of characters to write
     */
    @Override
	public void write(String s, int off, int len) {
	    synchronized (lock) {
	    	buffer.append(s.toCharArray(), off, off+len);
	    }
    }

    /**
     * Writes a string.  This method cannot be inherited from the Writer class
     * because it must suppress I/O exceptions.
     * @param s String to be written
     */
    @Override
	public void write(String s) {
	write(s, 0, s.length());
    }

    @Override
	public String toString()
    {
    	return buffer.toString();
    }
}

Back to the top