View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.impl.io;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.net.Socket;
34  import java.util.concurrent.atomic.AtomicReference;
35  
36  import org.apache.hc.core5.util.Args;
37  
38  /**
39   * Utility class that holds a {@link Socket} along with copies of its {@link InputStream}
40   * and {@link OutputStream}.
41   *
42   * @since 5.0
43   */
44  public class SocketHolder {
45  
46      private final Socket socket;
47      private final AtomicReference<InputStream> inputStreamRef;
48      private final AtomicReference<OutputStream> outputStreamRef;
49  
50      public SocketHolder(final Socket socket) {
51          this.socket = Args.notNull(socket, "Socket");
52          this.inputStreamRef = new AtomicReference<>(null);
53          this.outputStreamRef = new AtomicReference<>(null);
54      }
55  
56      public final Socket getSocket() {
57          return socket;
58      }
59  
60      public final InputStream getInputStream() throws IOException {
61          InputStream local = inputStreamRef.get();
62          if (local != null) {
63              return local;
64          } else {
65              local = getInputStream(socket);
66              if (inputStreamRef.compareAndSet(null, local)) {
67                  return local;
68              } else {
69                  return inputStreamRef.get();
70              }
71          }
72      }
73  
74      protected InputStream getInputStream(final Socket socket) throws IOException {
75          return socket.getInputStream();
76      }
77  
78      protected OutputStream getOutputStream(final Socket socket) throws IOException {
79          return socket.getOutputStream();
80      }
81  
82      public final OutputStream getOutputStream() throws IOException {
83          OutputStream local = outputStreamRef.get();
84          if (local != null) {
85              return local;
86          } else {
87              local = getOutputStream(socket);
88              if (outputStreamRef.compareAndSet(null, local)) {
89                  return local;
90              } else {
91                  return outputStreamRef.get();
92              }
93          }
94      }
95  
96      @Override
97      public String toString() {
98          return socket.toString();
99      }
100 
101 }