Example usage for com.amazonaws.services.s3.model S3ObjectInputStream read

List of usage examples for com.amazonaws.services.s3.model S3ObjectInputStream read

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model S3ObjectInputStream read.

Prototype

@Override
    public int read(byte b[], int off, int len) throws IOException 

Source Link

Usage

From source file:c3.ops.priam.backup.RangeReadInputStream.java

License:Apache License

public int read(final byte b[], final int off, final int len) throws IOException {
    //        logger.info(String.format("incoming buf req's size = %d, off = %d, len to read = %d, on file size %d, cur offset = %d path = %s",
    //                b.length, off, len, path.getSize(), offset, path.getRemotePath()));
    final long fileSize = path.getSize();
    if (fileSize > 0 && offset >= fileSize)
        return -1;
    final long firstByte = offset;
    long curEndByte = firstByte + len;
    curEndByte = curEndByte <= fileSize ? curEndByte : fileSize;

    //need to subtract one as the call to getRange is inclusive
    //meaning if you want to download the first 10 bytes of a file, request bytes 0..9
    final long endByte = curEndByte - 1;
    //        logger.info(String.format("start byte = %d, end byte = %d", firstByte, endByte));
    try {//from w w w .  j  av a2  s . c  o  m
        Integer cnt = new RetryableCallable<Integer>() {
            public Integer retriableCall() throws IOException {
                GetObjectRequest req = new GetObjectRequest(bucketName, path.getRemotePath());
                req.setRange(firstByte, endByte);
                S3ObjectInputStream is = null;
                try {
                    is = s3Client.getObject(req).getObjectContent();

                    byte[] readBuf = new byte[4092];
                    int rCnt;
                    int readTotal = 0;
                    int incomingOffet = off;
                    while ((rCnt = is.read(readBuf, 0, readBuf.length)) >= 0) {
                        System.arraycopy(readBuf, 0, b, incomingOffet, rCnt);
                        readTotal += rCnt;
                        incomingOffet += rCnt;
                        //                            logger.info("    local read cnt = " + rCnt + "Current Thread Name = "+Thread.currentThread().getName());
                    }
                    if (readTotal == 0 && rCnt == -1)
                        return -1;
                    offset += readTotal;
                    return Integer.valueOf(readTotal);
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        }.call();
        //            logger.info("read cnt = " + cnt);
        return cnt.intValue();
    } catch (Exception e) {
        String msg = String.format("failed to read offset range %d-%d of file %s whose size is %d", firstByte,
                endByte, path.getRemotePath(), path.getSize());
        throw new IOException(msg, e);
    }
}