Skip to content Skip to sidebar Skip to footer

Reading Unknown Length of Byte[] Inputstream in Java

Best Java code snippets using java.io.BufferedInputStream.read (Showing top xx results out of 11,295)

Refine searchRefine arrow

  • Common ways to obtain BufferedInputStream
                    

private void myMethod ()

{

}

                                                                                                                                                                BufferedInputStream bis =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (inputStream);                                                                                                                                            ByteArrayOutputStream buf =                                                                                    new                                                                                      ByteArrayOutputStream();                                                                                                                                            int                                                                                      consequence = bis.                                                              read                                                              ();                                                                                                                                            while                                                              (result != -                                                              one                                                              ) {                                                                                                                                                                    buf.write((                                                              byte                                                              ) result);                                                                                                                                                                    result = bis.                                                              read                                                              ();                                                                                                  }                                                                                                  render                                                                                      buf.toString();                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            private                                                                                      String loadLogFile(InputStream stream)                                                                                    throws                                                                                      IOException {                                                                                                                                                                    BufferedInputStream br =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (stream);                                                                                                                                                                                                                                                                                        int                                                                                      count =                                                                                    0                                                              ;                                                                                                                                                                                                                                int                                                                                      size = br.available();                                                                                                                                                                              StringBuffer sb = goose egg;                                                                                                                                                                                      if                                                                                      (size >                                                                                    0                                                              ) {                                                                                                                                                                    sb =                                                                                    new                                                                                      StringBuffer(size);                                                                                                                                                                    }                                                                                    else                                                                                      {                                                                                                                                                                    sb =                                                                                    new                                                                                      StringBuffer(                                                              1024                                                              );                                                                                                                      }                                                                                                                                                                                                                                              while                                                                                      ((count = br.                                                              read                                                              ()) != -                                                              1                                                              ) {                                                                                                                                                                    sb.append((                                                              char                                                              ) count);                                                                                                                      }                                                                                                                                                                                  br.                                                              close                                                              ();                                                                                                                      br = cypher;                                                                                                                                                                                      render                                                                                      sb.toString();                                                                                                                                                          }                                                                                          
                                                                                                                                              public                                                                                                                                                  static                                                                                      String getCharset1(String fileName)                                                                                    throws                                                                                      IOException {                                                                                                                                                                    BufferedInputStream bin =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (                                                              new                                                                                                                                                  FileInputStream                                                                                                                            (fileName));                                                                                                                                                                                                                                int                                                                                      p = (bin.                                                              read                                                              () <<                                                                                    viii                                                              ) + bin.                                                              read                                                              ();                                                                                                                                                                              String code;                                                                                                                                                                                      switch                                                                                      (p) {                                                                                                                                                                                                                                case                                                                                                                                                  0xefbb                                                              :                                                                                                                                                                    code =                                                                                    "UTF-8"                                                              ;                                                                                                                                                                                                                                suspension                                                              ;                                                                                                                                                                                                                                case                                                                                                                                                  0xfffe                                                              :                                                                                                                                                                    code =                                                                                    "Unicode"                                                              ;                                                                                                                                                                                                                                interruption                                                              ;                                                                                                                                                                                                                                case                                                                                                                                                  0xfeff                                                              :                                                                                                                                                                    code =                                                                                    "UTF-16BE"                                                              ;                                                                                                                                                                                                                                break                                                              ;                                                                                                                                                                                                                                default                                                              :                                                                                                                                                                    code =                                                                                    "GBK"                                                              ;                                                                                                                      }                                                                                                                                                                                      return                                                                                      code;                                                                                                  }                                                                                          
                                                                                                                                        BufferedInputStream bis =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (is);                                                                                                                                                                    ByteArrayOutputStream buffer =                                                                                    new                                                                                      ByteArrayOutputStream();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    byte                                                              [] data =                                                                                    new                                                                                      byte[                                                              l                                                              ];                                                                                                                                                                                                                                int                                                                                      current =                                                                                    0                                                              ;                                                                                                                                                                                                                                                                                        while                                                              ((current = bis.                                                              read                                                              (data,                                                              0                                                              ,data.length)) != -                                                              ane                                                              ){                                                                                                                                                                    buffer.write(data,                                                              0                                                              ,electric current);                                                                                                                      }                                                                                                                                                                                  FileOutputStream fos =                                                                                    new                                                                                                                                                  FileOutputStream                                                              (file);                                                                                                                      fos.write(buffer.toByteArray());                                                                            fos.close();                                  
                                                                                                                                              public                                                                                                                                                  static                                                                                                                                                  void                                                                                      download(String source_url, File target_file) {                                                                                                                                                                                                                                endeavour                                                                                      {                                                                                                                                                                    ClientConnection connection =                                                                                    new                                                                                      ClientConnection(source_url);                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    OutputStream bone =                                                                                    new                                                                                                                                                  BufferedOutputStream                                                              (                                                              new                                                                                                                                                  FileOutputStream                                                                                                                            (target_file));                                                                                                                                                                                                                                int                                                                                      count;                                                                                                                                                                                                                                byte                                                              [] buffer =                                                                                    new                                                                                      byte[                                                              2048                                                              ];                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                                                                                while                                                                                      ((count = connection.inputStream.                                                              read                                                              (buffer)) >                                                                                    0                                                              ) os.write(buffer,                                                                                    0                                                              , count);                                                                                                                                                                    }                                                                                    catch                                                                                      (IOException east) {                                                                                                                      DAO.severe(due east);                                                                                                                          }                                                                                    finally                                                                                      {                                                                                                                      os.close();                                                                            }                                                                                                                          }                                                                                    catch                                                                                      (IOException e) {                                                                                                                      DAO.severe(eastward);                                                                                                                          }                                                                                    finally                                                                                      {                                                                                                                      connection.close();                                                                            }                                                                                                                          }                                                                                    take hold of                                                                                      (IOException e) {                                                                                                                      DAO.severe(eastward);                                                                            }                                                        }                                                                                          
                                                                                                                                              private                                                                                                                                                  void                                                                                      transToBAOS(BufferedInputStream bi,                                                                                                                                                                    ByteArrayOutputStream bos)                                                                                    throws                                                                                      Exception {                                                                                                                                                                                                                                while                                                                                      (                                                              true                                                              ) {                                                                                                                                                                                                                                synchronized                                                                                      (m_buffer) {                                                                                                                                                                                                                                int                                                                                      amountRead = bi.                                                              read                                                              (m_buffer);                                                                                                                                                                                                                                if                                                                                      (amountRead == -                                                              ane                                                              ) {                                                                                                                                                                                                                                break                                                              ;                                                                                                                      }                                                                                                                          bos.write(m_buffer,                                                                                    0                                                              , amountRead);                                                                                                                      }                                                                            }                                                                                                                                                                                  bi.                                                              close                                                              ();                                                                                                  }                                                                                          
                                                                                                                                                                File file =                                                                                    new                                                                                                                                                  File                                                              (path);                                                                                                                                            int                                                                                      size = (                                                              int                                                              ) file.length();                                                                                                                                            byte                                                              [] bytes =                                                                                    new                                                                                      byte[size];                                                                                                                                            attempt                                                                                      {                                                                                                                                                                    BufferedInputStream buf =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (                                                              new                                                                                                                                                  FileInputStream                                                              (file));                                                                                                                                                                    buf.                                                              read                                                              (bytes,                                                                                    0                                                              , bytes.length);                                                                                                                                                                    buf.                                                              shut                                                              ();                                                                                                                                            }                                                                                    catch                                                                                      (FileNotFoundException e) {                                                                                                                                                                                                                                                                                                                                                                          due east.printStackTrace();                                                                                                  }                                                                                    catch                                                                                      (IOException e) {                                                                                                                                                                                                                                                                                                                                                                          e.printStackTrace();                                                        }                                  
                                                                                                                                                                                                                            boolean                                                                                      running =                                                                                    true                                                              ;                                                                                                                                            BufferedInputStream reader =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (                                                              new                                                                                                                                                  FileInputStream                                                              (                                                                                    "out.txt"                                                                                      ) );                                                                                                                                                                                                    public                                                                                                                                                  void                                                                                      run() {                                                                                                                                                                                                                                while                                                              ( running ) {                                                                                                                                                                                                                                if                                                              ( reader.bachelor() >                                                                                    0                                                                                      ) {                                                                                                                                                                    Organisation.out.print( (                                                              char                                                              )reader.                                                              read                                                              () );                                                                                                                      }                                                                                                                                                                                      else                                                                                      {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    sleep(                                                                                    500                                                                                      );                                                                                                                      }                                                                                                                                                                                      catch                                                              ( InterruptedException ex ) {                                                                                                                                                                    running =                                                                                    false                                                              ;                                                                                                                      }                                                                            }                                                                            }                                                        }                                  
                                                                                                                                        BufferedInputStream bis =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (is);                                                                                                                                                                    ByteArrayBuffer baf =                                                                                    new                                                                                      ByteArrayBuffer(                                                              50                                                              );                                                                                                                                                                                                                                while                                                                                      ((electric current = bis.                                                              read                                                              ()) != -                                                              1                                                              ) {                                                                                                                                                                    baf.append((                                                              byte                                                              ) current);                                                                                                                      }                                                                                                                          FileOutputStream fos =                                                                                    new                                                                                                                                                  FileOutputStream                                                              (file);                                                                                                                      fos.write(buffer.toByteArray());                                  
                                                                                                                                                                BufferedInputStream bis =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (inputStream);                                                                                                                                            bis.marking(                                                              2                                                              );                                                                                                                                            int                                                                                      byte1 = bis.                                                              read                                                              ();                                                                                                                                            int                                                                                      byte2 = bis.                                                              read                                                              ();                                                                                                  bis.reset();                                                                                                                                                                                                  
                                                                                                                                                                    private                                                                                                                                                  static                                                                                                                                                  byte                                                              [] computeSHA256Hash(                                                              byte                                                              [] information)                                                                                    throws                                                                                      NoSuchAlgorithmException, IOException {                                                                                                                                                                    BufferedInputStream bis =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (                                                              new                                                                                      ByteArrayInputStream(data));                                                                                                                                                                                                                                attempt                                                                                      {                                                                                                                                                                    MessageDigest messageDigest = MessageDigest.getInstance(                                                              "SHA-256"                                                              );                                                                                                                                                                                                                                byte                                                              [] buffer =                                                                                    new                                                                                      byte[                                                              16384                                                              ];                                                                                                                                                                                                                                int                                                                                      bytesRead = -                                                              i                                                              ;                                                                                                                                                                                                                                while                                                                                      ( (bytesRead = bis.                                                              read                                                              (buffer,                                                                                    0                                                              , buffer.length)) != -                                                              one                                                                                      ) {                                                                                                                                                                    messageDigest.update(buffer,                                                                                    0                                                              , bytesRead);                                                                                                                      }                                                                                                                                                                                      return                                                                                      messageDigest.assimilate();                                                                                                                                                                    }                                                                                    finally                                                                                      {                                                                                                                                                                                                                                endeavour                                                                                      { bis.                                                              close                                                              (); }                                                                                    take hold of                                                                                      ( Exception e ) {}                                                                                                                      }                                                                            }                                                        }                                  
                                                                                                                                              private                                                                                                                                                  void                                                                                      writeEntry(BufferedInputStream bis, ZipEntry ze)                                                                                    throws                                                                                      IOException {                                                                                                                                                                                                                                byte                                                              [] buf =                                                                                    new                                                                                      byte[BUFFER_SIZE];                                                                                                                      mZos.putNextEntry(ze);                                                                                                                                                                                      int                                                                                      len;                                                                                                                                                                                                                                while                                                                                      ((len = bis.                                                              read                                                              (buf,                                                                                    0                                                              , BUFFER_SIZE)) != -                                                              1                                                              ) {                                                                                                                                                                    mZos.write(buf,                                                                                    0                                                              , len);                                                                                                                      }                                                                            mZos.closeEntry();                                                                                                                          bis.                                                              close                                                              ();                                                                                                  }                                                                                          
                                                                                                                                              individual                                                                                                                                                  byte                                                              [] fileDigest(File file)                                                                                    throws                                                                                      IOException {                                                                                                                                                                    BufferedInputStream fis =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (                                                              new                                                                                                                                                  FileInputStream                                                                                                                            (file));                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                                                                                byte                                                              [] data =                                                                                    new                                                                                      byte[                                                              8192                                                              ];                                                                                                                                                                                                                                int                                                                                      read;                                                                                                                                                                                                                                while                                                                                      ((read = fis.                                                              read                                                              (data)) >                                                                                    0                                                              ) {                                                                                                                                                                    messageDigest.update(data,                                                                                    0                                                              , read);                                                                                                                      }                                                                                                                          }                                                                                    finally                                                                                      {                                                                                                                                                                    fis.                                                              close                                                              ();                                                                                                                      }                                                                                                                                                                                                                                              return                                                                                      messageDigest.digest();                                                                                                  }                                                                                          
                                      @Override                                                                                                                                                                byte                                                              [] readFile(File file)                                                                                    throws                                                                                      IOException {                                                                                                                                                                    InputStream is =                                                                                    new                                                                                                                                                  FileInputStream                                                              (file);                                                                                                                                                                    BufferedInputStream buff =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (is);                                                                                                                                                                    ByteArrayOutputStream os =                                                                                    new                                                                                      ByteArrayOutputStream();                                                                                                                                                                                                                                byte                                                              [] buffer =                                                                                    new                                                                                      byte[                                                              0xFFFF                                                              ];                                                                                                                                                                                                                                int                                                                                      len;                                                                                                                                                                                                                                while                                                                                      ((len = buff.                                                              read                                                              (buffer)) >                                                                                    0                                                              ) {                                                                                                                                                                    os.write(buffer,                                                                                    0                                                              , len);                                                                                                                      }                                                                            os.flush();                                                                            close(vitrify);                                                                                                                                                                                      return                                                                                      bone.toByteArray();                                                                                                  }                                                                                          
                                                          @Override                                                                                                                                                                                      public                                                                                                                                                  void                                                                                      processStream(InputStream input)                                                                                    throws                                                                                      IOException {                                                                                                                                                                    BufferedInputStream  in =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (input);                                                                                                                                                                                                                                                                                        byte                                                              [] buffer =                                                                                    new                                                                                      byte[                                                              8092                                                              ];                                                                                                                                                                                                                                int                                                                                      len;                                                                                                                                                                                                                                                                                        while                                                                                      ((len = in.                                                              read                                                              (buffer)) != -                                                              i                                                              ) {                                                                                                                                                                                                                                if                                                                                      (len >                                                                                    0                                                              ) {                                                                                                                                                                    bytes.write(buffer,                                                                                    0                                                              , len);                                                                                                                      }                                                                            }                                                                            }                                                        }                                  
                                                                                                                                              public                                                                                                                                                  static                                                                                                                                                  int                                                                                      copyAndCloseInputStream(InputStream inputStream, OutputStream outputStream)                                                                                                                                                                                                                                throws                                                                                      IOException {                                                                                                                      BufferedInputStream bufferedInputStream = zilch;                                                                                                                                                                                      int                                                                                      totalBytes =                                                                                    0                                                              ;                                                                                                                                                                                                                                endeavour                                                                                      {                                                                                                                                                                    bufferedInputStream =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (inputStream);                                                                                                                                                                                                                                                                                        byte                                                              [] buffer =                                                                                    new                                                                                      byte[                                                              8192                                                              ];                                                                                                                                                                                                                                int                                                                                      bytesRead;                                                                                                                                                                                                                                while                                                                                      ((bytesRead = bufferedInputStream.                                                              read                                                              (buffer)) != -                                                              1                                                              ) {                                                                                                                                                                    outputStream.write(buffer,                                                                                    0                                                              , bytesRead);                                                                                                                      totalBytes += bytesRead;                                                                            }                                                                                                                          }                                                                                    finally                                                                                      {                                                                                                                                                                                                                                if                                                                                      (bufferedInputStream != null) {                                                                                                                                                                    bufferedInputStream.                                                              close                                                              ();                                                                                                                      }                                                                                                                                                                                      if                                                                                      (inputStream != null) {                                                                                                                      inputStream.close();                                                                            }                                                                            }                                                                                                                                                                                                                                              return                                                                                      totalBytes;                                                                                                  }                                                                                          
                                      @Override                                                                                                                                                                protected                                                                                                                                                  byte                                                              [] readValueFromDisk(File file)                                                                                    throws                                                                                      IOException {                                                                                                                                                                    BufferedInputStream istream =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (                                                              new                                                                                                                                                  FileInputStream                                                                                                                            (file));                                                                                                                                                                                                                                long                                                                                      fileSize = file.length();                                                                                                                                                                                                                                if                                                                                      (fileSize > Integer.MAX_VALUE) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                      IOException(                                                              "Cannot read files larger than "                                                                                      + Integer.MAX_VALUE +                                                                                    " bytes"                                                              );                                                                                                                      }                                                                                                                                                                                                                                              int                                                                                      imageDataLength = (                                                              int                                                              ) fileSize;                                                                                                                                                                                                                                                                                        byte                                                              [] imageData =                                                                                    new                                                                                      byte[imageDataLength];                                                                                                                                                                    istream.                                                              read                                                              (imageData,                                                                                    0                                                              , imageDataLength);                                                                                                                                                                    istream.                                                              close                                                              ();                                                                                                                                                                                                                                                                                        return                                                                                      imageData;                                                                                                  }                                                                                          
                                      @Override                                                                                                                                                                byte                                                              [] readFile(File file)                                                                                    throws                                                                                      IOException {                                                                                                                                                                    InputStream is =                                                                                    new                                                                                                                                                  FileInputStream                                                              (file);                                                                                                                                                                    ZipInputStream zis =                                                                                    new                                                                                      ZipInputStream(is);                                                                                                                      zis.getNextEntry();                                                                                                                          BufferedInputStream buff =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (zis);                                                                                                                                                                    ByteArrayOutputStream os =                                                                                    new                                                                                      ByteArrayOutputStream();                                                                                                                                                                                                                                byte                                                              [] buffer =                                                                                    new                                                                                      byte[                                                              0xFFFF                                                              ];                                                                                                                                                                                                                                int                                                                                      len;                                                                                                                                                                                                                                while                                                                                      ((len = vitrify.                                                              read                                                              (buffer)) >                                                                                    0                                                              ) {                                                                                                                                                                    os.write(buffer,                                                                                    0                                                              , len);                                                                                                                      }                                                                            bone.flush();                                                                            Helper.close(vitrify);                                                                                                                                                                                      render                                                                                      bone.toByteArray();                                                                                                  }                                                                                          
                                                                                                                                        BufferedInputStream in =                                                                                    new                                                                                                                                                  BufferedInputStream                                                              (                                                              new                                                                                      URL(                                                              "http://world wide web.website.com/information.asp"                                                              ).openStream())                                                                                                                                                                                                                                byte                                                                                      information[] =                                                                                    new                                                                                      byte[                                                              1024                                                              ];                                                                                                                                                                                                                                int                                                                                      count;                                                                                                                                                                                                                                while                                                              ((count = in.                                                              read                                                              (information,                                                              0                                                              ,                                                              1024                                                              )) != -                                                              i                                                              )                                                                                                                      {                                                                                                                          out.write(information,                                                                                    0                                                              , count);                                                                                                                      }                                  

borkholderuposecushers.blogspot.com

Source: https://www.tabnine.com/code/java/methods/java.io.BufferedInputStream/read

Post a Comment for "Reading Unknown Length of Byte[] Inputstream in Java"