/** * Writes the contents of the response to the specified File. * * @param outputFile the output file to write to * @throws FileNotFoundException if unable to write to {@code outputFile} * @throws IOException if unable to read the response contents */ public void saveToFile(String outputFile) throws FileNotFoundException, IOException { Streams.copy(getInputStream(), new BufferedOutputStream(new FileOutputStream(outputFile))); }
/** * Reads all input into memory, close the steam, and return as a String. Reads * the input in the default charset: Charset.defaultCharset(). * * @param inputStream InputStream to read from. * @return String contents of the stream. * @throws IOException if there is an problem reading from the stream. * @deprecated it is always safer to supply a charset. See * {@link #readAll(InputStream, Charset)}. */ @Deprecated public static String readAll(final InputStream inputStream) throws IOException { return readAll(inputStream, Charset.defaultCharset()); }
/** * Wraps the provided string into an InputStream in the default charset: * Charset.defaultCharset(). * * @param str String to wrap. * @return InputStream containing the string argument. * @deprecated it is always safer to supply a charset. See * {@link #wrapString(String, Charset)}. */ @Deprecated public static InputStream wrapString(String str) { return wrapString(str, Charset.defaultCharset()); }
/** * Writes the specified string to stream with buffering and closes the stream. * Writes in the default charset: Charset.defaultCharset(). * * @param str String to write. * @param outputStream Stream to write to. * @throws IOException If there is an exception while writing. * @deprecated it is always safer to supply a charset. See * {@link #write(String, OutputStream, Charset)}. */ @Deprecated public static void write(String str, final OutputStream outputStream) throws IOException { write(str, outputStream, Charset.defaultCharset()); }
/** * Writes the specified string to stream with buffering and closes the stream. * Writes in the default charset: Charset.defaultCharset(). * * @param str String to write. * @param outputStream Stream to write to. * @throws IOException If there is an exception while writing. * @deprecated it is always safer to supply a charset. See * {@link #write(String, OutputStream, Charset)}. */ @Deprecated public static void write(String str, final OutputStream outputStream) throws IOException { write(str, outputStream, Charset.defaultCharset()); }
/** * Reads all input into memory, close the steam, and return as a String. Reads * the input in the default charset: Charset.defaultCharset(). * * @param inputStream InputStream to read from. * @return String contents of the stream. * @throws IOException if there is an problem reading from the stream. * @deprecated it is always safer to supply a charset. See * {@link #readAll(InputStream, Charset)}. */ @Deprecated public static String readAll(final InputStream inputStream) throws IOException { return readAll(inputStream, Charset.defaultCharset()); }
/** * Writes the contents of the response to the specified File. * * @param outputFile the output file to write to * @throws FileNotFoundException if unable to write to {@code outputFile} * @throws IOException if unable to read the response contents */ public void saveToFile(String outputFile) throws FileNotFoundException, IOException { Streams.copy(getInputStream(), new BufferedOutputStream(new FileOutputStream(outputFile))); }
/** * Wraps the provided string into an InputStream in the default charset: * Charset.defaultCharset(). * * @param str String to wrap. * @return InputStream containing the string argument. * @deprecated it is always safer to supply a charset. See * {@link #wrapString(String, Charset)}. */ @Deprecated public static InputStream wrapString(String str) { return wrapString(str, Charset.defaultCharset()); }
/** * Test method for {@link com.google.api.ads.common.lib.utils.Streams#write(java.lang.String, * java.io.OutputStream, java.nio.charset.Charset)}. */ @Test public void testWriteStringOutputStream_wrongCharset() throws Exception { String testString = "Testing©ÿ»"; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Streams.write(testString, outputStream, Charset.forName(UTF_8)); byte[] stringBytes = testString.getBytes(Charset.forName(ISO_8859_1)); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); assertFalse( "Byte arrays should be not equal", Arrays.equals(stringBytes, outputStream.toByteArray())); String actual = new String(outputStream.toByteArray(), Charset.forName(UTF_8)); assertEquals(testString, actual); }
/** * Returns an OAuth response that will contain the specified values. */ public static String getTestOAuthResponse(String accessToken, Long expirationInSeconds, String refreshToken) throws IOException { String response = Streams.readAll( AuthResponseProvider.class.getResourceAsStream("test_oauth_response.txt"), Charset.forName(UTF_8)); return response.replace("#ACCESS_TOKEN#", accessToken) .replace("#EXPIRATION_SECONDS#", expirationInSeconds.toString()) .replace("#REFRESH_TOKEN#", refreshToken); } }
Streams.copy(gzipReportStream, new FileOutputStream(reportFile)); logger.debug("... " + reportFile.getPath() + " copied successfully.");
/** * Test method for * {@link com.google.api.ads.common.lib.utils.Streams#wrapString(String, Charset)}. */ @Test public void testWrapString_wrongCharset() throws Exception { String testString = "Testing©ÿ»"; final InputStream inputStream = Streams.wrapString(testString, Charset.forName(UTF_8)); byte[] stringBytes = testString.getBytes(Charset.forName(ISO_8859_1)); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); assertFalse("Streams should not be equal", Arrays.equals(stringBytes, ByteStreams.toByteArray(inputStream))); }
/** * Test method for {@link com.google.api.ads.common.lib.utils.Streams#write(java.lang.String, * java.io.OutputStream, java.nio.charset.Charset)}. */ @Test public void testWriteStringOutputStream_sameCharset() throws Exception { String testString = "Testing©ÿ»"; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Streams.write(testString, outputStream, Charset.forName(UTF_8)); byte[] stringBytes = testString.getBytes(Charset.forName(UTF_8)); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); assertTrue( "Byte arrays shoud be equal", Arrays.equals(stringBytes, outputStream.toByteArray())); String actual = new String(outputStream.toByteArray(), Charset.forName(UTF_8)); assertEquals(testString, actual); }
/** * Returns the contents of the response as a String. Inflates the response if it is in one of the * gzip formats. * * @throws IOException if unable to read the response contents */ public String getAsString() throws IOException { InputStream inputStream = isGzipped() ? new GZIPInputStream(getInputStream()) : getInputStream(); return Streams.readAll(inputStream, rawResponse.getCharset()); }
/** * Test method for {@link com.google.api.ads.common.lib.utils.Streams#copy(java.io.InputStream, * java.io.OutputStream)}. */ @Test public void testCopy() throws Exception { String testString = "Testing©ÿ»"; byte[] stringBytes = testString.getBytes(); InputStream inputStream = new ByteArrayInputStream(Arrays.copyOf(stringBytes, stringBytes.length)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Streams.copy(inputStream, outputStream); assertTrue( "Byte arrays should be equal", Arrays.equals(stringBytes, outputStream.toByteArray())); } }
/** * Test method for * {@link com.google.api.ads.common.lib.utils.Streams#wrapString(String, Charset)}. */ @Test public void testWrapString_sameCharset() throws Exception { String testString = "Testing©ÿ»"; final InputStream inputStream = Streams.wrapString(testString, Charset.forName(UTF_8)); byte[] stringBytes = testString.getBytes(Charset.forName(UTF_8)); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); assertTrue("Streams should be equal", Arrays.equals(stringBytes, ByteStreams.toByteArray(inputStream))); }
/** * Test method for {@link com.google.api.ads.common.lib.utils.Streams#write(java.lang.String, * java.io.OutputStream)}. */ @SuppressWarnings({"javadoc", "deprecation"}) @Test public void testWriteStringOutputStream() throws Exception { String testString = "Testing©ÿ»"; byte[] stringBytes = testString.getBytes(); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Streams.write(testString, outputStream); outputStream.close(); String expectedTestString = new String(testString.getBytes()); String actual = new String(outputStream.toByteArray()); assertEquals(expectedTestString, actual); byte[] actualBytes = Arrays.copyOf(outputStream.toByteArray(), stringBytes.length); assertTrue( "Byte arrays should be equal", Arrays.equals(stringBytes, actualBytes)); }
public static String getTestBatchUploadRequest(String apiVersion) throws IOException { String soapXml = Streams.readAll( SoapRequestXmlProvider.class.getResourceAsStream("test_batch_upload_request.xml"), Charset.forName(UTF_8)); return SoapXmlTemplater.templateResponse(soapXml, apiVersion); }
/** * Test method for * {@link com.google.api.ads.adwords.lib.utils.ReportDownloadResponse#getAsString()} * when response data is compressed. */ @Test public void testGetAsString_gzippedCSV() throws IOException { String expectedContents = "a,b,c\nd,e,f\n"; ByteArrayOutputStream zippedBytesOut = new ByteArrayOutputStream(); Streams.copy(new ByteArrayInputStream(expectedContents.getBytes(UTF_8)), new GZIPOutputStream(zippedBytesOut)); RawReportDownloadResponse rawResponse = new RawReportDownloadResponse(HttpURLConnection.HTTP_OK, new ByteArrayInputStream(zippedBytesOut.toByteArray()), UTF_8, "GZIPPED_CSV"); ReportDownloadResponse response = new ReportDownloadResponse(rawResponse); assertEquals("contents as string are incorrect for gzipped format", expectedContents, response.getAsString()); }
/** * Test method for * {@link com.google.api.ads.common.lib.utils.Streams#wrapString(String)}. */ @SuppressWarnings({"javadoc", "deprecation"}) @Test public void testWrapString() throws Exception { String testString = "Testing©ÿ»"; final InputStream inputStream = Streams.wrapString(testString); byte[] stringBytes = testString.getBytes(Charset.defaultCharset()); stringBytes = Arrays.copyOf(stringBytes, stringBytes.length); assertTrue("Streams should be equal", Arrays.equals(stringBytes, ByteStreams.toByteArray(inputStream))); }