admin管理员组文章数量:1278920
i have a question about using reusing ByteBuffer
s from java.nio multiple times to produce InputStreams
. i've found a nice converter in jackson called ByteBufferBackedInputStream
. my problem however is that i want to potentially have multiple InputStream
s open at the same time, backed by the same underlying ByteBuffer
. consider,
@Test
void testStuff() throws Exception {
ByteBuffer bb = ByteBuffer.allocateDirect(42);
for (int i = 0; i < 42; i++) {
bb.put((byte) i);
}
bb.flip();
InputStream foo = new ByteBufferBackedInputStream(bb);
InputStream bar = new ByteBufferBackedInputStream(bb);
byte[] eightyFourBytes = new byte[84];
int readFromFoo = foo.read(eightyFourBytes);
int readFromBar = bar.read(eightyFourBytes);
assertThat(readFromFoo).isEqualTo(42);
assertThat(readFromBar).isEqualTo(42); // oops! reading mutated the underlying ByteBuffer's position
}
i'm scratching my head to work out a solution to this. i need this because,
- i am using and pooling direct
ByteBuffers
for better file io performance (that using it is fairly drop-in for file io is important as well), and to reduce GC pressure, - i am in other places calling into an interface which takes an
InputStream
, and is inflexible, - my application has had issues with memory usage so i don't want to recopy the buffer
i guess my question is, is there an alternative to ByteBuffer
, or a better way of using it than i am? obviously i could just manually reset the buffer between usages, but doing that seems brittle and in my mind the ideal solution can produce InputStream
s that i can hand off without thinking about it.
i have a question about using reusing ByteBuffer
s from java.nio multiple times to produce InputStreams
. i've found a nice converter in jackson called ByteBufferBackedInputStream
. my problem however is that i want to potentially have multiple InputStream
s open at the same time, backed by the same underlying ByteBuffer
. consider,
@Test
void testStuff() throws Exception {
ByteBuffer bb = ByteBuffer.allocateDirect(42);
for (int i = 0; i < 42; i++) {
bb.put((byte) i);
}
bb.flip();
InputStream foo = new ByteBufferBackedInputStream(bb);
InputStream bar = new ByteBufferBackedInputStream(bb);
byte[] eightyFourBytes = new byte[84];
int readFromFoo = foo.read(eightyFourBytes);
int readFromBar = bar.read(eightyFourBytes);
assertThat(readFromFoo).isEqualTo(42);
assertThat(readFromBar).isEqualTo(42); // oops! reading mutated the underlying ByteBuffer's position
}
i'm scratching my head to work out a solution to this. i need this because,
- i am using and pooling direct
ByteBuffers
for better file io performance (that using it is fairly drop-in for file io is important as well), and to reduce GC pressure, - i am in other places calling into an interface which takes an
InputStream
, and is inflexible, - my application has had issues with memory usage so i don't want to recopy the buffer
i guess my question is, is there an alternative to ByteBuffer
, or a better way of using it than i am? obviously i could just manually reset the buffer between usages, but doing that seems brittle and in my mind the ideal solution can produce InputStream
s that i can hand off without thinking about it.
1 Answer
Reset to default 2The simple approach is to hand out a read only copy:
InputStream foo = new ByteBufferBackedInputStream(bb.asReadOnlyBuffer());
The buffer will be shared, but each copy will get its own position, limit and mark. Note that changes to the data in the buffer will be seen by all clients, so you need to be really sure that nobody is using your buffer before you return it to the pool again! Depending on your use case, that may be easy or very hard. If it is hard, pooling might not be such a good idea.
本文标签: javareuse ByteBuffers across multiple ByteBufferBackedInputStreamsStack Overflow
版权声明:本文标题:java - reuse ByteBuffers across multiple ByteBufferBackedInputStreams - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741234214a2362682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论