netty-incubator-buffer-api/src/test/java/io/netty/buffer/api/Fixture.java
Chris Vest d382017dc6 Add support for iterating underlying buffer components
Motivation:
It's desirable to be able to access the contents of a Buf via an array or a ByteBuffer.
However, we would also like to have a unified API that works for both composite and non-composite buffers.
Even for nested composite buffers.

Modification:
Add a forEachReadable method, which uses internal iteration to process all buffer components.
The internal iteration allows us to hide any nesting of composite buffers.
The consumer in the internal iteration is presented with a Component object, which exposes the contents in various ways.
The data is exposed from the Component via methods, such that anything that is expensive to create, will not have to be paid for unless it is used.
This mechanism also let us avoid any allocation unnecessary allocation; the ByteBuffers and arrays will necessarily have to be allocated, but the consumer may or may not need allocation depending on how it's implemented, and the component objects do not need to be allocated, because the non-composite buffers can directly implement the Component interface.

Result:
It's now possible to access the contents of Buf instances as arrays or ByteBuffers, without having to copy the data.
2021-01-11 16:31:36 +01:00

84 lines
2.1 KiB
Java

/*
* Copyright 2020 The Netty Project
*
* The Netty Project licenses this file to you 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:
*
* https://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 io.netty.buffer.api;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.function.Supplier;
public final class Fixture implements Supplier<Allocator> {
private final String name;
private final Supplier<Allocator> factory;
private final EnumSet<Properties> properties;
public Fixture(String name, Supplier<Allocator> factory, Properties... props) {
this.name = name;
this.factory = factory;
properties = EnumSet.copyOf(Arrays.asList(props));
}
public Allocator createAllocator() {
return factory.get();
}
@Override
public Allocator get() {
return factory.get();
}
@Override
public String toString() {
return name;
}
public Properties[] getProperties() {
return properties.toArray(Properties[]::new);
}
public boolean isHeap() {
return properties.contains(Properties.HEAP);
}
public boolean isDirect() {
return properties.contains(Properties.DIRECT);
}
public boolean isComposite() {
return properties.contains(Properties.COMPOSITE);
}
public boolean isPooled() {
return properties.contains(Properties.POOLED);
}
public boolean isCleaner() {
return properties.contains(Properties.CLEANER);
}
public boolean isSlice() {
return properties.contains(Properties.SLICE);
}
public enum Properties {
HEAP,
DIRECT,
COMPOSITE,
CLEANER,
POOLED,
SLICE
}
}