use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.
the class OtrDataHandler method onIncomingRequest.
public synchronized void onIncomingRequest(Address requestThem, Address requestUs, byte[] value) {
// Log.e( TAG, "onIncomingRequest:" + requestThem);
SessionInputBuffer inBuf = new MemorySessionInputBuffer(value);
HttpRequestParser parser = new HttpRequestParser(inBuf, lineParser, requestFactory, params);
HttpRequest req;
try {
req = (HttpRequest) parser.parse();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (HttpException e) {
e.printStackTrace();
return;
}
String requestMethod = req.getRequestLine().getMethod();
String uid = req.getFirstHeader("Request-Id").getValue();
String url = req.getRequestLine().getUri();
if (requestMethod.equals("OFFER")) {
debug("incoming OFFER " + url);
for (Header header : req.getAllHeaders()) {
debug("incoming header: " + header.getName() + "=" + header.getValue());
}
if (!url.startsWith(URI_PREFIX_OTR_IN_BAND)) {
debug("Unknown url scheme " + url);
sendResponse(requestUs, requestThem, 400, "Unknown scheme", uid, EMPTY_BODY);
return;
}
if (!req.containsHeader("File-Length")) {
sendResponse(requestUs, requestThem, 400, "File-Length must be supplied", uid, EMPTY_BODY);
return;
}
int length = Integer.parseInt(req.getFirstHeader("File-Length").getValue());
if (!req.containsHeader("File-Hash-SHA1")) {
sendResponse(requestUs, requestThem, 400, "File-Hash-SHA1 must be supplied", uid, EMPTY_BODY);
return;
}
sendResponse(requestUs, requestThem, 200, "OK", uid, EMPTY_BODY);
String sum = req.getFirstHeader("File-Hash-SHA1").getValue();
String type = null;
if (req.containsHeader("Mime-Type")) {
type = req.getFirstHeader("Mime-Type").getValue();
}
debug("Incoming sha1sum " + sum);
Transfer transfer;
try {
transfer = new VfsTransfer(url, type, length, requestUs, requestThem, sum);
} catch (IOException e) {
e.printStackTrace();
return;
}
transferCache.put(url, transfer);
// Handle offer
// TODO ask user to confirm we want this
boolean accept = false;
if (mDataListener != null) {
try {
mDataListener.onTransferRequested(url, requestThem.getAddress(), requestUs.getAddress(), transfer.url);
// callback is now async, via "acceptTransfer" method
// if (accept)
// transfer.perform();
} catch (RemoteException e) {
LogCleaner.error(ImApp.LOG_TAG, "error approving OTRDATA transfer request", e);
}
}
} else if (requestMethod.equals("GET") && url.startsWith(URI_PREFIX_OTR_IN_BAND)) {
debug("incoming GET " + url);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int reqEnd;
try {
Offer offer = offerCache.get(url);
if (offer == null) {
sendResponse(requestUs, requestThem, 400, "No such offer made", uid, EMPTY_BODY);
return;
}
// in case we don't see a response to underlying request, but peer still proceeds
offer.seen();
if (!req.containsHeader("Range")) {
sendResponse(requestUs, requestThem, 400, "Range must start with bytes=", uid, EMPTY_BODY);
return;
}
String rangeHeader = req.getFirstHeader("Range").getValue();
String[] spec = rangeHeader.split("=");
if (spec.length != 2 || !spec[0].equals("bytes")) {
sendResponse(requestUs, requestThem, 400, "Range must start with bytes=", uid, EMPTY_BODY);
return;
}
String[] startEnd = spec[1].split("-");
if (startEnd.length != 2) {
sendResponse(requestUs, requestThem, 400, "Range must be START-END", uid, EMPTY_BODY);
return;
}
int start = Integer.parseInt(startEnd[0]);
int end = Integer.parseInt(startEnd[1]);
if (end - start + 1 > MAX_CHUNK_LENGTH) {
sendResponse(requestUs, requestThem, 400, "Range must be at most " + MAX_CHUNK_LENGTH, uid, EMPTY_BODY);
return;
}
File fileGet = new File(offer.getUri());
long fileLength = -1;
if (fileGet.exists()) {
fileLength = fileGet.length();
FileInputStream is = new FileInputStream(fileGet);
readIntoByteBuffer(byteBuffer, is, start, end);
is.close();
} else {
java.io.File fileGetExtern = new java.io.File(offer.getUri());
if (fileGetExtern.exists()) {
fileLength = fileGetExtern.length();
java.io.FileInputStream is = new java.io.FileInputStream(fileGetExtern);
readIntoByteBuffer(byteBuffer, is, start, end);
is.close();
}
}
if (mDataListener != null && fileLength != -1) {
float percent = ((float) end) / ((float) fileLength);
mDataListener.onTransferProgress(true, offer.getId(), requestThem.getAddress(), offer.getUri(), percent);
String mimeType = null;
if (req.getFirstHeader("Mime-Type") != null)
mimeType = req.getFirstHeader("Mime-Type").getValue();
mDataListener.onTransferComplete(true, offer.getId(), requestThem.getAddress(), offer.getUri(), mimeType, offer.getUri());
}
} catch (UnsupportedEncodingException e) {
// throw new RuntimeException(e);
sendResponse(requestUs, requestThem, 400, "Unsupported encoding", uid, EMPTY_BODY);
return;
} catch (IOException e) {
// throw new RuntimeException(e);
sendResponse(requestUs, requestThem, 400, "IOException", uid, EMPTY_BODY);
return;
} catch (NumberFormatException e) {
sendResponse(requestUs, requestThem, 400, "Range is not numeric", uid, EMPTY_BODY);
return;
} catch (Exception e) {
sendResponse(requestUs, requestThem, 500, "Unknown error", uid, EMPTY_BODY);
return;
}
byte[] body = byteBuffer.toByteArray();
// debug("Sent sha1 is " + sha1sum(body));
sendResponse(requestUs, requestThem, 200, "OK", uid, body);
} else {
debug("Unknown method / url " + requestMethod + " " + url);
sendResponse(requestUs, requestThem, 400, "OK", uid, EMPTY_BODY);
}
}
use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.
the class SecureMediaStore method getThumbnailVfs.
public static Bitmap getThumbnailVfs(Uri uri, int thumbnailSize) {
if (!VirtualFileSystem.get().isMounted())
return null;
File image = new File(uri.getPath());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inInputShareable = true;
options.inPurgeable = true;
try {
FileInputStream fis = new FileInputStream(new File(image.getPath()));
BitmapFactory.decodeStream(fis, null, options);
} catch (Exception e) {
LogCleaner.warn(ImApp.LOG_TAG, "unable to read vfs thumbnail" + e.toString());
return null;
}
if ((options.outWidth == -1) || (options.outHeight == -1))
return null;
int originalSize = (options.outHeight > options.outWidth) ? options.outHeight : options.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / thumbnailSize;
try {
FileInputStream fis = new FileInputStream(new File(image.getPath()));
Bitmap scaledBitmap = BitmapFactory.decodeStream(fis, null, opts);
return scaledBitmap;
} catch (FileNotFoundException e) {
LogCleaner.warn(ImApp.LOG_TAG, "can't find IOcipher file: " + image.getPath());
return null;
} catch (OutOfMemoryError oe) {
LogCleaner.error(ImApp.LOG_TAG, "out of memory loading thumbnail: " + image.getPath(), oe);
return null;
}
}
use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.
the class SecureMediaStore method copyToExternal.
public static void copyToExternal(String sourcePath, java.io.File targetPath) throws IOException {
// copy
FileInputStream fis = new FileInputStream(new File(sourcePath));
java.io.FileOutputStream fos = new java.io.FileOutputStream(targetPath, false);
IOUtils.copyLarge(fis, fos);
fos.close();
fis.close();
}
use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.
the class HttpMediaStreamer method create.
private Uri create(final File fileStream, final String mimeType) throws IOException {
try {
if (serverSocket != null)
serverSocket.close();
} catch (Exception e) {
}
// use random free port
serverSocket = new ServerSocket(0);
new Thread() {
public void run() {
try {
while (true) {
Socket socket = serverSocket.accept();
byte[] b = new byte[8192];
int len;
InputStream is = socket.getInputStream();
StringBuilder isb = new StringBuilder();
len = is.read(b);
isb.append(new String(b));
// Log.i(TAG, "request: " + isb.toString());
StringBuilder sb = new StringBuilder();
sb.append("HTTP/1.1 200\r\n");
sb.append("Content-Type: " + mimeType + "\r\n");
sb.append("Content-Length: " + fileStream.length() + "\r\n\r\n");
Log.d(TAG, "sharing content of length: " + fileStream.length());
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(sb.toString().getBytes());
FileInputStream fis = new FileInputStream(fileStream);
int idx = 0;
while ((len = fis.read(b)) != -1) {
bos.write(b, 0, len);
idx += len;
Log.d(TAG, "sharing via stream: " + idx);
}
fis.close();
bos.flush();
bos.close();
socket.close();
}
} catch (IOException e) {
Log.d(TAG, "web share error", e);
}
}
}.start();
Uri uri = Uri.parse("http://localhost:" + serverSocket.getLocalPort() + fileStream.getAbsolutePath());
return uri;
}
use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.
the class SecureCameraActivity method getThumbnail.
public Bitmap getThumbnail(ContentResolver cr, String filename) throws IOException {
File file = new File(filename);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inInputShareable = true;
options.inPurgeable = true;
FileInputStream fis = new FileInputStream(file);
BitmapFactory.decodeStream(fis, null, options);
fis.close();
if ((options.outWidth == -1) || (options.outHeight == -1))
throw new IOException("Bad image " + file);
int originalSize = (options.outHeight > options.outWidth) ? options.outHeight : options.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
fis = new FileInputStream(file);
Bitmap scaledBitmap = BitmapFactory.decodeStream(fis, null, opts);
return scaledBitmap;
}
Aggregations