How to read data byte by byte in java code examples

public  void readDataByteByByte( byte[] data,int contentLength,InputStream raw){
        InputStream in = new BufferedInputStream(raw);
        int offset = 0;
        try{
        int bytesRead = 0;
        while (offset < contentLength) {
          bytesRead = in.read(data, offset, data.length - offset);
          if (bytesRead == -1)
            break;
          offset += bytesRead;
        }
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            if (in!=null)
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        if (offset != contentLength) {
              try {
                throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
      
        //return data;
       
    }

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

Shallow Copy Deep Copy Java example

Database Connection using NODE JS example