Converting encoding of a Stream/String

Hi,
I am wondering if there is a way we can convert encoding of a Stream/String. For example, I have an Stream/String having iso-8859-1 encoding and I want to convert it to utf-8 encoding. Is there any way to do that conversion?
I figured out a way, but that is expensive to do. I build a JDOM document out of that InpuStream, and use Outputter to output the document into an OutputStream/String setting the encoding. The resultant OutputStream/String has the new encoding. Here is what I do for this:
Document doc = getDocumentFromStream(inputStream);
XMLOutputter outputter = new XMLOutputter();
//default encoding in JDOM is "UTF-8", if no encoding is specified, encoding
//is not set and thus defaults to "UTF-8"
if(StringUtils.isNotEmpty(encoding)){          
     outputter.setEncoding(encoding);
outputter.setTextTrim(true);
outputter.output(doc, out);
I am wondering if there is any way we can do it without using JDOM thing, which is expensive.
I will appreciate any help on this issue.
Thanks in advance,
Pramodh.

Hi,
Thanks for the reply!
The reason, I want to convert the encoding of a Stream is: Our application supports UTF-8 very well. So, incoming data in non-UTF-8 encoding may most probably fail. There are characters like �, TM, copyright, etc which are supported by UTF and also ISO-8859-1. So, if I change the incoming ISO-8859-1 Stream into UTF-8, then the application will like resultant UTF-8 data.
I tried what u suuggested, but stil can't get it work. Following is what i did:
reader = new InputStreamReader(inStream, "ISO-8859-1");
writer = new OutputStreamWriter(outStream, "UTF-8");
int n;               
while ((n = reader.read(c)) > 0) {
writer.write(c, 0, n);
Any other suggestions?
Thanks.

Similar Messages

  • Live encoding of .asx stream

    Hello,
    I have a live .asx stream which I need to encode into m4v or mp4 inorder to be able to broadcast such that users can view it on the iphone. The .asx stream is being viewed in QuickTime player in Mac using Flip4Mac. When I am using Quicktime broadcaster for live encoding its giving error. Can anyone suggest solution?

    Hi,
    Thanks for the reply!
    The reason, I want to convert the encoding of a Stream is: Our application supports UTF-8 very well. So, incoming data in non-UTF-8 encoding may most probably fail. There are characters like �, TM, copyright, etc which are supported by UTF and also ISO-8859-1. So, if I change the incoming ISO-8859-1 Stream into UTF-8, then the application will like resultant UTF-8 data.
    I tried what u suuggested, but stil can't get it work. Following is what i did:
    reader = new InputStreamReader(inStream, "ISO-8859-1");
    writer = new OutputStreamWriter(outStream, "UTF-8");
    int n;               
    while ((n = reader.read(c)) > 0) {
    writer.write(c, 0, n);
    Any other suggestions?
    Thanks.

  • How can I convert an int to a string?

    Hi
    How can I convert an int to a string?
    /ad87geao

    Here is some the code:
    public class GUI
        extends Applet {
      public GUI() { 
        lastValue = 5;
        String temp = Integer.toString(lastValue);
        System.out.println(temp);
        showText(temp);
      private void showText(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            tArea2.setText(text + "\n");
    }

  • Cannot convert type class java.lang.String to class oracle.jbo.domain.Clob

    Cannot convert type class java.lang.String to class oracle.jbo.domain.ClobDomain.
    Using ADF Business Components I have a JSFF page fragment with an ADF form based on a table with has a column of type CLOB. The data is retrieved from the database and displayed correctly but when any field is changed and submitted the above error occurs. I have just used the drag and drop technique to create the ADF form with a submit button, am I missing a step?
    I am using the production release of Jdeveloper11G

    Reproduced and filed bug# 7487124
    The workaround is to add a custom converter class to your ViewController project like this
    package oow2008.view;
    import javax.faces.application.FacesMessage;
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.convert.Converter;
    import javax.faces.convert.ConverterException;
    import oracle.jbo.domain.ClobDomain;
    import oracle.jbo.domain.DataCreationException;
    public class ClobConverter implements Converter {
         public Object getAsObject(FacesContext facesContext,
                                   UIComponent uIComponent,
                                   String string) {
           try {
             return string != null ? new ClobDomain(string) : null;
           } catch (DataCreationException dce) {
             dce.setAppendCodes(false);
             FacesMessage fm =
               new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                "Invalid Clob Value",
                                dce.getMessage());
             throw new ConverterException(fm);
         public String getAsString(FacesContext facesContext,
                                   UIComponent uIComponent,
                                   Object object) {
           return object != null ?
                  object.toString() :
                  null;
    }then to register the converter in faces-config.xml like this
    <faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
      <application>
        <default-render-kit-id>oracle.adf.rich</default-render-kit-id>
      </application>
      <converter>
        <converter-id>clobConverter</converter-id>
        <converter-class>oow2008.view.ClobConverter</converter-class>
      </converter>
    </faces-config>then reference this converter in the field for the ClobDomain value like this
              <af:inputText value="#{bindings.Description.inputValue}"
                            label="#{bindings.Description.hints.label}"
                            required="#{bindings.Description.hints.mandatory}"
                            columns="40"
                            maximumLength="#{bindings.Description.hints.precision}"
                            shortDesc="#{bindings.Description.hints.tooltip}"
                            wrap="soft" rows="10">
                <f:validator binding="#{bindings.Description.validator}"/>
                <f:converter converterId="clobConverter"/>
              </af:inputText>

  • Need to convert a long into a string, please

    hi there
    i need to convert a long into a string. can i just cast it like this:
    (String)longNumber = some function that returns a long;

    Why not just use Long.toString()? If you start with a long value, you can create a Long object and get it's value as a String.

  • Need to convert  Date from calendar to String in the format dd-mom-yyyy

    Need to convert Date from calendar to String in the format dd-mom-yyyy+..
    This is absolutely necessary... any help plz..
    Rgds
    Arwinder

    Look up the SimpleDateFormat class: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
    Arwinder wrote:
    This is absolutely necessary... any help plz..For you maybe, not others. Please refrain from trying to urge others to answer your queries. They'll do it at their own pace ( if at all ).
    People on the forum help others voluntarily, it's not their job.
    Help them help you.
    Learn how to ask questions first: http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
    (Yes I know it's on JavaRanch but I think it applies everywhere)
    ----------------------------------------------------------------

  • How can I convert  an ArrayList to a String[]

    Hi,
    How can I convert an ArrayList (only with strings) to a String[] ?
    I've tried this :
         public static String listToString(List l) {
              StringBuffer sb = new StringBuffer();
              Iterator iter = l.iterator();
              while (iter.hasNext()) {
                   sb.append(iter.next().toString());
                   if (iter.hasNext()) {
                        sb.append(',');
              return sb.toString();
    But what I get is an array of xxxxx@435634 (for example).
    Thanks a lot !

    Strings are Objects but not all Objects are Strings and at least one of the elements in your List is not a String.

  • How can i convert an object to stream of chars or bytes?

    how can i convert an object to stream of chars or bytes?

    One way is the serialization mechanism. There are examples and explanations of it in the Java tutorial: http://java.sun.com/docs/books/tutorial/essential/io/serialization.html

  • How do i convert an object into a string?

    has said above, im trying to convert a object to a string.
    here is what i ahve so far:
    Object nodeInfo = node.getUserObject()

    RTFM
    Object o =...
    String str = o.toString();

  • How do I convert an int to a String?

    How do I convert an int to a String?

    You can also use any of these methods if you need to get more complicated:
    Integer.toString(int i)
    Integer.toBinaryString(int i)
    Integer.toHexString(int i)
    Integer.toOctalString(int i)
    Integer.toString(int i, int radix)

  • Converting an int to a string

    i want to know how to convert an int to a string .
    I have tried toString() but it says can't dereference an int.
    any ideas ????
    thanks

    What I mean by the object being null is, say for example, you have the following method:
       public String combine(Object o1, Object o2){
          return o1.toString() + o2.toString();
       }This method will throw a NullPointerException if either or both o1 and/or o2 are null. If you use it like so:
       public String combine(Object o1, Object o2){
          return String.valueOf(o1) + String.valueOf(o2);
       }This will always work. (I know someone out there would say that this would return a String like "nullnull" if both are null, and so forth and so on, but hey, you get my drift.) I'm also not saying the you can't not check if either o1 or o2 is null before proceeding, so the following also works:
       public String combine(Object o1, Object o2){
          String s = null;    // I'm using this instead of StringBuffer for
                              // simplicity's sake so don't get this wrong
          if (o1 != null){
              s = o1.toString();
          if (o2 != null){
              s += o2.toString();
          return s;
       }As you can see, there's no right or wrong way in programming as long as you achieve the result. The only thing that would matter is how clean your code is, how efficient your code is, and how maintainable your code is.

  • What are the quicktime settings to convert mpg4 files to streaming video?

    What are the quicktime settings to convert mpg4 files to streaming video?

    http://www.tansee.com/what-is-mp4-video.html
    "hint tracks" are required. QuickTime Player Pro can add them.

  • How do I convert a Variant of type=String to G data? (LV 5.1.1)

    I've tried to return a string in the Variant return of a VB function in a DLL. (I want to do this so that I can retain the interface and thus binary compatibility in the DLL.)
    When I use the DLL function in LabVIEW 5.1.1 I
    get an error in the call library function call (returning). It looks like there is a way to handle this in v 6.0 but I have to use 5.1.1 for this application. Is there a way for LV 5.1.1 to handle a Variant containing a string?
    Thanks
    Dave Karon
    Concepts NREC

    Hi Dave,
    you can use the type conversion function located under advanced -> data
    manipulation to convert the variant into a string.
    Henrik
    Dave Karon schrieb in im Newsbeitrag:
    [email protected]..
    > I've tried to return a string in the Variant return of a VB function
    > in a DLL. (I want to do this so that I can retain the interface and
    > thus binary compatibility in the DLL.)
    > When I use the DLL function in LabVIEW 5.1.1 I
    > get an error in the call library function call (returning). It looks
    > like there is a way to handle this in v 6.0 but I have to use 5.1.1
    > for this application. Is there a way for LV 5.1.1 to handle a Variant
    > containing a string?
    >
    > Thanks
    > Dave Karon
    > Concepts NREC

  • How to convert a "text variable" to String with plugin

    Hello,
    I am currently developing a InDesign (CS5) plugin, where I need to manipulate text variable.
    From the plugin I want to convert the "text variable" to string (in principle I should use the method "VariableToString (..)" of "ITextVariable")"
    My problem is, how to find the text varial from its name (I think I should use the method "FindLocationsUsed(..)" of "ITextVariableSuite") and then convert it to String
    I don't know how to use interfaces ITextVariable and ITextVariableSuite
    Plugin implemented in C++ language
    thank you

         InputStreamReader in=new InputStreamReader(fis);
          StringWriter out=new StringWriter();
          char[] buffer=new char[8192];
          int sizeRead;
          while ( ( sizeRead=in.read(buffer, 0, 8192) ) != -1 )
            out.write(buffer, 0, sizeRead);
         String content=out.toString();

  • Gdk-WARNING **: Error converting from UTF-8 to STRING

    I just installed Arch64 few days ago. In my old setup I was used to work with 2 softwares: Eclipse CDT and WingIDE.
    I tried to start these applications (they require java and python but they are binary releases) locally and I get this errors which I can't solve by myself googling... Reporting snapshots of my desktop too.
    [j_zar@Surprise eclipse]$ ./eclipse
    (eclipse:7864): Gdk-WARNING **: Error converting from UTF-8 to STRING: Impossibile aprire il convertitore da "UTF-8" a "ISO-8859-1"
    (eclipse:7864): Gdk-WARNING **: Error converting from UTF-8 to STRING: La conversione del set di caratteri da "UTF-8" a "ISO-8859-1" non è supportata
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: Failed to load Pango module '/usr/lib/pango/1.6.0/modules/pango-basic-fc.so' for id 'BasicScriptEngineFc'
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: pango_shape called with bad font, expect ugly output
    (eclipse:7864): Pango-WARNING **: pango_font_get_glyph_extents called with null font argument, expect ugly output
    (eclipse:7864): Pango-WARNING **: pango_font_get_metrics called with null font argument, expect ugly output
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Pango-WARNING **: /usr/lib/pango/1.6.0/modules/pango-basic-fc.so: wrong ELF class: ELFCLASS64
    (eclipse:7864): Gdk-WARNING **: Error converting from UTF-8 to STRING: La conversione del set di caratteri da "UTF-8" a "ISO-8859-1" non è supportata
    (eclipse:7864): Gdk-WARNING **: Error converting from UTF-8 to STRING: La conversione del set di caratteri da "UTF-8" a "ISO-8859-1" non è supportata
    (eclipse:7864): Gdk-WARNING **: Error converting from UTF-8 to STRING: La conversione del set di caratteri da "UTF-8" a "ISO-8859-1" non è supportata
    (eclipse:7864): Gdk-WARNING **: Error converting from UTF-8 to STRING: La conversione del set di caratteri da "UTF-8" a "ISO-8859-1" non è supportata
    (eclipse:7864): Gdk-WARNING **: Error converting from UTF-8 to STRING: La conversione del set di caratteri da "UTF-8" a "ISO-8859-1" non è supportata
    (eclipse:7864): Pango-WARNING **: pango_cairo_font_get_scaled_font called with bad font, expect ugly output
    [j_zar@Surprise wingide]$ ./wing
    (wing.py:7922): Gdk-WARNING **: Error converting from UTF-8 to STRING: Could not open converter from 'UTF-8' to 'ISO-8859-1'
    (wing.py:7922): Gdk-WARNING **: Error converting from UTF-8 to STRING: Conversion from character set 'UTF-8' to 'ISO-8859-1' is not supported
    Anybody can help in this???
    Last edited by J_Zar (2007-10-30 11:48:28)

    There has been talk about adding these config files, that fixes this kinda problems, to a separate pkg with a helper script you run 32bit programs with.
    for quick fix
    Link
    Grab the 3 pango files, throw them somewhere, edit pangorc to corresponding paths for the 2 other files.
    Start program with
    export PANGO_RC_FILE="/path/to/pangorc"
    program
    for the string error, if its iconv
    export GCONV_PATH=/opt/lib32/usr/lib/gconv
    to previous command
    Note, you cant start 64bit programs that use pango/iconv from the terminal you exported the variable.

Maybe you are looking for

  • Issue in attachement in email

    HI,    Am trying to send a attachement in email.If a trying to send it as a text format am getting the o/p if i change it as XLS or CSV am not getting any attachement.Wats the reason for it and how to correct it.

  • Help with if/else in displaying data - (possibly foreach)

    Hello all, I've been trying to set something up, and I'm having difficulty getting it to work. Lines 84-111 have become my biggest difficulty.  I've got an if/else statement set up so that if one record has an image located on the localhost that imag

  • Adobe creative cloud shows update for photoshop but fails to install

    I recently downloaded the trial version of PhotoShop through the creative cloud app for my Microsoft surface pro3. The program seemed to completely install apart from at the end there was an update that failed. It still shows an update in the cc app

  • How to discard changes?

    If you make changes to part of a template, the red "Apply Changes" button will appear under that part of the template.  Is there a way to discard those changes without continually undoing until the red button disappears?

  • Exported images are being exported as hidden file types

    So when I export my images using the current up to date version of Aperture 2, the files were not visible until I clicked "show hidden files." This really ***** because then when I burn a CD it appears as the CD is blank. Has this happened to anybody