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<>();
53          this.outputStreamRef = new AtomicReference<>();
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          }
65          local = getInputStream(socket);
66          if (inputStreamRef.compareAndSet(null, local)) {
67              return local;
68          }
69          return inputStreamRef.get();
70      }
71  
72      protected InputStream getInputStream(final Socket socket) throws IOException {
73          return socket.getInputStream();
74      }
75  
76      protected OutputStream getOutputStream(final Socket socket) throws IOException {
77          return socket.getOutputStream();
78      }
79  
80      public final OutputStream getOutputStream() throws IOException {
81          OutputStream local = outputStreamRef.get();
82          if (local != null) {
83              return local;
84          }
85          local = getOutputStream(socket);
86          if (outputStreamRef.compareAndSet(null, local)) {
87              return local;
88          }
89          return outputStreamRef.get();
90      }
91  
92      @Override
93      public String toString() {
94          return socket.toString();
95      }
96  
97  }