Showing posts with label Source Code. Show all posts
Showing posts with label Source Code. Show all posts

Sunday, May 10, 2009

Not enough storage is available to complete this operation

In IE createStyleSheet throws javascript error Not "enough storage is available to complete this operation".

document.createStyleSheet(cssfile);

To handle it swallow javascript error by catching exception.

try {
    document.createStyleSheet(cssfile)
} catch(e) {}

Friday, July 4, 2008

Compile a Java source from inside a Java program

Here is sample program which shows how to compile Java source file from inside a Java program using JDK 6.

Sample Java Code

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.tools.JavaCompilerTool;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompilerTool.CompilationTask;

public class Compiler {

public static void main (String[] args) {
String sourceFile = "c:/Sample.Java";
JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool ();
StandardJavaFileManager fileManager =
compiler.getStandardFileManager (null);

// prepare the source file(s) to compile
List sourceFileList = new ArrayList ();
sourceFileList.add (new File (sourceFile));
Iterable compilationUnits =fileManager.getJavaFileObjectsFromFiles (sourceFileList);
CompilationTask task = compiler.getTask (null,fileManager, null, null, null, compilationUnits);
task.run ();
boolean result = task.getResult ();
if (result) {
System.out.println ("Compilation was successful");
} else {
System.out.println ("Compilation failed");
}
try {
fileManager.close ();
} catch (IOException e) {
}
}
}

Reference
http://www.java2s.com

Thursday, June 26, 2008

Calculate CheckSum using Java

Some times we need to calculte checksum of specified file. In java, We can use MessageDigest class to get checksum of specified file.

Sample Java Code

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class CheckSum {
public static void main(String args[]){
StringBuffer checksum;
try {
File file = new File("C:\\Test.zip");
FileInputStream is = new FileInputStream(file);
byte buffer[] = new byte[(int)file.length()];
is.read(buffer);
MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
md.update(buffer);
byte digest[] = md.digest();
checksum = new StringBuffer();
for(int i = 0; i <>
{
String digit = Integer.toHexString(0xff & digest[i]);
if(digit.length() == 1)
checksum.append('0');
checksum.append(digit);
}
System.out.println("checksum::"+checksum.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Friday, June 20, 2008

Splash screens using Java

Almost all modern applications have a splash screen. Typically splash screens are used for the following purposes:

* Advertising a product
* Indicating to the user that the application is launching during long startup times
* Providing information that is only needed once per visit

Fortunately, Java™ SE 6 provides a solution that allows the application to display the splash screen much earlier, even before the virtual machine starts. A Java application launcher is able to decode an image and display it in a simple non-decorated window.

Here is sample java code available to display splash Screen.

Sample Java Code

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.SplashScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SplashScreen extends Frame{
static void renderFrame(Graphics2D g, int frame) {
final String[] comps = { "foo", "bar", "baz" };
g.setComposite(AlphaComposite.Clear);
g.fillRect(130, 250, 280, 40);
g.setPaintMode();
g.setColor(Color.BLACK);
g.drawString("Loading " + comps[(frame / 5) % 3] + "...", 130, 260);
g.fillRect(130, 270, (frame * 10) % 280, 20);
}

public SplashScreen() {
super("SplashScreen demo");
setSize(500, 300);
setLayout(new BorderLayout());
Menu menu = new Menu("File");
MenuItem menuItem = new MenuItem("Exit");
menu.add(menuItem);
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}

});

MenuBar mb = new MenuBar();
setMenuBar(mb);
mb.add(menu);
final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
return;
}
Graphics2D g = (Graphics2D) splash.createGraphics();
if (g == null) {
return;
}
for (int i = 0; i <>
renderFrame(g, i);
splash.update();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
splash.close();
setVisible(true);
toFront();
}

public static void main(String args[]) {
SplashScreen screen = new SplashScreen();
}
}

References

http://java.sun.com/docs/books/tutorial/uiswing/misc/splashscreen.html

Tuesday, June 17, 2008

Passing properties from command line

We can pass property from command line. The syntax to pass properties from command line is as follows.

java -D<name>=
<value> .....

The getProperty() method in System class is used to get the value of a property by specifying the property name as key.

Sample Java Code

public class Properties {

public static void main(String[] args) {

String name = System.getProperty("name");
System.out.println("name is " name);
String address = System.getProperty("address");
System.out.println("address is " +address);
String age = System.getProperty("age");
System.out.println("age is " +age);
}
}

Monday, June 16, 2008

Copy File using Java

FileChannel is class used for reading, writing, mapping, and manipulating a file. File Channels are part of Java New I/O Packages. A file can be viewed as a sequence of bytes. The various Buffer classes in New I/O Packages serve as a container for manipulating the primitive byte contents. It is also possible to allocate a new Buffer object, read and write byte data into the existing Buffer using the Buffer classes. The File Channel objects are tightly associated with the Buffer class, and now File objects are coupled with File Channel object which means that it is now possible to perform read and write data on the files using the File Channel objects.

Sample Java Code

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

public class CopyFile {

public static void main(String[] args) throws Exception{

String file = "input.txt";
FileInputStream source = new FileInputStream(file);
FileOutputStream destination = new FileOutputStream("Output.txt");

FileChannel sourceFileChannel = source.getChannel();
FileChannel destinationFileChannel = destination.getChannel();

long size = sourceFileChannel.size();
sourceFileChannel.transferTo(0, size, destinationFileChannel);
}
}

Saturday, June 7, 2008

Encrypt user password before storing it to database

It is important to encrypt user password before storing it in the database. Password must be interpreted only by your java application.
Here is the sample java code to encrypt a plain password text. Before comparing password, encrypt it using following java code.

Sample Java Code

public synchronized String encrypt(String plainpassword) throws SystemUnavailableException {
MessageDigest md = null;
try
{
md = MessageDigest.getInstance("SHA");
}
catch(NoSuchAlgorithmException e)
{
throw new SystemUnavailableException(e.getMessage());
}
try
{
md.update(plainpassword.getBytes("UTF-8"));
}
catch(UnsupportedEncodingException e)
{
throw new SystemUnavailableException(e.getMessage());
}

byte data[] = md.digest();
String encryptedPassword = (new BASE64Encoder()).encode(data);
return encryptedPassword;
}

ORM Framework for Kotlin

In Kotlin, ORM (Object-Relational Mapping) libraries provide a convenient way to interact with databases using object-oriented programming p...