protected DataInputStream getDataInputStream()

in commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5RandomAccessContent.java [63:119]


    protected DataInputStream getDataInputStream() throws IOException {
        if (dataInputStream != null) {
            return dataInputStream;
        }

        final HttpGet httpGet = new HttpGet(fileObject.getInternalURI());
        httpGet.setHeader("Range", "bytes=" + filePointer + "-");
        final ClassicHttpResponse httpResponse = fileObject.executeHttpUriRequest(httpGet);
        final int status = httpResponse.getCode();

        if (status != HttpURLConnection.HTTP_PARTIAL && status != HttpURLConnection.HTTP_OK) {
            throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
                    Long.valueOf(filePointer), Integer.valueOf(status));
        }

        monitorInputStream = new MonitoredHttpResponseContentInputStream(httpResponse);

        // If the range request was ignored
        if (status == HttpURLConnection.HTTP_OK) {
            final long skipped = monitorInputStream.skip(filePointer);
            if (skipped != filePointer) {
                throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
                        Long.valueOf(filePointer), Integer.valueOf(status));
            }
        }

        dataInputStream = new DataInputStream(new FilterInputStream(monitorInputStream) {
            @Override
            public int read() throws IOException {
                final int ret = super.read();
                if (ret > -1) {
                    filePointer++;
                }
                return ret;
            }

            @Override
            public int read(final byte[] b) throws IOException {
                final int ret = super.read(b);
                if (ret > -1) {
                    filePointer += ret;
                }
                return ret;
            }

            @Override
            public int read(final byte[] b, final int off, final int len) throws IOException {
                final int ret = super.read(b, off, len);
                if (ret > -1) {
                    filePointer += ret;
                }
                return ret;
            }
        });

        return dataInputStream;
    }