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

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…..

Fetching server name and port number for ADF application

Requirement – How to get server name and server port.
Solutions- You can get this information in by writing below code in java

FacesContext fctx = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) fctx.getExternalContext ().GetRequest ();
String serverName = request.getServerName();
int serverPort =request.getServerPort();

System.out.println ("Server Name:" + serverName);
System.out.println ("Server Port no:" + server port);

Happy coding with Vinay Kumar in Techartifact.