How to Read/convert an InputStream to a String

There are multiple ways .I will list down few of them.

Using Old and standard java solution –

public static String fromStream(InputStream in) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder out = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        out.append(line);
    }
    return out.toString();
}

return sb.toString();

if you using Google-Collections/Guava-

InputStream stream = ...
String content = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
Closeables.closeQuietly(stream);

If you can use Apache Common library..then this is useful –

StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();

Quick way but only work in deserialization process –

String result = (String)new ObjectInputStream( inputStream ).readObject();

Note:ObjectInputStream is about deserialization, and the stream have to respect the serialization protocol to work, which may not always true in all cases.

In the end, the most efficient solution and only in two lines using java Scanner class –

Tricky is to remember the regex \A, which matches the beginning of input. This effectively tells Scanner to tokenize the entire stream, from beginning to (illogical) next beginning.

public static String convertToString(InputStream in) {
    java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\\A");  
        
    return s.hasNext() ? s.next() : "";
}

I would love to hear any comment by you for making more simple…

Happy learning by Vinay Kumar in techartifact

Book Review: Developing Web Applications with Oracle ADF Essentials

The folks at Packt asked me to review one of their book on Developing Web Applications with Oracle ADF Essentials of stenvesterli .If you are interested in buying it .
You can get it from

Amazon site
packt site

Book cover

This book contains 8 chapter –

Chapter 1. My First ADF Essentials Application
– How to install MySql,JDK, Glassfish.
– How you can setup jdev and ADF essential.

Chapter 2. Creating Business Services
– Give depth knowledge of ADF BC
– Creating sample application in ADF BC and testing.

Chapter 3. Creating Task Flows and Pages
– How you can create pages
– How to work in taskflow. Explanation of task flow.I really liked.

Chapter 4. Adding Business Logic
– Adding custom login in BC classes
– Adding logic to UI
– Sample application

Chapter 5. Building Enterprise Applications
– How to structure your code ,Using sub version.
– How to create libraries in ADF
– Creating sample application and use these features
Chapter 6. Debugging ADF Applications
– How to use ADF logging.
– How to do logging in Glassfish
– How you can do debugging in Jdeveloper
Chapter 7. Securing an ADF Essentials Application
– Most interesting chapter for me.Clearly explained APache shiro .
– Advanced features of Apache shiro.
– Implementation Authorization
Chapter 8. Build and Deploy
– Another interesting topic.How you can build Application
– creating script and deploying in glassfish.
– Advanced topoc for preparing application for go live.

Summary:
Good and detailed explanation of ADF essentials what are limitations and how to overcome on it. This book not only cover ADF essentail but whole ADF except ADF security.
Very usefull if you want to run your app on glassfish,Jboss or tomcat.Creatomg build script for ADF developer is really usefull. Someone who starting ADF learning, i would really recommend for him

Happy learning with Vinay in techartifact….

Oracle ADF certification

Yesterday, I had given the Oracle Application Development Framework 11g Essentials exam (1Z0-554). I passed it and I am now an Application Development Framework Implementation specialist.
Tricky questions were based on ADF BC (Business Components) & ADF UI. Having BC experience is a must for taking the exam. There were 79 questions that i had to answer in 105 minutes .Passing percentage 61%. I completed in around 75 minutes. The questions were above standard and i enjoyed the test .There are few question of Many to many relationship between Table as well.

Tips for new candidates -Tricky question based on life cycle .Focus on ADF BC. Reuse example

For more details, check this:http://www.oracle.com/partners/en/knowledge-zone/middleware/adf-exam-page-322499.html

O_Certified Specialist_clr

Soon i will posting more article not questions for sure , which helps for preparation of this exam.

Happy leaning with Vinay Kumar in Techartifact…..