4

When sending file, you can do ctx.writeAndFlush(new ChunkedFile(new File("file.png")));.

how about a List<Object>?

The list contains String and bytes of image.

from the documentation there's ChunkedInput() but I'm not able to get the use of it.

UPDATE

let's say in my Handler, inside channelRead0(ChannelHandlerContext ctx, Object o) method where I want to send the List<Object> I've done the following

@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)); //

}

Dynamically add Button id based on the button click show div in jquery

I had one for-loop in that for loop one div data and summit button.

Code i tried :

 for(i=0; i<response.data.length; i++){
           var job = '<div class="job-headers">';
           job += '<h3>'+response.data[i].job_title+'</h3>';
           job += '<h5>'+response.data[i].location+'</h5>';
           job += '<h5>'+response.data[i].description+'</h5>';
           job += '<h5> Created on :'+response.data[i].created_at+'</h5>';
           job += '<h5> Status :'+response.data[i].job_status+'</h5>';
     **show****job += '<div><h2>Title: ' + response.data[i].job_title + <h2>;
          job  += '<h2>Title: ' + response.data[i].job_role + <h2>';    
          $job+= '</div>'
         ****** job += '<input type="submit" class="submit-button" value="submit">';
           $(".submit-button").click(function() {
                     $(this).prev('div').toggle();
                  });

                  job += '</div>';
                   $('#job-preview').append(job);
                 }
             });
        });

first button only show the first div second button not working? any ides???

4

1 回答 1

4

只需实现您自己的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。所以只需添加到您的下游。这是文档中的引用:

增加了对异步写入大型数据流的ChannelHandler支持,既不消耗大量内存也不获取 OutOfMemoryError. 诸如文件传输之类的大数据流需要在ChannelHandler实现中进行复杂的状态管理。 ChunkedWriteHandler管理如此复杂的状态,以便您可以毫无困难地发送大数据流。

于 2017-12-13T16:23:26.513 回答