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.io.entity;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.nio.ByteBuffer;
34  import java.nio.channels.Channels;
35  import java.nio.channels.WritableByteChannel;
36  
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.util.Args;
39  
40  /**
41   * An entity that delivers the contents of a {@link ByteBuffer}.
42   */
43  public class ByteBufferEntity extends AbstractHttpEntity {
44  
45      private final ByteBuffer buffer;
46  
47      private class ByteBufferInputStream extends InputStream {
48  
49          ByteBufferInputStream() {
50              buffer.position(0);
51          }
52  
53          @Override
54          public int read() throws IOException {
55              if (!buffer.hasRemaining()) {
56                  return -1;
57              }
58              return buffer.get() & 0xFF;
59          }
60  
61          @Override
62          public int read(final byte[] bytes, final int off, final int len) throws IOException {
63              if (!buffer.hasRemaining()) {
64                  return -1;
65              }
66  
67              final int chunk = Math.min(len, buffer.remaining());
68              buffer.get(bytes, off, chunk);
69              return chunk;
70          }
71      }
72  
73      public ByteBufferEntity(final ByteBuffer buffer, final ContentType contentType) {
74          super();
75          Args.notNull(buffer, "Source byte buffer");
76          this.buffer = buffer;
77          if (contentType != null) {
78              setContentType(contentType.toString());
79          }
80      }
81  
82      public ByteBufferEntity(final ByteBuffer buffer) {
83          this(buffer, null);
84      }
85  
86      @Override
87      public boolean isRepeatable() {
88          return true;
89      }
90  
91      @Override
92      public long getContentLength() {
93          return buffer.capacity();
94      }
95  
96      @Override
97      public InputStream getContent() throws IOException, UnsupportedOperationException {
98          return new ByteBufferInputStream();
99      }
100 
101     @Override
102     public void writeTo(final OutputStream outstream) throws IOException {
103         Args.notNull(outstream, "Output stream");
104         final WritableByteChannel channel = Channels.newChannel(outstream);
105         channel.write(buffer);
106         outstream.flush();
107     }
108 
109     @Override
110     public boolean isStreaming() {
111         return false;
112     }
113 
114     @Override
115     public void close() throws IOException {
116     }
117 
118 }