InputStreamUtils.java

來源:互聯網
上載者:User

InputStreamUtils.java001import java.io.ByteArrayInputStream;002import java.io.ByteArrayOutputStream;003import java.io.IOException;004import java.io.InputStream;005 006/**007 *008 * @author Andy.Chen009 * @mail Chenjunjun.ZJ@gmail.com010 *011 */012public class InputStreamUtils {013     014    final static int BUFFER_SIZE = 4096;015     016    /**017     * 將InputStream轉換成String018     * @param in InputStream019     * @return String020     * @throws Exception022     */023    public static String InputStreamTOString(InputStream in) throws Exception{024         025        ByteArrayOutputStream outStream = new ByteArrayOutputStream();026        byte[] data = new byte[BUFFER_SIZE];027        int count = -1;028        while((count = in.read(data,0,BUFFER_SIZE)) != -1)029            outStream.write(data, 0, count);030         031        data = null;032        return new String(outStream.toByteArray(),"ISO-8859-1");033    }034     035    /**036     * 將InputStream轉換成某種字元編碼的String037     * @param in038     * @param encoding039     * @return040     * @throws Exception041     */042         public static String InputStreamTOString(InputStream in,String encoding) throwsException{043         044        ByteArrayOutputStream outStream = new ByteArrayOutputStream();045        byte[] data = new byte[BUFFER_SIZE];046        int count = -1;047        while((count = in.read(data,0,BUFFER_SIZE)) != -1)048            outStream.write(data, 0, count);049         050        data = null;051        return new String(outStream.toByteArray(),"ISO-8859-1");052    }053     054    /**055     * 將String轉換成InputStream056     * @param in057     * @return058     * @throws Exception059     */060    public static InputStream StringTOInputStream(String in) throws Exception{061         062        ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));063        return is;064    }065     066    /**067     * 將InputStream轉換成byte數組068     * @param in InputStream069     * @return byte[]070     * @throws IOException071     */072    public static byte[] InputStreamTOByte(InputStream in) throws IOException{073         074        ByteArrayOutputStream outStream = new ByteArrayOutputStream();075        byte[] data = new byte[BUFFER_SIZE];076        int count = -1;077        while((count = in.read(data,0,BUFFER_SIZE)) != -1)078            outStream.write(data, 0, count);079         080        data = null;081        return outStream.toByteArray();082    }083     084    /**085     * 將byte數群組轉換成InputStream086     * @param in087     * @return088     * @throws Exception089     */090    public static InputStream byteTOInputStream(byte[] in) throws Exception{091         092        ByteArrayInputStream is = new ByteArrayInputStream(in);093        return is;094    }095     096    /**097     * 將byte數群組轉換成String098     * @param in099     * @return100     * @throws Exception101     */102    public static String byteTOString(byte[] in) throws Exception{103         104        InputStream is = byteTOInputStream(in);105        return InputStreamTOString(is);106    }107 108}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.