1 14 15 package mx4j.tools.adaptor.http; 16 17 18 import java.io.BufferedOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 27 32 33 public class HttpOutputStream extends BufferedOutputStream 34 35 { 36 37 40 41 protected int code; 42 43 44 47 48 protected boolean sendHeaders; 49 50 51 54 55 protected Map headers = new HashMap (7); 56 57 58 66 67 public HttpOutputStream(OutputStream out, HttpInputStream in) 68 69 { 70 71 super(out); 72 73 code = HttpConstants.STATUS_OKAY; 74 75 setHeader("Server", HttpConstants.SERVER_INFO); 76 77 sendHeaders = (in.getVersion() >= 1.0); 78 79 } 80 81 82 87 88 public void setCode(int code) 89 90 { 91 92 this.code = code; 93 94 } 95 96 97 103 104 public void setHeader(String attr, String value) 105 106 { 107 108 headers.put(attr, value); 109 110 } 111 112 113 119 120 public boolean sendHeaders() throws IOException 121 122 { 123 124 if (sendHeaders) 125 126 { 127 128 StringBuffer buffer = new StringBuffer (512); 129 130 buffer.append(HttpConstants.HTTP_VERSION); 131 132 buffer.append(code); 133 134 buffer.append(" "); 135 136 buffer.append(HttpUtil.getCodeMessage(code)); 137 138 buffer.append("\r\n"); 139 140 Iterator attrs = headers.keySet().iterator(); 141 142 int size = headers.size(); 143 144 145 for (int i = 0; i < size; i++) 146 147 { 148 149 String attr = (String )attrs.next(); 150 151 buffer.append(attr); 152 153 buffer.append(": "); 154 155 buffer.append(headers.get(attr)); 156 157 buffer.append("\r\n"); 158 159 } 160 161 buffer.append("\r\n"); 162 163 write(buffer.toString()); 164 165 } 166 167 return sendHeaders; 168 169 } 170 171 172 178 179 public void write(String msg) throws IOException 180 181 { 182 183 write(msg.getBytes("latin1")); 184 185 } 186 187 188 194 195 public void write(InputStream in) throws IOException 196 197 { 198 199 int n; 200 201 int length = buf.length; 202 203 while ((n = in.read(buf, count, length - count)) >= 0) 204 205 { 206 207 if ((count += n) >= length) 208 209 { 210 211 out.write(buf, count = 0, length); 212 213 } 214 215 } 216 217 } 218 219 } 220 221 | Popular Tags |