comparison src/goodjava/webserver/Connection.java @ 1493:471ef3e6a84e

more io
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 May 2020 00:12:15 -0600
parents fb003c4003dd
children a0a74f5f490a
comparison
equal deleted inserted replaced
1492:aaac1d29edea 1493:471ef3e6a84e
5 import java.io.IOException; 5 import java.io.IOException;
6 import java.net.Socket; 6 import java.net.Socket;
7 import goodjava.logging.Logger; 7 import goodjava.logging.Logger;
8 import goodjava.logging.LoggerFactory; 8 import goodjava.logging.LoggerFactory;
9 import goodjava.parser.ParseException; 9 import goodjava.parser.ParseException;
10 import goodjava.io.IoUtils;
10 11
11 12
12 final class Connection { 13 final class Connection {
13 private static final Logger logger = LoggerFactory.getLogger(Connection.class); 14 private static final Logger logger = LoggerFactory.getLogger(Connection.class);
14 15
113 response.headers.put("content-length",Long.toString(response.body.length)); 114 response.headers.put("content-length",Long.toString(response.body.length));
114 byte[] header = response.toHeaderString().getBytes(); 115 byte[] header = response.toHeaderString().getBytes();
115 116
116 OutputStream out = socket.getOutputStream(); 117 OutputStream out = socket.getOutputStream();
117 out.write(header); 118 out.write(header);
118 copyAll(response.body.content,out); 119 IoUtils.copyAll(response.body.content,out);
119 out.close(); 120 out.close();
120 socket.close(); 121 socket.close();
121 } catch(IOException e) { 122 } catch(IOException e) {
122 logger.info("",e); 123 logger.info("",e);
123 } 124 }
124 } 125 }
125 126
126 private static void copyAll(InputStream in,OutputStream out)
127 throws IOException
128 {
129 byte[] a = new byte[8192];
130 int n;
131 while( (n=in.read(a)) != -1 ) {
132 out.write(a,0,n);
133 }
134 }
135
136 } 127 }