~ubuntu-branches/ubuntu/natty/aspectj/natty

« back to all changes in this revision

Viewing changes to org.aspectj/modules/testing/src/org/aspectj/testing/util/StreamSniffer.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-10-04 16:37:23 UTC
  • mfrom: (1.1.3 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091004163723-ck4y7j7fhjxskkie
Tags: 1.6.6+dfsg-1
* New upstream release.
  - Update 02_use_gjdoc.diff patch
* Update my email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* *******************************************************************
2
 
 * Copyright (c) 1999-2001 Xerox Corporation, 
3
 
 *               2002 Palo Alto Research Center, Incorporated (PARC).
4
 
 * All rights reserved. 
5
 
 * This program and the accompanying materials are made available 
6
 
 * under the terms of the Eclipse Public License v1.0 
7
 
 * which accompanies this distribution and is available at 
8
 
 * http://www.eclipse.org/legal/epl-v10.html 
9
 
 *  
10
 
 * Contributors: 
11
 
 *     Xerox/PARC     initial implementation 
12
 
 * ******************************************************************/
13
 
 
14
 
 
15
 
/*
16
 
 * StreamGrabber.java created on May 16, 2002
17
 
 *
18
 
 */
19
 
package org.aspectj.testing.util;
20
 
 
21
 
import java.io.FilterOutputStream;
22
 
import java.io.IOException;
23
 
import java.io.OutputStream;
24
 
 
25
 
/**
26
 
  * Listen to a stream using StringBuffer.
27
 
  * Clients install and remove buffer to enable/disable listening.
28
 
  * Does not affect data passed to underlying stream
29
 
  */
30
 
public class StreamSniffer extends FilterOutputStream {
31
 
    StringBuffer buffer;
32
 
    /** have to use delegate, not super, because super we will double-count input */
33
 
    final OutputStream delegate;
34
 
    
35
 
    public StreamSniffer(OutputStream stream) {
36
 
        super(stream);
37
 
        delegate = stream;
38
 
    }
39
 
 
40
 
    /** set to null to stop copying */
41
 
    public void setBuffer(StringBuffer sb) {
42
 
        buffer = sb;
43
 
    }
44
 
 
45
 
    //---------------- FilterOutputStream 
46
 
    public void write(int b) throws IOException {
47
 
        StringBuffer sb = buffer;
48
 
        if (null != sb) {
49
 
            if ((b > Character.MAX_VALUE) 
50
 
                || (b < Character.MIN_VALUE)) {
51
 
                throw new Error("don't know double-byte"); // XXX
52
 
            } else {
53
 
                sb.append((char) b);
54
 
            }
55
 
        }
56
 
        delegate.write(b);
57
 
    }
58
 
    
59
 
    public void write(byte[] b) throws IOException {
60
 
        StringBuffer sb = buffer;
61
 
        if (null != sb) {
62
 
            String s = new String(b);
63
 
            sb.append(s);
64
 
        }
65
 
        delegate.write(b);
66
 
    }
67
 
    
68
 
    public void write(byte[] b, int offset, int length) throws IOException {
69
 
        StringBuffer sb = buffer;
70
 
        if (null != sb) {
71
 
            String s = new String(b, offset, length);
72
 
            sb.append(s);
73
 
        }
74
 
        delegate.write(b, offset, length);
75
 
    }
76
 
}