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.nio.ByteBuffer;
33
34 import org.apache.hc.core5.http.ContentType;
35 import org.apache.hc.core5.util.Args;
36
37 /**
38 * An entity that delivers the contents of a {@link ByteBuffer}.
39 */
40 public class ByteBufferEntity extends AbstractHttpEntity {
41
42 private final ByteBuffer buffer;
43 private final long length;
44
45 /**
46 * Constructs a new instance with the given attributes kept as immutable.
47 * <p>
48 * The new instance:
49 * </p>
50 * <ul>
51 * <li>is not chunked.</li>
52 * </ul>
53 *
54 * @param buffer The message body contents as a byte buffer.
55 * @param contentType The content-type, may be null.
56 * @param contentEncoding The content encoding string, may be null.
57 */
58 public ByteBufferEntity(final ByteBuffer buffer, final ContentType contentType, final String contentEncoding) {
59 super(contentType, contentEncoding);
60 Args.notNull(buffer, "Source byte buffer");
61 this.buffer = buffer;
62 this.length = buffer.remaining();
63 }
64
65 /**
66 * Constructs a new instance with the given attributes kept as immutable.
67 * <p>
68 * The new instance:
69 * </p>
70 * <ul>
71 * <li>is not chunked.</li>
72 * <li>does not define a content encoding.</li>
73 * </ul>
74 *
75 * @param buffer The message body contents as a byte buffer.
76 * @param contentType The content-type, may be null.
77 */
78 public ByteBufferEntity(final ByteBuffer buffer, final ContentType contentType) {
79 this(buffer, contentType, null);
80 }
81
82 /**
83 * {@inheritDoc}
84 * <p>
85 * This implementation always returns {@code true}.
86 * </p>
87 */
88 @Override
89 public final boolean isRepeatable() {
90 return false;
91 }
92
93 @Override
94 public final long getContentLength() {
95 return length;
96 }
97
98 @Override
99 public final InputStream getContent() throws IOException, UnsupportedOperationException {
100 return new InputStream() {
101
102 @Override
103 public int read() throws IOException {
104 if (!buffer.hasRemaining()) {
105 return -1;
106 }
107 return buffer.get() & 0xFF;
108 }
109
110 @Override
111 public int read(final byte[] bytes, final int off, final int len) throws IOException {
112 if (!buffer.hasRemaining()) {
113 return -1;
114 }
115 final int chunk = Math.min(len, buffer.remaining());
116 buffer.get(bytes, off, chunk);
117 return chunk;
118 }
119 };
120 }
121
122 /**
123 * {@inheritDoc}
124 * <p>
125 * This implementation always returns {@code false}.
126 * </p>
127 */
128 @Override
129 public final boolean isStreaming() {
130 return false;
131 }
132
133 /**
134 * {@inheritDoc}
135 * <p>
136 * This implementation is a no-op.
137 * </p>
138 */
139 @Override
140 public final void close() throws IOException {
141 }
142
143 }