Wednesday, May 13, 2009

Deleting files securely in Java

It is possible to delete a file securely in Java. I wrote this small java utility function. It utilizes a workaround in order to avoid this bug. After running this code I undeleted the file with Undelete Plus 2.9.8. The file was filled with random bytes, not the original content, so it worked. But further testing might me needed before production.

Here is the code:

public void secureDelete(File file) throws IOException {
if (file.exists()) {
long length = file.length();
SecureRandom random = new SecureRandom();
RandomAccessFile raf = new RandomAccessFile(file, "rws");;
byte[] data = new byte[64];
int pos = 0;
while (pos <>
pos += data.length;

Tuesday, April 7, 2009

Sample code for Image Resizing in Java

I needed to resize a java.awt.Image object so that it fits into a small JLabel in my j2se/java/swing desktop application. I wrote a utility method which resizes the given Image to fit into given width and height. Here is the code of my resize method:

// import java.awt.Image;
// import java.awt.image.BufferedImage;

 * Resizes the given Image so that it fits into given width and height.
 * @param inputImage
 * @param fitWidth
 * @param fitHeight
 * @return
public static Image fitImage(Image inputImage, int fitWidth, int fitHeight) {
    int w = inputImage.getWidth(null);
    int h = inputImage.getHeight(null);
    double dw = w / (double) fitWidth;
    double dh = h / (double) fitHeight;
    if (dw <= 1d && dh <= 1d)
        return inputImage;

    double d = Math.max(dw, dh);
    w = (int)Math.min(fitWidth, Math.round(w / d));
    h = (int)Math.min(fitHeight, Math.round(h / d));
    BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    bufferedImage.getGraphics().drawImage(inputImage, 0, 0, w, h, null);
    return bufferedImage;

Thursday, April 2, 2009

Google Tools for Webmasters

Google has a great service for webmasters: Google Webmaster Tools

Today, a website is not visible to the world if it's not visible to Google. And a small pagerank is not enough for a publishing website to survive.

In the old days a webmaster had to submit her site's URL to search engines and wait for it to be indexed. Now that there is only one dominant search engine in the market (Google) it is sufficient for a website to be seen on google search results.

Other than submitting URLs of your site to Google, you can see a lot of information about your site by using this tool. Google gives results of crawling your site. Those results include: broken links, HTTP errors, unfollowed URLs and information about your robots.txt file.

Another important feature is that Google sends warning messages through this tool, before giving pagerank penalty or when it detects your site is hacked. or even before your site is hacked.

Technology behind Google Maps Street View

I really have wondered and looked up in google for the technology behind Google Maps Street View. It's such an amazing technology that changes my perception of Google Maps completely. Another page is an interview with the people collecting images for google street view driving through cities.

Although the technology seems so simple, it is a great innovation for the world wide web. I bet we will see great applications of this in the near future.

For example, a mall (a restaurant, a store etc.) can make a 3D journey in its building and add it to its website.