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

convert af:inputText to upperCase,lowerCase,capitalize in Oracle ADF

Requirement- We need to show value in input text into uppercase,lower or capitalize.There is a way to do this by setting contentStyle of the input text.

For UpperCase- <af:inputText label="name" id="it1" contentStyle="text-transform:uppercase;"/>



For LowerCase- <af:inputText label="name" id="it1" contentStyle="text-transform:lowercase;"/>


For InitCap-  <af:inputText label="name" id="it1" contentStyle="text-transform:capitalize;"/>

Note: – This is only used in displaying.When you commit, it will save as what user entered in the input text.

Using Java script –

<af:resource type="javascript">
  function changeToUpper(event) {
      var inputComp = event.getCurrentTarget();
      inputComp.setValue(inputComp.getSubmittedValue().toUpperCase());
  }
</af:resource>

<af:inputText value="#{bindings.EmpName.inputValue}"
    label="#{bindings.EmpName.hints.label}"
    columns="#{bindings.EmpName.hints.displayWidth}"
    shortDesc="#{bindings.EmpName.hints.tooltip}" id="it2"
    clientComponent="true">
 <f:validator binding="#{bindings.EmpName.validator}"/>
 <af:clientListener type="keyUp" method="changeToUpper"/>
</af:inputText>

It will trigger the javascript function which will fetch value of inputText and set to upper case.

Another way of doing this.Override voImpl method like below

public void setEmpName(String value) {
setAttributeInternal(EmpName, value.toUpperCase());
}

Happy coding with Vinay in Techartifact….. 🙂