Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Friday, April 22, 2011
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");raf.seek(0);raf.getFilePointer();byte[] data = new byte[64];int pos = 0;while (pos <>random.nextBytes(data);raf.write(data);pos += data.length;}raf.close();file.delete();}}
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;}
Subscribe to:
Posts (Atom)