package com.martin.simpledevelop.utils.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.Collections; import java.util.HashMap; import java.util.Map; import com.martin.simpledevelop.utils.log.SaLogUtils; /** * @Description IO流工具类<br> * 仅支持文本操作<br> * @File SaIOUtils.java * @Package com.martin.simpledevelop.utils.io * @Date 2015年6月26日上午1:22:01 * @Author Donghongyu 1358506549@qq.com * @Version v1.0.0 */ public class SaIOUtils { /** * Log 输出标签 */ public static String TAG = SaIOUtils.class.getName(); /** * 文本的写入操作 * * @param filePath * 文件路径。一定要加上文件名字 <br> * 例如:../a/a.txt * @param content * 写入内容 */ public static void write(String filePath, String content) { BufferedWriter bufw = null; try { bufw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(filePath))); bufw.write(content); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "文本的写入操作>>失败..." + e.getMessage()); } finally { if (bufw != null) { try { bufw.close(); } catch (IOException e) { e.printStackTrace(); SaLogUtils.e(TAG, "文本的写入操作>>失败..." + e.getMessage()); } } } } /** * 文本的读取操作 * * @param path * 文件路径,一定要加上文件名字<br> * 例如:../a/a.txt * @return */ public static String read(String path) { BufferedReader bufr = null; try { bufr = new BufferedReader(new InputStreamReader( new FileInputStream(path))); StringBuffer sb = new StringBuffer(); String str = null; while ((str = bufr.readLine()) != null) { sb.append(str); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "文本的读取操作>>失败..." + e.getMessage()); } finally { if (bufr != null) { try { bufr.close(); } catch (IOException e) { e.printStackTrace(); SaLogUtils.e(TAG, "文本的读取操作>>失败..." + e.getMessage()); } } } return null; } /** * 文本的读取操作 * * @param is * 输入流 * @return */ public static String readIN(InputStream is) { BufferedReader bufr = null; try { bufr = new BufferedReader(new InputStreamReader(is)); StringBuffer sb = new StringBuffer(); String str = null; while ((str = bufr.readLine()) != null) { sb.append(str); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); SaLogUtils.e(TAG, "文本的读取操作>>失败..." + e.getMessage()); } finally { if (bufr != null) { try { bufr.close(); } catch (IOException e) { e.printStackTrace(); SaLogUtils.e(TAG, "文本的读取操作>>失败..." + e.getMessage()); } } } return null; } /** * 把Stream转换成String * * @param is * @return */ public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "/n"); } } catch (IOException e) { e.printStackTrace(); SaLogUtils.e(TAG, " 把Stream转换成String>>失败..." + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); SaLogUtils.e(TAG, " 把Stream转换成String>>失败..." + e.getMessage()); } } return sb.toString(); } /** * 获取ByteArrayInputStream. * * @param buf * the buf * @return the input stream */ public static InputStream bytes2Stream(byte[] buf) { return new ByteArrayInputStream(buf); } /** * 从流中读取数据到byte[].. * * @param inStream * the in stream * @return the byte[] * @throws IOException * Signals that an I/O exception has occurred. */ public static byte[] stream2bytes(InputStream inStream) throws IOException { byte[] buff = new byte[1024]; byte[] data = null; try { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); int read = 0; while ((read = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, read); } data = swapStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return data; } /** * 从流中读取指定的长度到byte[]. * * @param in * the in * @param length * the length * @return the byte[] * @throws IOException * Signals that an I/O exception has occurred. */ public static byte[] stream2Bytes(InputStream in, int length) throws IOException { byte[] bytes = new byte[length]; int count; int pos = 0; while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) { pos += count; } if (pos != length) { throw new IOException("Expected " + length + " bytes, read " + pos + " bytes"); } return bytes; } /** * Simple wrapper around {@link InputStream#read()} that throws EOFException * instead of returning -1. * * @param is * the is * @return the int * @throws IOException * Signals that an I/O exception has occurred. */ public static int read(InputStream is) throws IOException { int b = is.read(); if (b == -1) { throw new EOFException(); } return b; } /** * Write int. * * @param os * the os * @param n * the n * @throws IOException * Signals that an I/O exception has occurred. */ public static void writeInt(OutputStream os, int n) throws IOException { os.write((n >> 0) & 0xff); os.write((n >> 8) & 0xff); os.write((n >> 16) & 0xff); os.write((n >> 24) & 0xff); } /** * Read int. * * @param is * the is * @return the int * @throws IOException * Signals that an I/O exception has occurred. */ public static int readInt(InputStream is) throws IOException { int n = 0; n |= (read(is) << 0); n |= (read(is) << 8); n |= (read(is) << 16); n |= (read(is) << 24); return n; } /** * Write long. * * @param os * the os * @param n * the n * @throws IOException * Signals that an I/O exception has occurred. */ public static void writeLong(OutputStream os, long n) throws IOException { os.write((byte) (n >>> 0)); os.write((byte) (n >>> 8)); os.write((byte) (n >>> 16)); os.write((byte) (n >>> 24)); os.write((byte) (n >>> 32)); os.write((byte) (n >>> 40)); os.write((byte) (n >>> 48)); os.write((byte) (n >>> 56)); } /** * Read long. * * @param is * the is * @return the long * @throws IOException * Signals that an I/O exception has occurred. */ public static long readLong(InputStream is) throws IOException { long n = 0; n |= ((read(is) & 0xFFL) << 0); n |= ((read(is) & 0xFFL) << 8); n |= ((read(is) & 0xFFL) << 16); n |= ((read(is) & 0xFFL) << 24); n |= ((read(is) & 0xFFL) << 32); n |= ((read(is) & 0xFFL) << 40); n |= ((read(is) & 0xFFL) << 48); n |= ((read(is) & 0xFFL) << 56); return n; } /** * Write string. * * @param os * the os * @param s * the s * @throws IOException * Signals that an I/O exception has occurred. */ public static void writeString(OutputStream os, String s) throws IOException { byte[] b = s.getBytes("UTF-8"); writeLong(os, b.length); os.write(b, 0, b.length); } /** * Read string. * * @param is * the is * @return the string * @throws IOException * Signals that an I/O exception has occurred. */ public static String readString(InputStream is) throws IOException { int n = (int) readLong(is); byte[] b = SaIOUtils.stream2Bytes(is, n); return new String(b, "UTF-8"); } /** * Write string string map. * * @param map * the map * @param os * the os * @throws IOException * Signals that an I/O exception has occurred. */ public static void writeStringStringMap(Map<String, String> map, OutputStream os) throws IOException { if (map != null) { writeInt(os, map.size()); for (Map.Entry<String, String> entry : map.entrySet()) { writeString(os, entry.getKey()); writeString(os, entry.getValue()); } } else { writeInt(os, 0); } } /** * Read string string map. * * @param is * the is * @return the map * @throws IOException * Signals that an I/O exception has occurred. */ public static Map<String, String> readStringStringMap(InputStream is) throws IOException { int size = readInt(is); Map<String, String> result = (size == 0) ? Collections .<String, String> emptyMap() : new HashMap<String, String>(size); for (int i = 0; i < size; i++) { String key = readString(is).intern(); String value = readString(is).intern(); result.put(key, value); } return result; } }