package org.helioviewer.viewmodel.view.jp2view.io.jpip;

import java.io.IOException;
import java.util.LinkedList;

import org.helioviewer.viewmodel.view.jp2view.io.http.HTTPResponse;

/**
 * A response to a JPIPRequest. Encapsulates both the HTTPResponse headers and
 * the JPIPDataSegments.
 * 
 * @author caplins
 * 
 */
public class JPIPResponse extends HTTPResponse {

    /** The status... could be EOR_WINDOW_DONE or EOR_IMAGE_DONE */
    private long status;

    /** A list of the data segments. */
    private LinkedList<JPIPDataSegment> jpipDataList;

    /**
     * Used to form responses.
     * 
     * @param res
     * @throws IOException
     */
    public JPIPResponse(HTTPResponse res) throws IOException {
        super(res.getCode(), res.getReason());

        for (String key : res.getHeaders())
            this.setHeader(key, res.getHeader(key));

        status = -1;
        jpipDataList = new LinkedList<JPIPDataSegment>();
    }

    /**
     * Adds the data segment to this object.
     * 
     * @param data
     */
    public void addJpipDataSegment(JPIPDataSegment data) {
        if (data.isEOR) {
            status = data.binID;
        }
        jpipDataList.add(data);
    }

    /**
     * Removes a data segment from this object.
     * 
     * @return The removed data segment, null if the list was empty
     */
    public JPIPDataSegment removeJpipDataSegment() {
        return (jpipDataList.isEmpty() ? null : jpipDataList.remove());
    }

    /**
     * Determines the response size.
     * 
     * @return Response size
     */
    public long getResponseSize() {
        long size = 0;
        for (int i = 0; i < jpipDataList.size(); i++)
            size += jpipDataList.get(i).length;
        return size;
    }

    /**
     * Tells if the response completes the last request.
     * 
     * @return True, if the response is complete
     */
    public boolean isResponseComplete() {
        return (status == JPIPConstants.EOR_WINDOW_DONE || status == JPIPConstants.EOR_IMAGE_DONE);
    }

}

@@ -287,18 +287,17 @@
 ents.%0A *
- 
 %0A
+
  * @auth
@@ -309,17 +309,16 @@
 plins%0A *
- 
 %0A */%0Apub
@@ -510,16 +510,22 @@
 private 
+final 
 LinkedLi
@@ -557,16 +557,43 @@
 ataList;
+%0A    private long size = 0;
 %0A%0A    /*
@@ -623,33 +623,32 @@
 esponses.%0A     *
- 
 %0A     * @param r
@@ -1031,33 +1031,32 @@
 s object.%0A     *
- 
 %0A     * @param d
@@ -1227,16 +1227,45 @@
 (data);%0A
+        size += data.length;%0A
     %7D%0A%0A 
@@ -1317,33 +1317,32 @@
 s object.%0A     *
- 
 %0A     * @return 
@@ -1463,24 +1463,21 @@
         
-return
+if
  (
+!
 jpipData
@@ -1494,40 +1494,154 @@
 ty()
- ? null : jpipDataList.remove())
+) %7B%0A            JPIPDataSegment jpr = jpipDataList.remove();%0A            size -= jpr.length;%0A            return jpr;%0A        %7D%0A        return null
 ;%0A  
@@ -1688,33 +1688,32 @@
 nse size.%0A     *
- 
 %0A     * @return 
@@ -1774,133 +1774,8 @@
 ) %7B%0A
-        long size = 0;%0A        for (int i = 0; i %3C jpipDataList.size(); i++)%0A            size += jpipDataList.get(i).length;%0A
     
@@ -1863,23 +1863,22 @@
 equest.%0A
+
      *
- 
 %0A     * 

Remove latency of the client: don't loop linked list (thank you java...)