All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.liveSense.api.sql.BlobClobConverter Maven / Gradle / Ivy

The newest version!
package org.liveSense.api.sql;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;


public class BlobClobConverter {
	
	
	//METHODS - static
	public static String convertStreamToString(InputStream is) throws IOException {
		/*
		 * To convert the InputStream to String we use the
		 * Reader.read(char[] buffer) method. We iterate until the
		 * Reader return -1 which means there's no more data to
		 * read. We use the StringWriter class to produce the string.
		 */
		if (is != null) {
			Writer writer = new StringWriter();

			char[] buffer = new char[1024];
			try {
				Reader reader = new BufferedReader(new InputStreamReader(is,
						"UTF-8"));
				int n;
				while ((n = reader.read(buffer)) != -1) {
					writer.write(buffer, 0, n);
				}
			} finally {
				is.close();
			}
			return writer.toString();
		} else {
			return "";
		}
	}
	
	public static String convertReaderToString(Reader reader) throws IOException {
		StringBuffer data = new StringBuffer(1000);
		char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            data.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return data.toString();	 
    }	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy