Changeset 10759


Ignore:
Timestamp:
May 4, 2011, 7:21:57 PM (13 years ago)
Author:
mlg
Message:

Fixes problems with accents.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/transverse/util/Tools.java

    r10749 r10759  
    1111import java.io.InputStreamReader;
    1212import java.io.PrintWriter;
     13import java.io.Reader;
    1314import java.io.StringReader;
    1415import java.io.StringWriter;
     
    7374    private static final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class);
    7475
     76    //    /**
     77    //     * Transformation of an inpustream into string.<br/>
     78    //     * useful to read the result of the webservice
     79    //     * @param input the stream
     80    //     * @return the string
     81    //     * @throws IOException
     82    //     */
     83    //    public static String readInputStreamAsString(InputStream input) throws IOException {
     84    //  StringWriter writer = new StringWriter();
     85    //  InputStreamReader streamReader = new InputStreamReader(input);
     86    //  BufferedReader buffer = new BufferedReader(streamReader);
     87    //  String line = "";
     88    //  while (null != (line = buffer.readLine())) {
     89    //      writer.write(line);
     90    //  }
     91    //  return writer.toString();
     92    //    }
     93
    7594    /**
    7695     * Transformation of an inpustream into string.<br/>
     
    8099     * @throws IOException
    81100     */
    82     public static String readInputStreamAsString(InputStream input) throws IOException {
    83         StringWriter writer = new StringWriter();
    84         InputStreamReader streamReader = new InputStreamReader(input);
    85         BufferedReader buffer = new BufferedReader(streamReader);
    86         String line = "";
    87         while (null != (line = buffer.readLine())) {
    88             writer.write(line);
    89         }
    90         return writer.toString();
     101    public static String readInputStreamAsString(InputStream is) throws IOException {
     102        if (is != null) {
     103            Writer writer = new StringWriter();
     104
     105            char[] buffer = new char[1024];
     106            try {
     107                Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
     108                int n;
     109                while ((n = reader.read(buffer)) != -1) {
     110                    writer.write(buffer, 0, n);
     111                }
     112            } finally {
     113                is.close();
     114            }
     115            return writer.toString();
     116        } else {
     117            return "";
     118        }
    91119    }
    92120
Note: See TracChangeset for help on using the changeset viewer.