首页

通过dspace的源码中StreamUtils流工具类对常用InputStream转换字符串读取缓存转换处理示例

标签:StreamUtils,流转换工具类,dspace-services-utils     发布时间:2018-03-08   

一、前言

关于dspace-services-utils-2.0.3.jar源码包工具类,对输入流InputStream基于BufferedReader对字符串String读取转换处理。

二、源码说明

package org.dspace.utils;@b@@b@import java.io.BufferedReader;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.InputStreamReader;@b@@b@public class StreamUtils@b@{@b@  public static String convertStreamToString(InputStream is)@b@  {@b@    if (is == null) {@b@      throw new IllegalArgumentException("Invalid input stream, cannot be null");@b@    }@b@@b@    BufferedReader reader = new BufferedReader(new InputStreamReader(is));@b@    StringBuilder sb = new StringBuilder();@b@@b@    String line = null;@b@    try {@b@      while ((line = reader.readLine()) != null)@b@        sb.append(new StringBuilder().append(line).append("\n").toString());@b@    }@b@    catch (IOException e) {@b@      e.printStackTrace();@b@    } finally {@b@      try {@b@        is.close();@b@      } catch (IOException e) {@b@        e.printStackTrace();@b@      }@b@    }@b@    return sb.toString();@b@  }@b@}