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  public static byte[] stringToBytes(String content, boolean bom) throws IOException {
089    ByteArrayOutputStream bs = new ByteArrayOutputStream();
090    OutputStreamWriter sw = new OutputStreamWriter(bs, "UTF-8");
091    if (bom)
092      sw.write('\ufeff');  // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
093    sw.write(content);
094    sw.flush();
095    sw.close();
096    return bs.toByteArray(); 
097  }
098  public static void stringToFile(String content, String path) throws IOException  {
099    File file = new CSFile(path);
100    stringToFile(content, file);
101  }
102
103  public static void stringToFile(String content, File file, boolean bom) throws IOException  {
104    OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
105    if (bom)
106      sw.write('\ufeff');  // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
107    sw.write(content);
108    sw.flush();
109    sw.close();
110  }
111  
112  public static void stringToFile(String content, String path, boolean bom) throws IOException  {
113    File file = new CSFile(path);
114    stringToFile(content, file, bom);
115  }
116
117  public static void stringToFileNoPrefix(String content, String path) throws IOException  {
118    File file = new CSFile(path);
119    OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
120    sw.write(content);
121    sw.flush();
122    sw.close();
123  }
124
125  public static String fileToString(File f) throws FileNotFoundException, IOException {
126    return streamToString(new FileInputStream(f));
127  }
128  
129  public static String fileToString(String src) throws FileNotFoundException, IOException  {
130    return streamToString(new FileInputStream(new CSFile(src)));
131        }
132
133  public static String streamToString(InputStream input) throws IOException  {
134    InputStreamReader sr = new InputStreamReader(input, "UTF-8");    
135    StringBuilder b = new StringBuilder();
136    //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
137    int i = -1;
138    while((i = sr.read()) > -1) {
139      char c = (char) i;
140      b.append(c);
141    }
142    sr.close();
143    
144    return  b.toString().replace("\uFEFF", ""); 
145  }
146
147  public static byte[] streamToBytes(InputStream input) throws IOException  {
148    if (input== null) {
149      return null;
150    }
151    // Define a size if you have an idea of it.
152    ByteArrayOutputStream r = new ByteArrayOutputStream(2048);
153    byte[] read = new byte[512]; // Your buffer size.
154    for (int i; -1 != (i = input.read(read)); r.write(read, 0, i));
155    input.close();
156    return r.toByteArray();
157  }
158
159  public static void bytesToFile(byte[] bytes, String path) throws IOException {
160    File file = new CSFile(path);
161    OutputStream sw = new FileOutputStream(file);
162    sw.write(bytes);
163    sw.flush();
164    sw.close();
165    
166  }
167
168  public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException {
169    return streamToBytes(new FileInputStream(new CSFile(srcFile)));
170  }
171
172  public static String bytesToString(byte[] bs) throws IOException {
173    return streamToString(new ByteArrayInputStream(bs));
174  }
175
176
177}