- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
发送文件时,可以做ctx.writeAndFlush(new ChunkedFile(new File("file.png")));
.
List<Object>
怎么样? ?
列表包含String
和 bytes of image
.
文档中有 ChunkedInput()
但我无法使用它。
更新
假设在我的处理程序中,在 channelRead0(ChannelHandlerContext ctx, Object o)
中我要发送 List<Object>
的方法我做了以下事情
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object o) throws Exception {
List<Object> msg = new ArrayList<>();
/**getting the bytes of image**/
byte[] imageInByte;
BufferedImage originalImage = ImageIO.read(new File(fileName));
// convert BufferedImage to byte array
ByteArrayOutputStream bAoS = new ByteArrayOutputStream();
ImageIO.write(originalImage, "png", bAoS);
bAoS.flush();
imageInByte = baos.toByteArray();
baos.close();
msg.clear();
msg.add(0, "String"); //add the String into List
msg.add(1, imageInByte); //add the bytes of images into list
/**Chunk the List<Object> and Send it just like the chunked file**/
ctx.writeAndFlush(new ChunkedInput(DONT_KNOW_WHAT_TO_DO_HERE)); //
}
最佳答案
只需实现您自己的 ChunkedInput<ByteBuf>
.按照 Netty 附带的实现,您可以按如下方式实现它:
public class ChunkedList implements ChunkedInput<ByteBuf> {
private static final byte[] EMPTY = new byte[0];
private byte[] previousPart = EMPTY;
private final int chunkSize;
private final Iterator<Object> iterator;
public ChunkedList(int chunkSize, List<Object> objs) {
//chunk size in bytes
this.chunkSize = chunkSize;
this.iterator = objs.iterator();
}
public ByteBuf readChunk(ChannelHandlerContext ctx) {
return readChunk(ctx.alloc());
}
public ByteBuf readChunk(ByteBufAllocator allocator) {
if (isEndOfInput())
return null;
else {
ByteBuf buf = allocator.buffer(chunkSize);
boolean release = true;
try {
int bytesRead = 0;
if (previousPart.length > 0) {
if (previousPart.length > chunkSize) {
throw new IllegalStateException();
}
bytesRead += previousPart.length;
buf.writeBytes(previousPart);
}
boolean done = false;
while (!done) {
if (!iterator.hasNext()) {
done = true;
previousPart = EMPTY;
} else {
Object o = iterator.next();
//depending on the encoding
byte[] bytes = o instanceof String ? ((String) o).getBytes() : (byte[]) o;
bytesRead += bytes.length;
if (bytesRead > chunkSize) {
done = true;
previousPart = bytes;
} else {
buf.writeBytes(bytes);
}
}
}
release = false;
} finally {
if (release)
buf.release();
}
return buf;
}
}
public long length() {
return -1;
}
public boolean isEndOfInput() {
return !iterator.hasNext() && previousPart.length == 0;
}
public long progress() {
return 0;
}
public void close(){
//close
}
}
为了写ChunkedContent
Netty
附带一个特殊处理程序.参见 io.netty.handler.stream.ChunkedWriteHandler
.所以只需添加到您的下游。这是文档中的引述:
A
ChannelHandler
that adds support for writing a large data stream asynchronously neither spending a lot of memory nor gettingOutOfMemoryError
. Large data streaming such as file transfer requires complicated state management in aChannelHandler
implementation.ChunkedWriteHandler
manages such complicated states so that you can send a large data stream without difficulties.
关于java - 如何在 Netty 中分块 List<Object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47794599/
我正在使用 WCF 并希望将大文件从客户端上传到服务器。我已经调查并决定遵循 http://msdn.microsoft.com/en-us/library/aa717050.aspx 中概述的分块方
我试图了解有关 Transfer-Encoding:chunked 的更多信息。引用了一些文章: http://zoompf.com/blog/2012/05/too-chunky和 "Transfe
我们正在评估 HDF5 在分块数据集方面的性能。 特别是我们试图弄清楚是否可以跨不同的连续块进行读取以及这样做会如何影响性能? 例如。我们有一个块大小为 10 的数据集,一个有 100 个值的数据集,
使用 Eloquent,如何根据 chunk 中的条件终止分块函数的关闭?我试过返回,但这似乎只终止当前块而不是所有块。此时,我想停止从数据库中检索记录。 $query->chunk(self::CH
有没有办法在不删除所选文件的情况下重新启动 plupload 上传? plupload.stop() 停止上传,但如果我使用 start() 再次启动上传,它会从上次停止的地方继续。相反,我希望它再次
我有带有“id,名称”的文件1和带有“id,地址”的文件2。我无法加载第一个文件(小于 2Gb):它在 76k 行(带有 block 连接)和只有 2 列后崩溃...我也无法在第二个文件上 read_
我正在尝试从头开始设计一个系统,我想在其中通过 servlet 加载文本行。生产线的生产需要一些时间。因此,我希望能够在它们到达时在我的浏览器中逐步显示它们,一次显示几个。我想从 javascript
能否请您提供一个示例,说明如何在 Android 中读取来自 Web 服务的分块响应 谢谢 编辑:我尝试调用一个 soap 网络服务,它用代表图像的 base64 编码字符串回复我 代码如下: Str
我想制作一个无限平铺 map ,从(-max_int,-max_int)到(max_int,max_int),所以我要制作一个基本结构: chunk,每个 chunk 包含 char tiles[w]
这是一个典型的场景:评估一个页面,并且有一个缓冲区 - 一旦缓冲区已满,评估的页面部分就会发送到浏览器。这使用 HTTP 1.1 分块编码。 但是,其中一个 block 中可能会发生错误(在第一个 b
如何从给定模式的句子中获取所有 block 。例子 NP:{} 标记的句子: [("money", "NN"), ("market", "NN") ("fund", "NN")] 如果我解析我得到 (
我正在使用以下代码将 CSV 文件拆分为多个 block (来自 here) def worker(chunk): print len(chunk) def keyfunc(row):
我想我已经很接近这个了,我有以下 dropzone 配置: Dropzone.options.myDZ = { chunking: true, chunkSize: 500000, ret
因为我在更常规的基础上使用 WebSocket 连接,所以我对事情在幕后的工作方式很感兴趣。因此,我研究了无休止的规范文档一段时间,但到目前为止,我真的找不到任何关于对传输流本身进行分 block 。
我有一个 slice ,其中包含约 210 万个日志字符串,我想创建一个 slice ,字符串尽可能均匀分布。 这是我目前所拥有的: // logs is a slice with ~2.1 mill
问题: 我有一个大约为 [350000, 1] 的向量,我希望计算成对距离。这导致 [350000, 350000] 整数数据类型的矩阵不适合 RAM。我最终想得到一个 bool 值(适合 RAM),
我想将 JSONP 用于具有 x 域脚本编写的项目,但不太关心 IE 中的 2048 个字符限制。 如果字符大小超过 2048,JSONP 是否自动支持“分块”?如果是的话,有人可以分享一些例子吗?
我目前正在开发 2d 角色扮演游戏,例如《最终幻想 1-4》。基本上,我的平铺 map 可以加载, Sprite 可以在 map 上自由行走。 如何处理与平铺 map 的碰撞? 我创建了三个独立的图
Treetagger 可以进行词性标记和文本分块,这意味着提取口头和名词性从句,如这个德语示例所示: $ echo 'Das ist ein Test.' | cmd/tagger-chunker-g
我应该从服务器流式传输端点,该端点返回带有传输编码的 json:分块。 我有以下代码,但无法读取响应。我尝试了 responseBody.streamBytes() 并将输入流转换为字符串,但我不能在
我是一名优秀的程序员,十分优秀!