Skip to content

Commit 06ca036

Browse files
authored
Deprecate MediaTypeCodec and use Message Body Writer / Reader API for clients (#11121)
1 parent 8c15d0b commit 06ca036

File tree

38 files changed

+723
-168
lines changed

38 files changed

+723
-168
lines changed

config/checkstyle/custom-suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<suppress checks=".*" files="FlowControlHandler.java" />
1818

19-
<suppress checks="ParameterNumber" files="DefaultHttpClient.java" />
19+
<suppress checks="ParameterNumber" files="DefaultHttpClient.java|DefaultJdkHttpClient.java|JdkBlockingHttpClient.java" />
2020

2121
<suppress checks="IllegalImport" files="JavaxProviderBeanDefinition.java" />
2222

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2017-2024 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.core.io.buffer;
17+
18+
import io.micronaut.core.annotation.Internal;
19+
20+
/**
21+
* {@link ByteBufferFactory} implementation based on simple byte arrays.
22+
*
23+
* @author Jonas Konrad
24+
* @since 4.7
25+
*/
26+
@Internal
27+
public class ByteArrayBufferFactory implements ByteBufferFactory<Void, byte[]> {
28+
public static final ByteArrayBufferFactory INSTANCE = new ByteArrayBufferFactory();
29+
30+
private ByteArrayBufferFactory() {
31+
}
32+
33+
@Override
34+
public Void getNativeAllocator() {
35+
throw new UnsupportedOperationException("No native allocator");
36+
}
37+
38+
@Override
39+
public ByteArrayByteBuffer buffer() {
40+
return buffer(0);
41+
}
42+
43+
@Override
44+
public ByteArrayByteBuffer buffer(int initialCapacity) {
45+
return new ByteArrayByteBuffer(new byte[initialCapacity]);
46+
}
47+
48+
@Override
49+
public ByteArrayByteBuffer buffer(int initialCapacity, int maxCapacity) {
50+
return buffer(initialCapacity);
51+
}
52+
53+
@Override
54+
public ByteArrayByteBuffer copiedBuffer(byte[] bytes) {
55+
return wrap(bytes.clone());
56+
}
57+
58+
@Override
59+
public ByteArrayByteBuffer copiedBuffer(java.nio.ByteBuffer nioBuffer) {
60+
int pos = nioBuffer.position();
61+
int lim = nioBuffer.limit();
62+
byte[] arr = new byte[lim - pos];
63+
nioBuffer.get(pos, arr, 0, arr.length);
64+
return wrap(arr);
65+
}
66+
67+
@Override
68+
public ByteArrayByteBuffer wrap(byte[] existing) {
69+
return new ByteArrayByteBuffer(existing);
70+
}
71+
}
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/*
2+
* Copyright 2017-2023 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.core.io.buffer;
17+
18+
import io.micronaut.core.annotation.Experimental;
19+
import io.micronaut.core.annotation.Internal;
20+
21+
import java.io.ByteArrayInputStream;
22+
import java.io.InputStream;
23+
import java.io.OutputStream;
24+
import java.nio.charset.Charset;
25+
import java.util.Arrays;
26+
27+
/**
28+
* A {@link ByteBuffer} implementation that is backed by a byte array.
29+
*
30+
* @since 4.7
31+
*/
32+
@Internal
33+
@Experimental
34+
public class ByteArrayByteBuffer implements ByteBuffer<byte[]> {
35+
36+
private final byte[] underlyingBytes;
37+
private int readerIndex;
38+
private int writerIndex;
39+
40+
/**
41+
* Construct a new {@link ByteArrayByteBuffer} for the given bytes.
42+
*
43+
* @param underlyingBytes the bytes to wrap
44+
*/
45+
ByteArrayByteBuffer(byte[] underlyingBytes) {
46+
this(underlyingBytes, underlyingBytes.length);
47+
}
48+
49+
/**
50+
* Construct a new {@link ByteArrayByteBuffer} for the given bytes and capacity.
51+
* If capacity is greater than the length of the underlyingBytes, extra bytes will be zeroed out.
52+
* If capacity is less than the length of the underlyingBytes, the underlyingBytes will be truncated.
53+
*
54+
* @param underlyingBytes the bytes to wrap
55+
* @param capacity the capacity of the buffer
56+
*/
57+
ByteArrayByteBuffer(byte[] underlyingBytes, int capacity) {
58+
if (capacity < underlyingBytes.length) {
59+
this.underlyingBytes = Arrays.copyOf(underlyingBytes, capacity);
60+
} else if (capacity > underlyingBytes.length) {
61+
this.underlyingBytes = new byte[capacity];
62+
System.arraycopy(underlyingBytes, 0, this.underlyingBytes, 0, underlyingBytes.length);
63+
} else {
64+
this.underlyingBytes = underlyingBytes;
65+
}
66+
}
67+
68+
@Override
69+
public byte[] asNativeBuffer() {
70+
return underlyingBytes;
71+
}
72+
73+
@Override
74+
public int readableBytes() {
75+
return underlyingBytes.length - readerIndex;
76+
}
77+
78+
@Override
79+
public int writableBytes() {
80+
return underlyingBytes.length - writerIndex;
81+
}
82+
83+
@Override
84+
public int maxCapacity() {
85+
return underlyingBytes.length;
86+
}
87+
88+
@Override
89+
public ByteArrayByteBuffer capacity(int capacity) {
90+
return new ByteArrayByteBuffer(underlyingBytes, capacity);
91+
}
92+
93+
@Override
94+
public int readerIndex() {
95+
return readerIndex;
96+
}
97+
98+
@Override
99+
public ByteArrayByteBuffer readerIndex(int readPosition) {
100+
this.readerIndex = Math.min(readPosition, underlyingBytes.length);
101+
return this;
102+
}
103+
104+
@Override
105+
public int writerIndex() {
106+
return writerIndex;
107+
}
108+
109+
@Override
110+
public ByteArrayByteBuffer writerIndex(int position) {
111+
this.writerIndex = Math.min(position, underlyingBytes.length);
112+
return this;
113+
}
114+
115+
@Override
116+
public byte read() {
117+
return underlyingBytes[readerIndex++];
118+
}
119+
120+
@Override
121+
public CharSequence readCharSequence(int length, Charset charset) {
122+
String s = new String(underlyingBytes, readerIndex, length, charset);
123+
readerIndex += length;
124+
return s;
125+
}
126+
127+
@Override
128+
public ByteArrayByteBuffer read(byte[] destination) {
129+
int count = Math.min(readableBytes(), destination.length);
130+
System.arraycopy(underlyingBytes, readerIndex, destination, 0, count);
131+
readerIndex += count;
132+
return this;
133+
}
134+
135+
@Override
136+
public ByteArrayByteBuffer read(byte[] destination, int offset, int length) {
137+
int count = Math.min(readableBytes(), Math.min(destination.length - offset, length));
138+
System.arraycopy(underlyingBytes, readerIndex, destination, offset, count);
139+
readerIndex += count;
140+
return this;
141+
}
142+
143+
@Override
144+
public ByteArrayByteBuffer write(byte b) {
145+
underlyingBytes[writerIndex++] = b;
146+
return this;
147+
}
148+
149+
@Override
150+
public ByteArrayByteBuffer write(byte[] source) {
151+
int count = Math.min(writableBytes(), source.length);
152+
System.arraycopy(source, 0, underlyingBytes, writerIndex, count);
153+
writerIndex += count;
154+
return this;
155+
}
156+
157+
@Override
158+
public ByteArrayByteBuffer write(CharSequence source, Charset charset) {
159+
write(source.toString().getBytes(charset));
160+
return this;
161+
}
162+
163+
@Override
164+
public ByteArrayByteBuffer write(byte[] source, int offset, int length) {
165+
int count = Math.min(writableBytes(), length);
166+
System.arraycopy(source, offset, underlyingBytes, writerIndex, count);
167+
writerIndex += count;
168+
return this;
169+
}
170+
171+
@Override
172+
public ByteArrayByteBuffer write(ByteBuffer... buffers) {
173+
for (ByteBuffer<?> buffer : buffers) {
174+
write(buffer.toByteArray());
175+
}
176+
return this;
177+
}
178+
179+
@Override
180+
public ByteArrayByteBuffer write(java.nio.ByteBuffer... buffers) {
181+
for (java.nio.ByteBuffer buffer : buffers) {
182+
write(buffer.array());
183+
}
184+
return this;
185+
}
186+
187+
@Override
188+
public ByteArrayByteBuffer slice(int index, int length) {
189+
return new ByteArrayByteBuffer(Arrays.copyOfRange(underlyingBytes, index, index + length), length);
190+
}
191+
192+
@Override
193+
public java.nio.ByteBuffer asNioBuffer() {
194+
return java.nio.ByteBuffer.wrap(underlyingBytes, readerIndex, readableBytes());
195+
}
196+
197+
@Override
198+
public java.nio.ByteBuffer asNioBuffer(int index, int length) {
199+
return java.nio.ByteBuffer.wrap(underlyingBytes, index, length);
200+
}
201+
202+
@Override
203+
public InputStream toInputStream() {
204+
return new ByteArrayInputStream(underlyingBytes, readerIndex, readableBytes());
205+
}
206+
207+
@Override
208+
public OutputStream toOutputStream() {
209+
throw new IllegalStateException("Not implemented");
210+
}
211+
212+
@Override
213+
public byte[] toByteArray() {
214+
return Arrays.copyOfRange(underlyingBytes, readerIndex, readableBytes());
215+
}
216+
217+
@Override
218+
public String toString(Charset charset) {
219+
return new String(underlyingBytes, readerIndex, readableBytes(), charset);
220+
}
221+
222+
@Override
223+
public int indexOf(byte b) {
224+
for (int i = readerIndex; i < underlyingBytes.length; i++) {
225+
if (underlyingBytes[i] == b) {
226+
return i;
227+
}
228+
}
229+
return -1;
230+
}
231+
232+
@Override
233+
public byte getByte(int index) {
234+
return underlyingBytes[index];
235+
}
236+
}

http-client-core/src/main/java/io/micronaut/http/client/AbstractHttpClientFactory.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.micronaut.core.annotation.NonNull;
2020
import io.micronaut.core.annotation.Nullable;
2121
import io.micronaut.core.convert.ConversionService;
22+
import io.micronaut.http.body.MessageBodyHandlerRegistry;
2223
import io.micronaut.http.codec.MediaTypeCodecRegistry;
2324

2425
import java.net.URI;
@@ -36,13 +37,14 @@
3637
public abstract class AbstractHttpClientFactory<T extends HttpClient> implements HttpClientFactory {
3738

3839
protected final MediaTypeCodecRegistry mediaTypeCodecRegistry;
40+
protected final MessageBodyHandlerRegistry messageBodyHandlerRegistry;
3941
protected final ConversionService conversionService;
4042

41-
protected AbstractHttpClientFactory(
42-
@Nullable MediaTypeCodecRegistry mediaTypeCodecRegistry,
43-
ConversionService conversionService
44-
) {
43+
protected AbstractHttpClientFactory(@Nullable MediaTypeCodecRegistry mediaTypeCodecRegistry,
44+
MessageBodyHandlerRegistry messageBodyHandlerRegistry,
45+
ConversionService conversionService) {
4546
this.mediaTypeCodecRegistry = mediaTypeCodecRegistry;
47+
this.messageBodyHandlerRegistry = messageBodyHandlerRegistry;
4648
this.conversionService = conversionService;
4749
}
4850

0 commit comments

Comments
 (0)