Search code examples
javastringamazon-s3inputstream

How to get the value from the s3 input stream in java


I am trying to get value here :

  public char getTs() throws IOException {
   S3ObjectInputStream ts = null;
    int i;
    ts = (S3ObjectInputStream) s3Services.getResourceStream(fileURL.toString());
    while ((i = ts.read()) != -1) {
        c = (char) i;
        LOGGER.info("req time {} ", c);
    }
    return c;
}

The value I am getting is in bits, how to get the whole value at once? Is it suitable to use an array?

My Output :req time 0
           req time 1
           req time -
           req time 0
           req time 1.... and so on

My expected output is :req time 01-01-2000 03:10:10


Solution

  • You could wrap the S3ObjectInputStream within an InputStreamReader and the InputStreamReader within a BufferedInputStream. That way you can read the object line by line:

    var reader = new BufferedReader(new InputStreamReader(ts));
    var line = reader.readLine();
    

    Also check out Apache Commons IO which provide additional convenient streams and readers and utilities.