assertEquals("getPos should return 1 after reading one byte", 1, bris.getPos()); verify(oMock, times(1)).connect(0, false); bris.getPos()); verify(oMock, times(1)).connect(0, false); rMock.setURL(new URL("http://resolvedurl/")); assertEquals("getPos should return 101 after reading one byte", 101, bris.getPos()); verify(rMock, times(1)).connect(100, true); verify(rMock, times(1)).connect(100, true); verify(rMock, times(0)).connect(101, true); .when(rMock).connect(anyLong(), anyBoolean()); bris.seek(500); try {
assertEquals("getPos should return 1 after reading one byte", 1, bris.getPos()); verify(oMock, times(1)).connect(0, false); bris.getPos()); verify(oMock, times(1)).connect(0, false); rMock.setURL(new URL("http://resolvedurl/")); assertEquals("getPos should return 101 after reading one byte", 101, bris.getPos()); verify(rMock, times(1)).connect(100, true); verify(rMock, times(1)).connect(100, true); verify(rMock, times(0)).connect(101, true); .when(rMock).connect(anyLong(), anyBoolean()); bris.seek(500); try {
@VisibleForTesting protected InputStreamAndFileLength openInputStream(long startOffset) throws IOException { // Use the original url if no resolved url exists, eg. if // it's the first time a request is made. final boolean resolved = resolvedURL.getURL() != null; final URLOpener opener = resolved? resolvedURL: originalURL; final HttpURLConnection connection = opener.connect(startOffset, resolved); resolvedURL.setURL(getResolvedUrl(connection)); InputStream in = connection.getInputStream(); final Long length; final Map<String, List<String>> headers = connection.getHeaderFields(); if (isChunkedTransferEncoding(headers)) { // file length is not known length = null; } else { // for non-chunked transfer-encoding, get content-length long streamlength = getStreamLength(connection, headers); length = startOffset + streamlength; // Java has a bug with >2GB request streams. It won't bounds check // the reads so the transfer blocks until the server times out in = new BoundedInputStream(in, streamlength); } return new InputStreamAndFileLength(length, in); }
@VisibleForTesting protected InputStream openInputStream() throws IOException { // Use the original url if no resolved url exists, eg. if // it's the first time a request is made. final boolean resolved = resolvedURL.getURL() != null; final URLOpener opener = resolved? resolvedURL: originalURL; final HttpURLConnection connection = opener.connect(startPos, resolved); resolvedURL.setURL(getResolvedUrl(connection)); InputStream in = connection.getInputStream(); final Map<String, List<String>> headers = connection.getHeaderFields(); if (isChunkedTransferEncoding(headers)) { // file length is not known fileLength = null; } else { // for non-chunked transfer-encoding, get content-length long streamlength = getStreamLength(connection, headers); fileLength = startPos + streamlength; // Java has a bug with >2GB request streams. It won't bounds check // the reads so the transfer blocks until the server times out in = new BoundedInputStream(in, streamlength); } return in; }
private ByteRangeInputStream.URLOpener getMockURLOpener(URL url) throws IOException { ByteRangeInputStream.URLOpener opener = mock(ByteRangeInputStream.URLOpener.class, CALLS_REAL_METHODS); opener.setURL(url); doReturn(getMockConnection("65535")) .when(opener).connect(anyLong(), anyBoolean()); return opener; }
private ByteRangeInputStream.URLOpener getMockURLOpener(URL url) throws IOException { ByteRangeInputStream.URLOpener opener = mock(ByteRangeInputStream.URLOpener.class, CALLS_REAL_METHODS); opener.setURL(url); doReturn(getMockConnection("65535")) .when(opener).connect(anyLong(), anyBoolean()); return opener; }