001/*
002Copyright (c) 2011+, HL7, Inc
003All rights reserved.
004
005Redistribution and use in source and binary forms, with or without modification, 
006are permitted provided that the following conditions are met:
007
008 * Redistributions of source code must retain the above copyright notice, this 
009   list of conditions and the following disclaimer.
010 * Redistributions in binary form must reproduce the above copyright notice, 
011   this list of conditions and the following disclaimer in the documentation 
012   and/or other materials provided with the distribution.
013 * Neither the name of HL7 nor the names of its contributors may be used to 
014   endorse or promote products derived from this software without specific 
015   prior written permission.
016
017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
026POSSIBILITY OF SUCH DAMAGE.
027
028*/
029package org.hl7.fhir.utilities;
030
031import java.io.BufferedReader;
032import java.io.ByteArrayInputStream;
033import java.io.ByteArrayOutputStream;
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.FileNotFoundException;
037import java.io.FileOutputStream;
038import java.io.IOException;
039import java.io.InputStream;
040import java.io.InputStreamReader;
041import java.io.OutputStream;
042import java.io.OutputStreamWriter;
043import java.util.ArrayList;
044import java.util.List;
045
046/**
047 * Set of static helper functions to read lines from files, create files from lists of lines,
048 * read files into a single string and create files from a single string.
049 * @author Ewout
050 *
051 */
052public class TextFile {
053
054        public static List<String> readAllLines(String path) throws IOException
055        {
056                List<String> result = new ArrayList<String>();
057                
058                File file = new CSFile(path);
059                BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
060                
061                while( reader.ready() )
062                        result.add(reader.readLine());
063                
064                reader.close();
065                return result;
066        }
067        
068        public static void writeAllLines(String path, List<String> lines) throws IOException
069        {
070                File file = new CSFile(path);
071                FileOutputStream s = new FileOutputStream(file);
072                OutputStreamWriter sw = new OutputStreamWriter(s, "UTF-8");
073                for( String line : lines )
074                        sw.write(line + "\r\n");
075                
076                sw.flush();
077                s.close();
078        }
079        
080        
081  public static void stringToFile(String content, File file) throws IOException {
082    OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
083    sw.write('\ufeff');  // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
084    sw.write(content);
085    sw.flush();
086    sw.close();
087  }
088  
089  public static byte[] stringToBytes(String content, boolean bom) throws IOException {
090    ByteArrayOutputStream bs = new ByteArrayOutputStream();
091    OutputStreamWriter sw = new OutputStreamWriter(bs, "UTF-8");
092    if (bom)
093      sw.write('\ufeff');  // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
094    sw.write(content);
095    sw.flush();
096    sw.close();
097    return bs.toByteArray(); 
098  }
099  
100  public static void stringToFile(String content, String path) throws IOException  {
101    File file = new CSFile(path);
102    stringToFile(content, file);
103  }
104
105  public static void stringToFile(String content, File file, boolean bom) throws IOException  {
106    OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
107    if (bom)
108      sw.write('\ufeff');  // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
109    sw.write(content);
110    sw.flush();
111    sw.close();
112  }
113  
114  public static void stringToFile(String content, String path, boolean bom) throws IOException  {
115    File file = new CSFile(path);
116    stringToFile(content, file, bom);
117  }
118
119  public static void stringToFileNoPrefix(String content, String path) throws IOException  {
120    File file = new CSFile(path);
121    OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
122    sw.write(content);
123    sw.flush();
124    sw.close();
125  }
126
127  public static String fileToString(File f) throws FileNotFoundException, IOException {
128    return streamToString(new FileInputStream(f));
129  }
130  
131  public static String fileToString(String src) throws FileNotFoundException, IOException  {
132    return streamToString(new FileInputStream(new CSFile(src)));
133        }
134
135  public static String streamToString(InputStream input) throws IOException  {
136    InputStreamReader sr = new InputStreamReader(input, "UTF-8");    
137    StringBuilder b = new StringBuilder();
138    //while (sr.ready()) { Commented out by Claude Nanjo (1/14/2014) - sr.ready() always returns false - please remove if change does not impact other areas of codebase
139    int i = -1;
140    while((i = sr.read()) > -1) {
141      char c = (char) i;
142      b.append(c);
143    }
144    sr.close();
145    
146    return  b.toString().replace("\uFEFF", ""); 
147  }
148
149  public static byte[] streamToBytes(InputStream input) throws IOException  {
150    if (input== null) {
151      return null;
152    }
153    // Define a size if you have an idea of it.
154    ByteArrayOutputStream r = new ByteArrayOutputStream(2048);
155    byte[] read = new byte[512]; // Your buffer size.
156    for (int i; -1 != (i = input.read(read)); r.write(read, 0, i));
157    input.close();
158    return r.toByteArray();
159  }
160
161  public static void bytesToFile(byte[] bytes, String path) throws IOException {
162    File file = new CSFile(path);
163    OutputStream sw = new FileOutputStream(file);
164    sw.write(bytes);
165    sw.flush();
166    sw.close();
167    
168  }
169
170  public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException {
171    return streamToBytes(new FileInputStream(new CSFile(srcFile)));
172  }
173
174  public static String bytesToString(byte[] bs) throws IOException {
175    return streamToString(new ByteArrayInputStream(bs));
176  }
177
178
179}