В Нетти 3 мы исполнение LITTLE_ENDIAN ChannelBuffers на каждом конце с помощьюУстановка ByteBuf порядка байтов в Netty4
bootstrap.setOption("child.bufferFactory", new HeapChannelBufferFactory(ByteOrder.LITTLE_ENDIAN));
но в Нетти 4, конфигурация ByteBuf теперь, кажется, через ChannelOption.ALLOCATOR:
bootstrap.option(ChannelOption.ALLOCATOR, someAllocator);
Все, что мы действительно хотим сделать, это украсить UnpooledByteBufAllocator, но он окончательный, и методы , которые нам нужно украсить, защищены, поэтому мы не можем расширить класс или делегировать его. Мы пришлось прибегнуть к прокси подход:
private static class AllocatorProxyHandler implements InvocationHandler {
private final ByteBufAllocator allocator;
public AllocatorProxyHandler(ByteBufAllocator allocator) {
this.allocator = allocator;
}
public static ByteBufAllocator proxy(ByteBufAllocator allocator) {
return (ByteBufAllocator) Proxy.newProxyInstance(AllocatorProxyHandler.class.getClassLoader(), new Class[]{ByteBufAllocator.class}, new AllocatorProxyHandler(allocator));
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(allocator, args);
if (result instanceof ByteBuf) {
return ((ByteBuf) result).order(ByteOrder.LITTLE_ENDIAN);
} else {
return result;
}
}
}
установки опции Bootstrap так:
bootstrap.option(ChannelOption.ALLOCATOR, AllocatorProxyHandler.proxy(UnpooledByteBufAllocator.DEFAULT));
Есть ли какой-то другой (лучше) способ сделать это нам не хватает?
Возможно, полезны функции доступа, такие как getIntLE или getIntBE? – Climax
@Climax Да, это план на самом деле. – trustin
это отличная новость – Climax