|
25 | 25 | import java.util.zip.InflaterInputStream; |
26 | 26 |
|
27 | 27 | import com.genexus.common.interfaces.SpecificImplementation; |
| 28 | +import com.genexus.internet.GXInternetConstants; |
28 | 29 | import com.genexus.platform.NativeFunctions; |
29 | 30 | import com.genexus.util.Codecs; |
30 | 31 | import com.genexus.util.IniFile; |
@@ -767,38 +768,64 @@ public static void copyFileRetry(String src, String dest) |
767 | 768 | try { destination.close(); } catch (IOException e) { ; } |
768 | 769 | } |
769 | 770 | } |
770 | | - |
771 | | - |
772 | | - public static void InputStreamToFile(InputStream source, String fileName) |
773 | | - { |
774 | | - if (source == null) |
775 | | - { |
776 | | - throw new IllegalArgumentException("InputStreamToFile -> Input stream can't be null"); |
| 771 | + public static String BOMInputStreamToStringUTF8(InputStream istream) { |
| 772 | + if (istream == null) { |
| 773 | + throw new IllegalArgumentException("BOMInputStreamToStringUTF8 -> Input stream can't be null"); |
777 | 774 | } |
778 | | - |
779 | | - byte[] buffer; |
780 | | - int bytes_read; |
781 | | - OutputStream destination = null; |
782 | | - try |
783 | | - { |
784 | | - destination = new BufferedOutputStream(new FileOutputStream(fileName)); |
785 | | - buffer = new byte[1024]; |
786 | | - |
787 | | - while (true) |
788 | | - { |
789 | | - bytes_read = source.read(buffer); |
790 | | - if (bytes_read == -1) break; |
791 | | - destination.write(buffer, 0, bytes_read); |
| 775 | + boolean firstLine = true; |
| 776 | + StringBuilder stringBuilder = new StringBuilder(); |
| 777 | + try (BufferedReader r = new BufferedReader(new InputStreamReader(istream, "UTF8"))) { |
| 778 | + for (String s = ""; (s = r.readLine()) != null; ) { |
| 779 | + if (firstLine) { |
| 780 | + s = removeUTF8BOM(s); |
| 781 | + firstLine = false; |
| 782 | + } |
| 783 | + stringBuilder.append(s + GXInternetConstants.CRLFString); |
792 | 784 | } |
| 785 | + return stringBuilder.toString(); |
| 786 | + } catch (Exception e) { |
| 787 | + System.err.println("Error reading stream:" + e.getMessage()); |
| 788 | + return ""; |
793 | 789 | } |
794 | | - catch (IOException e) |
795 | | - { |
796 | | - System.err.println("Error writing file " + fileName + ":" + e.getMessage()); |
| 790 | + } |
| 791 | + static final String UTF8_BOM = "\uFEFF"; |
| 792 | + private static String removeUTF8BOM(String s) { |
| 793 | + if (s.startsWith(UTF8_BOM)) { |
| 794 | + s = s.substring(1); |
797 | 795 | } |
798 | | - finally |
799 | | - { |
800 | | - if (source != null) |
801 | | - try { source.close(); } catch (IOException e) { ; } |
| 796 | + return s; |
| 797 | + } |
| 798 | + |
| 799 | + public static void InputStreamToFile(InputStream source, String fileName) |
| 800 | + { |
| 801 | + if (source == null) |
| 802 | + { |
| 803 | + throw new IllegalArgumentException("InputStreamToFile -> Input stream can't be null"); |
| 804 | + } |
| 805 | + |
| 806 | + byte[] buffer; |
| 807 | + int bytes_read; |
| 808 | + OutputStream destination = null; |
| 809 | + try |
| 810 | + { |
| 811 | + destination = new BufferedOutputStream(new FileOutputStream(fileName)); |
| 812 | + buffer = new byte[1024]; |
| 813 | + |
| 814 | + while (true) |
| 815 | + { |
| 816 | + bytes_read = source.read(buffer); |
| 817 | + if (bytes_read == -1) break; |
| 818 | + destination.write(buffer, 0, bytes_read); |
| 819 | + } |
| 820 | + } |
| 821 | + catch (IOException e) |
| 822 | + { |
| 823 | + System.err.println("Error writing file " + fileName + ":" + e.getMessage()); |
| 824 | + } |
| 825 | + finally |
| 826 | + { |
| 827 | + if (source != null) |
| 828 | + try { source.close(); } catch (IOException e) { ; } |
802 | 829 | if (destination != null) |
803 | 830 | try {destination.close(); } catch (IOException e) { ; } |
804 | 831 | } |
|
0 commit comments