1 /* 2 * Copyright 2000-2004 The Apache Software Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.apache.commons.scaffold.http; 18 19 import java.net.*; 20 import java.io.*; 21 22 /** A shorthand way to create BufferedReaders and 23 * PrintWriters associated with a Socket. 24 * <P> 25 * Taken from Core Servlets and JavaServer Pages 26 * from Prentice Hall and Sun Microsystems Press, 27 * http://www.coreservlets.com/. 28 * © 2000 Marty Hall; may be freely used or adapted. 29 */ 30 public class SocketUtil { 31 32 /** 33 * Make a BufferedReader to get incoming data. 34 */ 35 public static BufferedReader getReader(Socket s) 36 throws IOException { 37 return(new BufferedReader( 38 new InputStreamReader(s.getInputStream()))); 39 } 40 41 42 /** 43 * Make a PrintWriter to send outgoing data. 44 * This PrintWriter will automatically flush stream 45 * when println is called. 46 */ 47 public static PrintWriter getWriter(Socket s) 48 throws IOException { 49 // 2nd argument of true means autoflush 50 return(new PrintWriter(s.getOutputStream(), true)); 51 52 } 53 54 55 } // end SocketUtil 56