001 /* 002 * Copyright 2000-2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.scaffold.http; 018 019 import java.net.*; 020 import java.io.*; 021 022 /** A shorthand way to create BufferedReaders and 023 * PrintWriters associated with a Socket. 024 * <P> 025 * Taken from Core Servlets and JavaServer Pages 026 * from Prentice Hall and Sun Microsystems Press, 027 * http://www.coreservlets.com/. 028 * © 2000 Marty Hall; may be freely used or adapted. 029 */ 030 public class SocketUtil { 031 032 /** 033 * Make a BufferedReader to get incoming data. 034 */ 035 public static BufferedReader getReader(Socket s) 036 throws IOException { 037 return(new BufferedReader( 038 new InputStreamReader(s.getInputStream()))); 039 } 040 041 042 /** 043 * Make a PrintWriter to send outgoing data. 044 * This PrintWriter will automatically flush stream 045 * when println is called. 046 */ 047 public static PrintWriter getWriter(Socket s) 048 throws IOException { 049 // 2nd argument of true means autoflush 050 return(new PrintWriter(s.getOutputStream(), true)); 051 052 } 053 054 055 } // end SocketUtil 056