Java 읽 기 InputStream 길이 및 available, readAllBytes, read, mark, reset 방법 소개

1717 단어 Java#기억
    /**   InputStream         InputStream  ,  InputStream     InputStream ,       ,readInputStream(inputStream).length   inputStream.readAllBytes().length,readAllBytes         ,             */

    public byte[] readInputStream(InputStream inputStream) {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        try {
            while ((length = inputStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return outStream.toByteArray();
    }
	
	/**       , InputStream         InputStream  ,        InputStream    ,  read   0   */
	inputStream.available();
	
    /**   :      InputStream ,            InputStream   0,       */
	int count = 0; 
    while (count == 0) { 
    count = inputStream.available(); 
    }
  
    /**   :    InputStream  ,InputStream     ,        ,  mark    ,               ,           ,     ,reset            ,  :        mark      ,   InputStream      */
    inputStream.mark(count);
  
    inputStream.reset();
  
    /**          1024     */
    inputStream.read(new byte[1024]);

    /**   :      inputstream,       ,      ,inputstream available()       ,   0,  : */
    URL url = null;
    try {
        url = new URL(urlString);
    } catch (MalformedURLException e) {
         e.printStackTrace();
    }

    HttpURLConnection httpURLConnection = null;
    try {
        httpURLConnection = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }

    httpURLConnection.getContentLength();
  

좋은 웹페이지 즐겨찾기