Showing posts with label Notes. Show all posts
Showing posts with label Notes. Show all posts

Thursday, December 29, 2011

Quick way to encrypt your objects in Java

Hi, hope you have a good Christmas :) Mine was not bad, but here I am to write a little more.
Besides eating chocolates, these days I have continued some of my school works. One in specific is a game for android, that soon I will talk more in detail.
 
One important thing in games is the possibility of save the scores in a way that no one can forge, at least directly. So how do this in Java?

The simplest idea that I remembered was to put the scores in a particular class, make it serializable, apply a xor and write it to a file, to recover do the reverse process.

Tuesday, December 13, 2011

Downloading files in Java via HTTP

Today a friend of mine asked me a small piece of code for an Android application that allows downloading a file via HTTP from a remote server and save it to a local file. I looked in my documents and quickly found a small download manager that did precisely that.
But now I will share with you a class in Java (android or not) that takes as argument a URL and the destination file and download it. Remember that this code is very basic and is only to give ideas.


public class DownloaderClass {
    public void downloadFile(URL url, File outputFile) {
        int BUFFER_SIZE = 4096;
       
        InputStream in = null;
        FileOutputStream fos = null;
       
        try {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();

            if (conn.getResponseCode() / 100 != 2) {
                throw new RuntimeException("Response Code: " + conn.getResponseCode());
            }
           
            in = conn.getInputStream();
            fos = new FileOutputStream(outputFile);

            byte data[] = new byte[BUFFER_SIZE];
            int numRead;
            while (((numRead = in.read(data)) != -1)) {
                fos.write(data, 0, numRead);
            }
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }
}



To use it, you just need 2 lines of code in your main.
DownloaderClass dc = new DownloaderClass();
dc.downloadFile(new URL("http://hackedprojects.blogspot.com/favicon.ico"),new File("c:\\favicon.ico"));



As you can see, it's simples :P

Read text file line-by-line in Java

I know that it's simple, but I'll leave it here.

DataInputStream dis = null;
try {
   dis = new DataInputStream(new FileInputStream("file.txt"));
   BufferedReader br = new BufferedReader(new InputStreamReader(dis));
   String line;
   while ((line = br.readLine()) != null) {
      System.out.println(line);
   }
} catch (Exception e) {
   System.err.println(e.getMessage());
}finally{
   try {
      dis.close();
   } catch (IOException ex) {}
}

Data types conversions in java


In the last weeks I've been programming in Java for 2 school works and others 3 more projects, I'm the type of person how prefer thing more low-level, so I can say that I never used it so much in so little time. The good thing about that is that I'm finally learning many things about objects and events, it's just like a new world.
So I have been taking notes and I'll share a little about a thing that I always forget, how to convert between data types commonly used.