Skip to content

Commit d4056fb

Browse files
committed
fix bug in transactUnsync(), split read request into smaller then inMaxPS.
Otherwise the getObject() will not work .
1 parent da0beb9 commit d4056fb

File tree

1 file changed

+33
-60
lines changed

1 file changed

+33
-60
lines changed

USBCamera/src/com/ptplib/usbcamera/BaselineInitiator.java

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//
1717
package com.ptplib.usbcamera;
1818

19+
import java.io.ByteArrayOutputStream;
1920
import java.nio.ByteBuffer;
2021

2122
import com.ptplib.usbcamera.eos.EosEventConstants;
@@ -708,66 +709,38 @@ public Response transactUnsync(Command command, Data data)
708709

709710
// read data?
710711
} else {
711-
// Log.d(TAG, "Start Read Data");
712-
byte buf1[] = new byte[inMaxPS];
713-
int len = 0;
714-
//len = device.getInputStream(epIn).read(buf1);
715-
// Log.d(TAG, "Receive data, max " +inMaxPS);
716-
len = mConnection.bulkTransfer(epIn, buf1, inMaxPS , DEFAULT_TIMEOUT);
717-
// Log.d(TAG, "received data bytes: " +len);
718-
719-
// Get the first bulk packet(s), check header for length
720-
data.data = buf1;
721-
data.length = len;
722-
if (TRACE) {
723-
System.err.println(data.toString());
724-
}
725-
if (!"data".equals(data.getBlockTypeName(data.getBlockType()))
726-
|| data.getCode() != command.getCode()
727-
|| data.getXID() != command.getXID()) {
728-
throw new PTPException("protocol err 1, " + data);
729-
}
730-
731-
// get the rest of it
732-
int expected = data.getLength();
733-
734-
// Special handling for the write-to-N-mbytes-file case
735-
//TODO yet to be implemented
736-
// if (data instanceof OutputStreamData) {
737-
// OutputStreamData fd = (OutputStreamData) data;
738-
// fd.write(buf1, Data.HDR_LEN, len - Data.HDR_LEN);
739-
// if (len == inMaxPS && expected != inMaxPS) {
740-
// InputStream is = device.getInputStream(epIn);
741-
//
742-
// // at max usb data rate, 128K ~= 0.11 seconds
743-
// // typically it's more time than that
744-
// buf1 = new byte[128 * 1024];
745-
// do {
746-
// len = is.read(buf1);
747-
// fd.write(buf1, 0, len);
748-
// } while (len == buf1.length);
749-
// }
750-
// } else
751-
if (len == inMaxPS && expected != inMaxPS) {
752-
buf1 = new byte[expected];
753-
byte [] buf2 = new byte[expected];
754-
// System.arraycopy(data.data, 0, buf1, 0, len);
755-
// data.data = buf1;
756-
// Log.d(TAG, "read additional bytes " +(expected - len));
757-
System.arraycopy(data.data, 0, buf2, 0, inMaxPS);
758-
data.length += mConnection.bulkTransfer(epIn, buf1,
759-
expected - len, DEFAULT_TIMEOUT);// device.getInputStream(epIn).read(buf1,
760-
// len, expected
761-
// - len);
762-
// Log.d(TAG, "Read oK");
763-
System.arraycopy(buf1, 0, buf2, inMaxPS, data.length-inMaxPS);
764-
data.data = buf2;
765-
}
766-
767-
// if ((expected % inMaxPS) == 0)
768-
// ... next packet will be zero length
769-
770-
// and do whatever parsing needs to be done
712+
// Log.d(TAG, "Start Read Data");
713+
byte readBuffer[] = new byte[inMaxPS];
714+
int readLen = 0;
715+
readLen = mConnection.bulkTransfer(epIn, readBuffer, inMaxPS,
716+
DEFAULT_TIMEOUT);
717+
data.data = readBuffer;
718+
data.length = readLen;
719+
if (!"data".equals(data.getBlockTypeName(data.getBlockType()))
720+
|| data.getCode() != command.getCode()
721+
|| data.getXID() != command.getXID()) {
722+
throw new PTPException("protocol err 1, " + data);
723+
}
724+
725+
int totalLen = data.getLength();
726+
if (totalLen > readLen) {
727+
ByteArrayOutputStream dataStream = new ByteArrayOutputStream(
728+
totalLen);
729+
730+
dataStream.write(readBuffer, 0, readLen);
731+
732+
int remaining = totalLen - readLen;
733+
while (remaining > 0) {
734+
int toRead = (remaining > inMaxPS )? inMaxPS : remaining;
735+
readLen = mConnection.bulkTransfer(epIn, readBuffer, toRead,
736+
DEFAULT_TIMEOUT);
737+
dataStream.write(readBuffer, 0, readLen);
738+
remaining -= readLen;
739+
}
740+
741+
data.data = dataStream.toByteArray();
742+
data.length = data.length;
743+
}
771744
data.parse();
772745
}
773746
}

0 commit comments

Comments
 (0)