More reliable method for reading vCard, may avoid OOME in weird stream behavior case

git-svn-id: https://zxing.googlecode.com/svn/trunk@2267 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-04-26 17:57:07 +00:00
parent 5af1cad965
commit a23a2b8adb

View file

@ -38,6 +38,7 @@ import android.os.Bundle;
import android.telephony.PhoneNumberUtils; import android.telephony.PhoneNumberUtils;
import android.util.Log; import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@ -174,16 +175,14 @@ final class QRCodeEncoder {
String vcardString; String vcardString;
try { try {
InputStream stream = activity.getContentResolver().openInputStream(uri); InputStream stream = activity.getContentResolver().openInputStream(uri);
int length = stream.available(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (length <= 0) { byte[] buffer = new byte[2048];
throw new WriterException("Content stream is empty"); int bytesRead;
while ((bytesRead = stream.read(buffer)) > 0) {
baos.write(buffer, 0, bytesRead);
} }
vcard = new byte[length]; vcard = baos.toByteArray();
int bytesRead = stream.read(vcard, 0, length); vcardString = new String(vcard, 0, vcard.length, "UTF-8");
if (bytesRead < length) {
throw new WriterException("Unable to fully read available bytes from content stream");
}
vcardString = new String(vcard, 0, bytesRead, "UTF-8");
} catch (IOException ioe) { } catch (IOException ioe) {
throw new WriterException(ioe); throw new WriterException(ioe);
} }