@Test public void testDownloadFileLarge() throws IOException { setupForDownloadFileTests(); final States state = context.states("readStep").startsAs("1"); context.checking(new Expectations() { { oneOf(httpReader).read(with(any(char[].class)), with(any(int.class)), with(any(int.class))); when(state.is("1")); then(state.is("2")); will(new ReadAction("ABCDEFGHIJ")); oneOf(httpReader).read(with(any(char[].class)), with(any(int.class)), with(any(int.class))); when(state.is("2")); then(state.is("3")); will(new ReadAction("abcdefghij")); oneOf(httpReader).read(with(any(char[].class))); when(state.is("3")); then(state.is("4")); will(new ReadAction("MMM")); oneOf(httpReader).read(with(any(char[].class))); when(state.is("4")); then(state.is("5")); will(new ReadAction("ZZZ")); oneOf(httpReader).read(with(any(char[].class))); when(state.is("5")); then(state.is("6")); will(returnValue(-1)); oneOf(httpReader).close(); when(state.is("6")); then(state.is("E")); } }); assertEquals("ABCDEFGHIJabcdefghijMMMZZZ", sut.downloadFile(URL, ACCOUNT_ID)); context.assertIsSatisfied(); }
public String downloadFile(final String url, final String inAccountId) throws IOException { char[] buffer = new char[expectedDownloadFileLimit]; Reader reader = getConnectionReader(url, inAccountId); try { int charsRead = 0; while (charsRead < buffer.length) { int thisRead = reader.read(buffer, charsRead, buffer.length - charsRead); if (thisRead < 0) { return new String(buffer, 0, charsRead); } charsRead += thisRead; } StringBuilder builder = new StringBuilder(); do { if (builder.length() + charsRead > maximumDownloadFileLimit) { throw new IOException("Downloaded file too large."); } builder.append(buffer, 0, charsRead); charsRead = reader.read(buffer); } while (charsRead >= 0); return builder.toString(); } finally { reader.close(); } }
ConnectionFacade { public String downloadFile(final String url, final String inAccountId) throws IOException { char[] buffer = new char[expectedDownloadFileLimit]; Reader reader = getConnectionReader(url, inAccountId); try { int charsRead = 0; while (charsRead < buffer.length) { int thisRead = reader.read(buffer, charsRead, buffer.length - charsRead); if (thisRead < 0) { return new String(buffer, 0, charsRead); } charsRead += thisRead; } StringBuilder builder = new StringBuilder(); do { if (builder.length() + charsRead > maximumDownloadFileLimit) { throw new IOException("Downloaded file too large."); } builder.append(buffer, 0, charsRead); charsRead = reader.read(buffer); } while (charsRead >= 0); return builder.toString(); } finally { reader.close(); } } }
ConnectionFacade { public String downloadFile(final String url, final String inAccountId) throws IOException { char[] buffer = new char[expectedDownloadFileLimit]; Reader reader = getConnectionReader(url, inAccountId); try { int charsRead = 0; while (charsRead < buffer.length) { int thisRead = reader.read(buffer, charsRead, buffer.length - charsRead); if (thisRead < 0) { return new String(buffer, 0, charsRead); } charsRead += thisRead; } StringBuilder builder = new StringBuilder(); do { if (builder.length() + charsRead > maximumDownloadFileLimit) { throw new IOException("Downloaded file too large."); } builder.append(buffer, 0, charsRead); charsRead = reader.read(buffer); } while (charsRead >= 0); return builder.toString(); } finally { reader.close(); } } ConnectionFacade(final List<ConnectionFacadeDecorator> inDecorators); }
ConnectionFacade { public String downloadFile(final String url, final String inAccountId) throws IOException { char[] buffer = new char[expectedDownloadFileLimit]; Reader reader = getConnectionReader(url, inAccountId); try { int charsRead = 0; while (charsRead < buffer.length) { int thisRead = reader.read(buffer, charsRead, buffer.length - charsRead); if (thisRead < 0) { return new String(buffer, 0, charsRead); } charsRead += thisRead; } StringBuilder builder = new StringBuilder(); do { if (builder.length() + charsRead > maximumDownloadFileLimit) { throw new IOException("Downloaded file too large."); } builder.append(buffer, 0, charsRead); charsRead = reader.read(buffer); } while (charsRead >= 0); return builder.toString(); } finally { reader.close(); } } ConnectionFacade(final List<ConnectionFacadeDecorator> inDecorators); final List<Integer> getRedirectCodes(); final void setRedirectCodes(final List<Integer> inRedirectCodes); final String getProxyPort(); final void setProxyPort(final String inProxyPort); final String getProxyHost(); final void setProxyHost(final String inProxyHost); final int getConnectionTimeOut(); final void setConnectionTimeOut(final int inConnectionTimeOut); String downloadFile(final String url, final String inAccountId); int getImgHeight(final String url, final String inAccountId); int getImgWidth(final String url, final String inAccountId); String getHost(final String url); String getProtocol(final String url); String getPath(final String url); String getFinalUrl(final String url, final String inAccountId); int getExpectedDownloadFileLimit(); void setExpectedDownloadFileLimit(final int inExpectedDownloadFileLimit); int getMaximumDownloadFileLimit(); void setMaximumDownloadFileLimit(final int inMaximumDownloadFileLimit); }
ConnectionFacade { public String downloadFile(final String url, final String inAccountId) throws IOException { char[] buffer = new char[expectedDownloadFileLimit]; Reader reader = getConnectionReader(url, inAccountId); try { int charsRead = 0; while (charsRead < buffer.length) { int thisRead = reader.read(buffer, charsRead, buffer.length - charsRead); if (thisRead < 0) { return new String(buffer, 0, charsRead); } charsRead += thisRead; } StringBuilder builder = new StringBuilder(); do { if (builder.length() + charsRead > maximumDownloadFileLimit) { throw new IOException("Downloaded file too large."); } builder.append(buffer, 0, charsRead); charsRead = reader.read(buffer); } while (charsRead >= 0); return builder.toString(); } finally { reader.close(); } } ConnectionFacade(final List<ConnectionFacadeDecorator> inDecorators); final List<Integer> getRedirectCodes(); final void setRedirectCodes(final List<Integer> inRedirectCodes); final String getProxyPort(); final void setProxyPort(final String inProxyPort); final String getProxyHost(); final void setProxyHost(final String inProxyHost); final int getConnectionTimeOut(); final void setConnectionTimeOut(final int inConnectionTimeOut); String downloadFile(final String url, final String inAccountId); int getImgHeight(final String url, final String inAccountId); int getImgWidth(final String url, final String inAccountId); String getHost(final String url); String getProtocol(final String url); String getPath(final String url); String getFinalUrl(final String url, final String inAccountId); int getExpectedDownloadFileLimit(); void setExpectedDownloadFileLimit(final int inExpectedDownloadFileLimit); int getMaximumDownloadFileLimit(); void setMaximumDownloadFileLimit(final int inMaximumDownloadFileLimit);  }