OSDN Git Service

フルサイズは途中。
authorMRSa <mrsa@myad.jp>
Tue, 8 Oct 2019 14:42:04 +0000 (23:42 +0900)
committerMRSa <mrsa@myad.jp>
Tue, 8 Oct 2019 14:42:04 +0000 (23:42 +0900)
app/src/main/java/net/osdn/gokigen/pkremote/camera/vendor/ptpip/wrapper/playback/PtpIpFullImageReceiver.java

index 995f4b9..ba56125 100644 (file)
@@ -7,10 +7,14 @@ import androidx.annotation.NonNull;
 
 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IDownloadContentCallback;
 import net.osdn.gokigen.pkremote.camera.interfaces.playback.IProgressEvent;
+import net.osdn.gokigen.pkremote.camera.utils.SimpleLogDumper;
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandCallback;
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.IPtpIpCommandPublisher;
 import net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command.messages.PtpIpCommandGeneric;
 
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+
 public class PtpIpFullImageReceiver implements IPtpIpCommandCallback
 {
     private static final String TAG = PtpIpFullImageReceiver.class.getSimpleName();
@@ -20,6 +24,9 @@ public class PtpIpFullImageReceiver implements IPtpIpCommandCallback
     private IDownloadContentCallback callback = null;
     private int objectId = 0;
 
+    private int received_total_bytes = 0;
+    private int received_remain_bytes = 0;
+
     PtpIpFullImageReceiver(@NonNull Activity activity, @NonNull IPtpIpCommandPublisher publisher)
     {
         this.activity = activity;
@@ -77,9 +84,10 @@ public class PtpIpFullImageReceiver implements IPtpIpCommandCallback
     @Override
     public void onReceiveProgress(final int currentBytes, final int totalBytes, byte[] rx_body)
     {
-        int length = (rx_body == null) ? 0 : rx_body.length;
+        byte[] body = cutHeader(rx_body);
+        int length = (body == null) ? 0 : body.length;
         Log.v(TAG, " onReceiveProgress() " + currentBytes + "/" + totalBytes + " (" + length + " bytes.)");
-        callback.onProgress(rx_body, length, new IProgressEvent() {
+        callback.onProgress(body, length, new IProgressEvent() {
             @Override
             public float getProgress() {
                 return ((float) currentBytes / (float) totalBytes);
@@ -97,6 +105,87 @@ public class PtpIpFullImageReceiver implements IPtpIpCommandCallback
         });
     }
 
+    private byte[] cutHeader(byte[] rx_body)
+    {
+        if (rx_body == null)
+        {
+            return (null);
+        }
+        int length = rx_body.length;
+        int data_position = 0;
+        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+        if (received_total_bytes == 0)
+        {
+            // データを最初に読んだとき。ヘッダ部分を読み飛ばす
+            data_position = (int) rx_body[0] & (0xff);
+        }
+        else if (received_remain_bytes > 0)
+        {
+
+            Log.v(TAG, "  >>> [ remain_bytes : " + received_remain_bytes + "] ( length : " + length + ") " + data_position);
+            SimpleLogDumper.dump_bytes("[zzz]", Arrays.copyOfRange(rx_body, data_position, (data_position + 160)));
+
+            // データの読み込みが途中だった場合...
+            if (length < received_remain_bytes)
+            {
+                // 全部コピーする、足りないバイト数は残す
+                received_remain_bytes = received_remain_bytes - length;
+                received_total_bytes = received_total_bytes + rx_body.length;
+                return (rx_body);
+            }
+            else
+            {
+                byteStream.write(rx_body, data_position, received_remain_bytes);
+                data_position = received_remain_bytes;
+                received_remain_bytes = 0;
+            }
+        }
+
+        while (data_position <= (length - 12))
+        {
+            int body_size =  (rx_body[data_position] & 0xff) + ((rx_body[data_position + 1]  & 0xff) << 8) +
+                    ((rx_body[data_position + 2] & 0xff) << 16) + ((rx_body[data_position + 3] & 0xff) << 24);
+            if (body_size <= 12)
+            {
+                Log.v(TAG, "  BODY SIZE IS SMALL : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] " + rx_body.length + "  ");
+
+                int startpos = (data_position > 48) ? (data_position - 48) : 0;
+                SimpleLogDumper.dump_bytes("[xxx]", Arrays.copyOfRange(rx_body, startpos, (data_position + 48)));
+
+                break;
+            }
+
+            Log.v(TAG, " RX DATA : " + data_position + " (" + body_size + ") [" + received_remain_bytes + "] (" + received_total_bytes + ")");
+            SimpleLogDumper.dump_bytes("[yyy] " + data_position + ": ", Arrays.copyOfRange(rx_body, data_position, (data_position + 64)));
+
+
+            if ((data_position + body_size) > length)
+            {
+                // データがすべてバッファ内になかったときは、バッファすべてコピーして残ったサイズを記憶しておく。
+                int copysize = (length - ((data_position + 12)));
+                byteStream.write(rx_body, (data_position + 12), copysize);
+                received_remain_bytes = body_size - copysize - 12;  // マイナス12は、ヘッダ分
+                received_total_bytes = received_total_bytes + copysize;
+                Log.v(TAG, " --- copy : " + (data_position + 12) + " " + copysize + " remain : " + received_remain_bytes + "  body size : " + body_size);
+                break;
+            }
+            try
+            {
+                byteStream.write(rx_body, (data_position + 12), (body_size - 12));
+                data_position = data_position + body_size;
+                received_total_bytes = received_total_bytes + 12;
+                Log.v(TAG, " --- COPY : " + (data_position + 12) + " " + (body_size - 12) + " remain : " + received_remain_bytes);
+
+            }
+            catch (Exception e)
+            {
+                Log.v(TAG, "  pos : " + data_position + "  size : " + body_size + " length : " + length);
+                e.printStackTrace();
+            }
+        }
+        return (byteStream.toByteArray());
+    }
+
     @Override
     public boolean isReceiveMulti()
     {