Category Archives: java

Getting IMEI Number

1.Android Device:

——————–

Dial *#06# to know your IMEI Number.

2.Direct Process:

——————-

Remove your battery and look for IMEI,you will find it.

3.Android Program:

———————-

You’ll need the

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

permission in AndroidManifest.xml file to do this.

final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);

String deviceid = tm.getDeviceId();

File Encryption and Decryption in Java And Android

package raja.JavaPages4all;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

public class AES
{
private static final String key = “1234567891234567”;
private static final String method = “AES”;
/**
* @param args
*/
public static void main(String[] args)
{
try
{
long stime=System.currentTimeMillis();
File inFile = new File(“D:/sample.wmv”);
File outFile = new File(“D:/sample_encrypted.wmv”);
File outFile_dec = new File(“D:/sample_decrypted.wmv”);
encrypt(new FileInputStream(inFile), new FileOutputStream(outFile));
long etime=System.currentTimeMillis();
System.out.println((etime-stime));
decrypt(new FileInputStream(outFile), new FileOutputStream(outFile_dec));
System.out.println(System.currentTimeMillis()-etime);
} catch (Exception e)
{
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}

public static SecretKeySpec keyGen(String s) throws NoSuchAlgorithmException
{
KeyGenerator kgen = KeyGenerator.getInstance(method);
kgen.init(128); // 192 and 256 bits may not be available
byte[] raw = s.getBytes();
SecretKeySpec result = new SecretKeySpec(raw, method);
//System.out.println(“result “+new String(result.getEncoded()));
// Instantiate the cipher
return result;
}

public static void encrypt(InputStream is, OutputStream os)
{
encryptOrDecrypt(Cipher.ENCRYPT_MODE, is, os);
}

public static void decrypt(InputStream is, OutputStream os)
{
encryptOrDecrypt(Cipher.DECRYPT_MODE, is, os);
}

public static void encryptOrDecrypt(int mode, InputStream is, OutputStream os)
{
try
{
Cipher cipher = Cipher.getInstance(method); // DES/ECB/PKCS5Padding for SunJCE
if (mode == Cipher.ENCRYPT_MODE)
{
cipher.init(Cipher.ENCRYPT_MODE, keyGen(key));
CipherInputStream cis = new CipherInputStream(is, cipher);
doCopy(cis, os);
} else if (mode == Cipher.DECRYPT_MODE)
{
cipher.init(Cipher.DECRYPT_MODE, keyGen(key));
CipherOutputStream cos = new CipherOutputStream(os, cipher);
doCopy(is, cos);
}
} catch (Exception e)
{
e.printStackTrace();
} catch (Throwable e)
{
e.printStackTrace();
}
}

public static void doCopy(InputStream is, OutputStream os) throws IOException
{
/* byte[] bytes = new byte[1024];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();*/

byte[] buffer = new byte[8192]; // Or larger (but use powers of 2)
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1)
{
os.write(buffer, 0, bytesRead);
}
os.flush();
os.close();
is.close();
}

}

BAR Code Generation in Java

Add the following jar into Class path and Lib folder in project

1.barcode.jar

Source Code:

package javapages4all;

import com.keepdynamic.barcode.generator.BarCode;

public class BARCodeGeneration {

/**
* @param args
*/
public static void main(String[] args)
{
try {
/**
* create BARCODE Class Object
*/
BarCode barcode = new BarCode();

/**
* set barcode properties
*/
barcode.setSymbologyType(BarCode.CODE128);
barcode.setCodeText(“Raja”);
/**
* disable Text displaying below Code
*/
barcode.setDisplayCodeText(false);

/**
* create image object
*/
java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(300,300,java.awt.image.BufferedImage.TYPE_BYTE_INDEXED );

/**
* get graphic context of image
*/
java.awt.Graphics2D imgGraphics = image.createGraphics();

/**
* draw barcode to specified location in Rectangle
*/
barcode.paintBarCode(imgGraphics, new java.awt.Rectangle(0, 0, 300, 300));
barcode.drawBarCode2Image(“/home/raja/Java/barimg.jpg”);
} catch (Exception e)
{
e.printStackTrace();
}

}

}

QR Code Generation in Java

Add the following jar into Class path and Lib folder in project

1.zxing-core-1.7.jar

2.qrgen-1.0.jar

3.zxing-j2se-1.7.jar

Source Code:

package javapages4all;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;
public class QRCodeGeneration {

/**
* @param args
*/
public static void main(String[] args)
{
ByteArrayOutputStream out = QRCode.from(“Raja”).to(ImageType.PNG).stream();
try
{
/**
* set the output Image Location
*/
FileOutputStream fout = new FileOutputStream(new File(“/home/raja/Java/QR_Code_raja.JPG”));
fout.write(out.toByteArray());
fout.flush();
fout.close();

} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}

}

 

Autoboxing and Unboxing

The Autoboxing and Unboxing  was released with the Java 5.

Autoboxing

During assignment, the automatic transformation of primitive  type(int, float, double etc.) into their object equivalents or wrapper type(Integer, Float, Double,etc) is known as Autoboxing.

Ex:

Integer intObject = 5; // autoboxing

auto-unboxing

During assignment or calling of constructor, the automatic transformation of wrapper types into their primitive equivalent  is known as Unboxing.

Ex:
int inative = 0;
inative = new Integer(5); // auto-unboxing

Autoboxing also works with comparison
int a = 10;
Integer b = 10;
System.out.println(a==b); // true

How to create read only List, Map and Set in Java

// creating List in Java
List<String>  names= new ArrayList<String>();

// initializing List in Java
names.add(“Raja”);
names.add(“Ramesh”);
names.add(“Ramu”);

Set<String> readOnlySet = new HashSet<String>(names);

 //Set is not yet read-only you can still add elements into Set
readOnlySet.add(“Ravi”);
//making Set readonly in Java – no add remove or set operation permitted
readOnlySet = Collections.unmodifiableSet(readOnlySet);

//trying to add element in read only Set – java.lang.UnSupportedOperationException
readOnlySet.add(“You can not add element in read Only Set”);

//trying to remove element from read only set
readOnlySet.remove(“Ramu”);

//you can not remove elements from read only Set

Map<StringString> agesmap = new HashMap<StringString>();
agesmap.put(“raja”, “24”);

//Map is not read only yet, you can still add entries into
agesmap.put(“ramesh”, “22”);

System.out.println(“Map in Java before making read only: ” + contries);

//Making Map read only in Java
Map readOnlyMap = Collections.unmodifiableMap(agesmap);

//you can not put a new entry in read only Map in Java
readOnlyMap.put(“ra”, “33”); //java.lang.UnSupportedOperation 

//you can not remove keys from read only Map in Java
readOnlyMap.remove(“raja”); //java.lang.UnSupportedOperation

Why is Java not a pure OOP Language?

Java is a OOP language and it is not a pure Object Based Programming Language.

Many languages are Object Oriented. There are seven qualities to be satisfied for a programming language to be pure Object Oriented. They are:

  1. Encapsulation/Data Hiding
  2. Inheritance
  3. Polymorphism
  4. Abstraction
  5. All predefined types are objects
  6. All operations are performed by sending messages to objects
  7. All user defined types are objects.

Java is not because it supports Primitive datatype such as int, byte, long… etc, to be used, which are not objects.

Contrast with a pure OOP language like Smalltalk, where there are no primitive types, and boolean, int and methods are all objects.

%d bloggers like this: