01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.jmeter.protocol.http.util;
20:
21: import java.io.ByteArrayInputStream;
22: import java.io.ByteArrayOutputStream;
23: import java.io.IOException;
24: import java.io.InputStream;
25: import java.io.OutputStream;
26: import java.net.InetAddress;
27: import java.net.Socket;
28: import java.net.UnknownHostException;
29:
30: /*
31: * Socket that reads back from the output
32: */
33: public class LoopbackHTTPSocket extends Socket {
34:
35: // get access to buffer
36: static class LoopbackOutputStream extends ByteArrayOutputStream {
37: byte[] getBuffer() {
38: return buf;
39: }
40: }
41:
42: // wrap read() methods to track output buffer
43: static class LoopBackInputStream extends ByteArrayInputStream {
44: LoopbackOutputStream os;
45:
46: public synchronized int read() {
47: buf = os.getBuffer(); // make sure buffer details
48: count = buf.length; // track the output
49: return super .read();
50: }
51:
52: public synchronized int read(byte[] b, int off, int len) {
53: buf = os.getBuffer();
54: count = buf.length;
55: return super .read(b, off, len);
56: }
57:
58: public LoopBackInputStream(LoopbackOutputStream _os) {
59: super (_os.getBuffer());
60: os = _os;
61: }
62: }
63:
64: private final LoopbackOutputStream os;
65:
66: private LoopbackHTTPSocket() throws IOException {
67: os = new LoopbackOutputStream();
68: // Preload the output so that can be read back as HTTP
69: os.write("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"
70: .getBytes());
71: }
72:
73: public LoopbackHTTPSocket(String host, int port,
74: InetAddress localAddress, int localPort, int timeout)
75: throws IOException {
76: this ();
77: }
78:
79: public LoopbackHTTPSocket(String host, int port,
80: InetAddress localAddr, int localPort) throws IOException {
81: this ();
82: }
83:
84: public LoopbackHTTPSocket(String host, int port)
85: throws UnknownHostException, IOException {
86: this ();
87: }
88:
89: // Override so we can intercept the stream
90: public OutputStream getOutputStream() throws IOException {
91: return os;
92: }
93:
94: // Override so we can intercept the stream
95: public InputStream getInputStream() throws IOException {
96: return new LoopBackInputStream(os);
97: }
98: }
|