1 52 53 package com.go.trove.net; 54 55 import java.io.*; 56 import java.net.Socket ; 57 import com.go.trove.io.*; 58 59 66 public class BufferedSocket extends FilteredSocket { 67 private final int mInBufSize; 68 private final int mOutBufSize; 69 70 private InputStream mIn; 71 private OutputStream mOut; 72 73 public BufferedSocket(Socket s) throws IOException { 74 this(s, -1, -1); 75 } 76 77 81 public BufferedSocket(Socket s, 82 int inputBufferSize, 83 int outputBufferSize) throws IOException { 84 super(s); 85 mInBufSize = inputBufferSize; 86 mOutBufSize = outputBufferSize; 87 } 88 89 public synchronized InputStream getInputStream() throws IOException { 90 if (mIn == null) { 91 InputStream in = super.getInputStream(); 92 93 if (mInBufSize < 0) { 94 mIn = new FastBufferedInputStream(in); 95 } 96 else if (mInBufSize > 0) { 97 mIn = new FastBufferedInputStream(in, mInBufSize); 98 } 99 else { 100 mIn = in; 101 } 102 } 103 return mIn; 104 } 105 106 public synchronized OutputStream getOutputStream() throws IOException { 107 if (mOut == null) { 108 OutputStream out = super.getOutputStream(); 109 110 if (mOutBufSize < 0) { 111 mOut = new FastBufferedOutputStream(out); 112 } 113 else if (mOutBufSize > 0) { 114 mOut = new FastBufferedOutputStream(out, mOutBufSize); 115 } 116 else { 117 mOut = out; 118 } 119 } 120 return mOut; 121 } 122 123 public synchronized void close() throws IOException { 124 if (mOutBufSize != 0 && mOut != null) { 126 mOut.close(); 127 } 128 super.close(); 129 } 130 } 131 | Popular Tags |