Skip to content

Instantly share code, notes, and snippets.

View rafayali's full-sized avatar
🚀

Rafay Ali rafayali

🚀
View GitHub Profile
/**
* Sends data back to client by using HTTPServletResponse object and adding data into JSON Array
* then flushes out the stream and closes the response
* Sends data
* @param resp
* @param result
* @param message
* @param stringData
* @throws IOException
*/
@rafayali
rafayali / Requesting Server for Data - JAVA
Last active August 29, 2015 14:10
Fetches the data from server and parses it into JSON format.
/**
* A static method which sends request to server by creating
* a HTTP connection with provided URL as parameter and converts
* the returned bytes data into readable form.
*
* @param parameterUrl
* @return {@code List<String>}
* @throws Exception
*/
public static List<String> queryServer(String parameterUrl) throws Exception{
@rafayali
rafayali / Uploading BufferedImage to Amazon S3.java
Created November 21, 2014 11:14
Uploads buffered image to amazon S3 server
public static void uploadBufferedImageToServer(BufferedImage image, String fileName, String imageType) throws IOException, NullPointerException {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
ImageIO.write(image, "png", outstream);
byte[] buffer = outstream.toByteArray();
InputStream is = new ByteArrayInputStream(buffer);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/" + imageType);
meta.setContentLength(buffer.length);
AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
s3client.putObject(new PutObjectRequest(awsBucketName, fileName, is, meta).withCannedAcl(CannedAccessControlList.PublicRead));
private class TextureDownloader
{
public WWW www;
public string url;
public Texture2D tex;
public string atlasName;
}
IEnumerator _UpdateAtlas(List<TextureDownloader> texIndex)
{
@rafayali
rafayali / LoadTexturesFromAssets
Created November 25, 2014 14:18
Loading textures from an asset path in unity
string relativePathToAssets = "//GUI//productImages2//";
List<Texture> LoadTextures ()
{
List<Texture> textures = new List<Texture>();
string[] productImagesNames = Directory.GetFiles(Application.dataPath + relativePathToAssets, "*.jpg", SearchOption.AllDirectories);
foreach(string imagePath in productImagesNames){
string relativeImagePath = ("Assets" + imagePath.Replace(Application.dataPath, "")).Replace("//","/");
textures.Add(AssetDatabase.LoadAssetAtPath(relativeImagePath, typeof(Texture)) as Texture);
@rafayali
rafayali / [Unity3D] Convert a color into Texture2D
Last active August 29, 2015 14:12
Converts a color into a Texture2D format which can be written to a file or can be used as a regular texture
private Texture2D MakeTex(int width, int height, Color col)
{
Color[] pix = new Color[width * height];
for( int i = 0; i < pix.Length; ++i )
{
pix[ i ] = col;
}
Texture2D result = new Texture2D( width, height );
result.SetPixels( pix );
result.Apply();
@rafayali
rafayali / custom_intent_chooser.java
Created March 31, 2015 07:27
Creating a chooser with custom intent resolvers
public void onShareClick(View v) {
Resources resources = getResources();
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
// Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_native)));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
emailIntent.setType("message/rfc822");
@rafayali
rafayali / randhashgenerator.java
Created April 1, 2015 12:17
Random string unique hash generator
private static char[] VALID_CHARACTERS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456879".toCharArray();
public static String csRandomAlphaNumericString(int numChars) throws Exception{
SecureRandom srand = new SecureRandom();
Random rand = new Random();
char[] buff = new char[numChars];
for (int i = 0; i < numChars; ++i) {
if ((i % 10) == 0) {
@rafayali
rafayali / OrmLiteConfigurationApp.java
Last active January 6, 2016 12:15
A standalone java app to create configuration of OrmLite for android
public class OrmLiteConfigurationApp extends OrmLiteConfigUtil{
/**
* classes represents the models to use for generating the ormlite_config.txt file
*/
private static final Class<?>[] classes = new Class[] {TimeEntries.class};
/**
* Given that this is a separate program from the android app, we have to use
* a static main java method to create the configuration file.
@rafayali
rafayali / JavaNetCookieJar.java
Created June 10, 2016 04:53
Cookie Jar implementation for OkHttp.
package okhttp3;
import java.io.IOException;
import java.net.CookieHandler;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import okhttp3.internal.Internal;