Posts

Showing posts from March, 2013

Impetus Interview questions: Code-2

What will be the output of the following...
class A{
public static void main(String [] arg){
        String s1="hello";
        String s2=new String("hello");
        System.out.println(s1==s2);
        String s3=new String("world");
        String s4=new String("world");

        System.out.println(s3==s4);
        System.out.println(s1.equals(s2));
        System.out.println(s3.equals(s4));
        String s5="hello";
        System.out.println(s1==s5);
    }
}

Output:  See here.....
false
false
true
true
true
s1 == s2 : false, both refernece variable pointing to different objects
s3 == s4 : false, both refernece variable pointing to different objects
s1.equals(s2): true:  Different objects but values are same
s3.equals(s4): true: Different objects but values are same

 s1 == s5 : true, both refernece variable pointing to same object

Impetus Interview Questions: Code Snipet 1

What will be the output of the code?


interface TestOne{
    int limit=100;
    void display();   
}

interface TestTwo{
    int limit=200;
    void display();   
}

public class TestInterface implements TestOne,TestTwo{
    //int limit=0;
    @Override
    public void display() {
        System.out.println("Hello... Test interface...."+limit);

    }

    public static void main(String [] arg){
        TestInterface obj=new TestInterface();
        obj.display();
     }

}
-------------------------------------------

Output:Compiler error at System.out.println(....) : The field "limit" is ambiguous.

Read file from google cloud storage using Java

You can read file from google cloud storage using following code....

public void readTextFileOnly(String fileName) { 
        log.info("Reading from google cloud storage,fileName:"+fileName);
        FileService fileService = FileServiceFactory.getFileService();
        String filename = "/gs/" + BUCKET_NAME + "/" + fileName;
        log.info("Reading from google cloud storag: filename:"+filename);
        AppEngineFile readableFile = new AppEngineFile(filename);
        FileReadChannel readChannel;
        try {
                readChannel = fileService.openReadChannel(readableFile, false);
                BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
                String line = reader.readLine();
                readChannel.close();
        } catch (FileNotFoundException e) {
            log.severe("FileNotFoundException:"+e.getMessage());
            e.printStackTrace();
        } catch (LockExce…

Blowfish decryption: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

Problem : Blowfish decryption+  javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

Solution: Always check your data whether it is a multiple of 8 bit or not.

if(decodedBytes.length % 8 != 0){ //not a multiple of 8
                   System.out.println("decodedBytes is not padded properly in 8 bits");
                   //create a new array with a size which is a multiple of 8
                   byte[] padded = new byte[decodedBytes.length + 8 - (decodedBytes.length % 8)];
                   //copy the old array into it
                   System.arraycopy(decodedBytes, 0, padded, 0, decodedBytes.length);
                   decodedBytes = padded;
               }else{
                   System.out.println("decodedBytes is padded properly in 8 bits");
               }

For complete example, see here... . http://knowledge-serve.blogspot.in/2013/03/encryption-and-decryption-using.html

Encryption and decryption using Blowfish and base64 on Google App Engine

Encryption and Decryption  using Blowfish and base64 on Google App Engine using Java

1.. Encryption double using blowfish and then base64

public static String doubleEncryption_Blowfish_Base64(String to_encrypt,String key,String charEncoding){
           //charEncoding="UTF-8";
           Cipher cipher;
            try {
               byte[] encodedBytes =to_encrypt.getBytes(charEncoding);       

               if(encodedBytes.length % 8 != 0){ //not a multiple of 8
                   System.out.println("encodedBytes is not padded properly in 8 bits");
                   //create a new array with a size which is a multiple of 8
                   byte[] padded = new byte[encodedBytes.length + 8 - (encodedBytes.length % 8)];
                   //copy the old array into it
                   System.arraycopy(encodedBytes, 0, padded, 0, encodedBytes.length);
                   encodedBytes = padded;
               }else{
                   System.out.println("encodedBytes is …

encrypt in php and decrypt in java using blowfish/base64 on Google App Engine

encrypt in php and decrypt in java using blowfish/base64 on Google App Engine
--------------------------------------------------------------------------------

Suppose you have a encryptedData with some key using blowfish and base64 from PHP code and you want to decrypt it using Java....

public static String doubleDecryption_Base64_Blowfish(String to_decrypt,String key,String charSet){          
          // charSet="UTF-8";
           Cipher cipher;
            try {
               byte[] decodedBytes = Base64.decodeBase64(to_decrypt.getBytes(charSet));
               if(decodedBytes.length % 8 != 0){ //not a multiple of 8
                   System.out.println("decodedBytes is not padded properly in 8 bits");
                   //create a new array with a size which is a multiple of 8
                   byte[] padded = new byte[decodedBytes.length + 8 - (decodedBytes.length % 8)];
                   //copy the old array into it
                   System.arraycopy(decodedByte…