-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added maxCapacity for downloaded strings. Introduced facility to obtain the response content as ByteBuffer. Introduced ReactiveResponse.Result to carry both the response and the response content. Signed-off-by: Simone Bordet <[email protected]>
- Loading branch information
Showing
9 changed files
with
400 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/org/eclipse/jetty/reactive/client/internal/AbstractBufferingProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.eclipse.jetty.reactive.client.internal; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.eclipse.jetty.io.Content; | ||
import org.eclipse.jetty.reactive.client.ReactiveResponse; | ||
|
||
/** | ||
* <p>A {@link org.reactivestreams.Processor} that buffers {@link Content.Chunk}s | ||
* up to a max capacity.</p> | ||
* <p>When the {@code complete} event is received from upstream, {@link #process(List)} | ||
* is invoked to processes the chunks and produce a single item of type {@code T}, | ||
* that is then published to downstream.</p> | ||
* | ||
* @param <T> the type of the item resulted from processing the chunks | ||
*/ | ||
public abstract class AbstractBufferingProcessor<T> extends AbstractSingleProcessor<Content.Chunk, T> { | ||
public static final int DEFAULT_MAX_CAPACITY = 2 * 1024 * 1024; | ||
|
||
private final List<Content.Chunk> chunks = new ArrayList<>(); | ||
private final ReactiveResponse response; | ||
private final int maxCapacity; | ||
private int capacity; | ||
|
||
public AbstractBufferingProcessor(ReactiveResponse response, int maxCapacity) { | ||
this.response = response; | ||
this.maxCapacity = maxCapacity; | ||
} | ||
|
||
public ReactiveResponse getResponse() { | ||
return response; | ||
} | ||
|
||
@Override | ||
public void onNext(Content.Chunk chunk) { | ||
capacity += chunk.remaining(); | ||
if ((maxCapacity > 0 && capacity > maxCapacity) || capacity < 0) { | ||
upStreamCancel(); | ||
onError(new IllegalStateException("buffering capacity %d exceeded".formatted(maxCapacity))); | ||
return; | ||
} | ||
chunks.add(chunk); | ||
upStreamRequest(1); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable throwable) { | ||
chunks.forEach(Content.Chunk::release); | ||
super.onError(throwable); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
T result = process(chunks); | ||
downStreamOnNext(result); | ||
super.onComplete(); | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
chunks.forEach(Content.Chunk::release); | ||
super.cancel(); | ||
} | ||
|
||
protected abstract T process(List<Content.Chunk> chunks); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/org/eclipse/jetty/reactive/client/internal/ByteBufferBufferingProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.eclipse.jetty.reactive.client.internal; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
import org.eclipse.jetty.io.Content; | ||
import org.eclipse.jetty.reactive.client.ReactiveResponse; | ||
|
||
public class ByteBufferBufferingProcessor extends AbstractBufferingProcessor<ByteBuffer> { | ||
public ByteBufferBufferingProcessor(ReactiveResponse response, int maxCapacity) { | ||
super(response, maxCapacity); | ||
} | ||
|
||
@Override | ||
protected ByteBuffer process(List<Content.Chunk> chunks) { | ||
int length = chunks.stream().mapToInt(Content.Chunk::remaining).sum(); | ||
ByteBuffer result = ByteBuffer.allocateDirect(length); | ||
for (Content.Chunk chunk : chunks) { | ||
result.put(chunk.getByteBuffer()); | ||
chunk.release(); | ||
} | ||
return result.flip(); | ||
} | ||
} |
Oops, something went wrong.