<commit_before>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package org.appcelerator.titanium;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiConfig;
import org.appcelerator.titanium.util.TiMimeTypeHelper;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

@Kroll.proxy
public class TiBlob extends KrollProxy
{
	private static final String LCAT = "TiBlob";
	private static final boolean DBG = TiConfig.LOGD;

	public static final int TYPE_IMAGE = 0;
	public static final int TYPE_FILE = 1;
	public static final int TYPE_DATA = 2;
	public static final int TYPE_STRING = 3;

	private int type;
	private Object data;
	private String mimetype;
	private int width, height;

	private TiBlob(TiContext tiContext, int type, Object data, String mimetype)
	{
		super(tiContext);
		this.type = type;
		this.data = data;
		this.mimetype = mimetype;
		this.width = 0;
		this.height = 0;
	}

	public static TiBlob blobFromString(TiContext tiContext, String data)
	{
		return new TiBlob(tiContext, TYPE_STRING, data, "text/plain");
	}

	public static TiBlob blobFromFile(TiContext tiContext, TiBaseFile file)
	{
		return blobFromFile(tiContext, file, TiMimeTypeHelper.getMimeType(file.nativePath()));
	}

	public static TiBlob blobFromFile(TiContext tiContext, TiBaseFile file, String mimeType)
	{
		return new TiBlob(tiContext, TYPE_FILE, file, mimeType);
	}

	public static TiBlob blobFromImage(TiContext tiContext, Bitmap image) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte data[] = new byte[0];
		if (image.compress(CompressFormat.PNG, 100, bos)) {
			data = bos.toByteArray();
		}

		TiBlob blob = new TiBlob(tiContext, TYPE_IMAGE, data, "image/bitmap");
		blob.width = image.getWidth();
		blob.height = image.getHeight();
		return blob;
	}

	public static TiBlob blobFromData(TiContext tiContext, byte[] data) {
		return blobFromData(tiContext, data, "application/octet-stream");
	}

	public static TiBlob blobFromData(TiContext tiContext, byte[] data, String mimetype) {<commit_msg>fall back to TiMimeTypeHelper if the passed in mimetype for blobFromFile is null<commit_after>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package org.appcelerator.titanium;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.io.TiBaseFile;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiConfig;
import org.appcelerator.titanium.util.TiMimeTypeHelper;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

@Kroll.proxy
public class TiBlob extends KrollProxy
{
	private static final String LCAT = "TiBlob";
	private static final boolean DBG = TiConfig.LOGD;

	public static final int TYPE_IMAGE = 0;
	public static final int TYPE_FILE = 1;
	public static final int TYPE_DATA = 2;
	public static final int TYPE_STRING = 3;

	private int type;
	private Object data;
	private String mimetype;
	private int width, height;

	private TiBlob(TiContext tiContext, int type, Object data, String mimetype)
	{
		super(tiContext);
		this.type = type;
		this.data = data;
		this.mimetype = mimetype;
		this.width = 0;
		this.height = 0;
	}

	public static TiBlob blobFromString(TiContext tiContext, String data)
	{
		return new TiBlob(tiContext, TYPE_STRING, data, "text/plain");
	}

	public static TiBlob blobFromFile(TiContext tiContext, TiBaseFile file)
	{
		return blobFromFile(tiContext, file, TiMimeTypeHelper.getMimeType(file.nativePath()));
	}

	public static TiBlob blobFromFile(TiContext tiContext, TiBaseFile file, String mimeType)
	{
		if (mimeType == null) {
			mimeType = TiMimeTypeHelper.getMimeType(file.nativePath());
		}
		return new TiBlob(tiContext, TYPE_FILE, file, mimeType);
	}

	public static TiBlob blobFromImage(TiContext tiContext, Bitmap image) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte data[] = new byte[0];
		if (image.compress(CompressFormat.PNG, 100, bos)) {
			data = bos.toByteArray();
		}

		TiBlob blob = new TiBlob(tiContext, TYPE_IMAGE, data, "image/bitmap");
		blob.width = image.getWidth();
		blob.height = image.getHeight();
		return blob;
	}

	public static TiBlob blobFromData(TiContext tiContext, byte[] data) {
		return blobFromData(tiContext, data, "application/octet-stream");
	}

	public static TiBlob blobFromData(TiContext tiContext, byte[] data, String mimetype) {
{'fraction_non_alphanumeric': 0.09333333333333334, 'fraction_numerical': 0.007407407407407408, 'mean_word_length': 5.158494868871152, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2366667', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1685, 'n_words': 477}