Java Code Examples for com.google.api.client.util.IOUtils#copy()
The following examples show how to use
com.google.api.client.util.IOUtils#copy() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MockLowLevelHttpRequest.java From google-http-java-client with Apache License 2.0 | 6 votes |
/** * Returns HTTP content as a string, taking care of any encodings of the content if necessary. * * <p>Returns an empty string if there is no HTTP content. * * @since 1.12 */ public String getContentAsString() throws IOException { if (getStreamingContent() == null) { return ""; } // write content to a byte[] ByteArrayOutputStream out = new ByteArrayOutputStream(); getStreamingContent().writeTo(out); // determine gzip encoding String contentEncoding = getContentEncoding(); if (contentEncoding != null && contentEncoding.contains("gzip")) { InputStream contentInputStream = new GZIPInputStream(new ByteArrayInputStream(out.toByteArray())); out = new ByteArrayOutputStream(); IOUtils.copy(contentInputStream, out); } // determine charset parameter from content type String contentType = getContentType(); HttpMediaType mediaType = contentType != null ? new HttpMediaType(contentType) : null; Charset charset = mediaType == null || mediaType.getCharsetParameter() == null ? Charsets.ISO_8859_1 : mediaType.getCharsetParameter(); return out.toString(charset.name()); }
Example 2
Source File: MastodonHttpUtilities.java From data-transfer-project with Apache License 2.0 | 5 votes |
private String requestRaw(String path) throws IOException { HttpRequest getRequest = TRANSPORT.createRequestFactory().buildGetRequest( new GenericUrl(baseUrl + path)); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("Bearer " + accessToken); getRequest.setHeaders(headers); HttpResponse response = getRequest.execute(); validateResponse(getRequest, response, 200); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(response.getContent(), byteArrayOutputStream, true); return byteArrayOutputStream.toString(); }
Example 3
Source File: ByteArrayContentTest.java From google-http-java-client with Apache License 2.0 | 5 votes |
public void subtestConstructor(ByteArrayContent content, String expected) throws IOException { assertEquals("type", content.getType()); assertTrue(content.retrySupported()); assertEquals(expected.length(), content.getLength()); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(content.getInputStream(), out); assertEquals(expected, out.toString()); }
Example 4
Source File: BuyerServiceHelper.java From googleads-adxbuyer-examples with Apache License 2.0 | 5 votes |
/** * reads results from the response object and converts them to a String * @param response you want to read content from * @return the response content as a String * @throws IOException */ protected static String getResultAsString(HttpResponse response) throws IOException { InputStream content = response.getContent(); if (content == null) { return ""; } ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(content, out); String json = out.toString(response.getContentCharset().name()); return json; }
Example 5
Source File: HttpResponseUtils.java From android-oauth-client with Apache License 2.0 | 5 votes |
static String parseAsStringWithoutClosing(HttpResponse response) throws IOException { InputStream content = response.getContent(); if (content == null) { return ""; } ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(content, out, false); return out.toString(response.getContentCharset().name()); }
Example 6
Source File: DicomStreamUtilTest.java From healthcare-dicom-dicomweb-adapter with Apache License 2.0 | 4 votes |
@Override public void copyTo(OutputStream out) throws IOException { IOUtils.copy(this.in, out); }
Example 7
Source File: NFSService.java From modeldb with Apache License 2.0 | 4 votes |
/** * Upload multipart file into respected artifact path * * @param artifactPath : artifact path * @param uploadedFileInputStream : uploaded file input stream * @return {@link String} : upload filename * @throws ModelDBException ModelDBException */ String storeFile(String artifactPath, InputStream uploadedFileInputStream) throws ModelDBException { LOGGER.trace("NFSService - storeFile called"); try { String cleanArtifactPath = StringUtils.cleanPath(Objects.requireNonNull(artifactPath)); String[] folders = cleanArtifactPath.split("/"); StringBuilder folderPath = new StringBuilder(); for (int i = 0; i < folders.length - 1; i++) { folderPath.append(folders[i]); folderPath.append(File.separator); } LOGGER.trace("NFSService - storeFile - folder path : {}", folderPath.toString()); // Copy file to the target location (Replacing existing file with the same name) File foldersExists = new File(this.fileStorageLocation + File.separator + folderPath.toString()); if (!foldersExists.exists()) { boolean folderCreatingStatus = foldersExists.mkdirs(); LOGGER.trace( "NFSService - storeFile - folders created : {}, Path: {}", folderCreatingStatus, foldersExists.getAbsolutePath()); } LOGGER.trace("NFSService - storeFile - folders found : {}", foldersExists.getAbsolutePath()); File destinationFile = new File(this.fileStorageLocation + File.separator + cleanArtifactPath); if (!destinationFile.exists()) { boolean destFileCreatingStatus = destinationFile.createNewFile(); LOGGER.trace( "NFSService - storeFile - file created : {}, Path: {}", destFileCreatingStatus, destinationFile.getAbsolutePath()); } LOGGER.trace("NFSService - storeFile - file found : {}", foldersExists.getAbsolutePath()); FileOutputStream fileOutputStream = new FileOutputStream(destinationFile); IOUtils.copy(uploadedFileInputStream, fileOutputStream); fileOutputStream.close(); uploadedFileInputStream.close(); LOGGER.trace( "NFSService - storeFile - file stored successfully, target location : {}", destinationFile.getAbsolutePath()); LOGGER.trace("NFSService - storeFile returned"); return destinationFile.getName(); } catch (IOException ex) { String errorMessage = "Could not store file. Please try again!"; LOGGER.warn(errorMessage, ex); throw new ModelDBException(errorMessage, ex); } }
Example 8
Source File: NFSArtifactStoreTest.java From modeldb with Apache License 2.0 | 4 votes |
@Test public void getArtifactTest() { LOGGER.info("get artifact test start................................"); try { storeArtifactTest(); GetUrlForArtifact getUrlForArtifactRequest = GetUrlForArtifact.newBuilder() .setId(experimentRun.getId()) .setKey(artifactKey) .setMethod("GET") .setArtifactType(ArtifactType.IMAGE) .build(); GetUrlForArtifact.Response getUrlForArtifactResponse = experimentRunServiceStub.getUrlForArtifact(getUrlForArtifactRequest); URL url = new URL(getUrlForArtifactResponse.getUrl()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream inputStream = connection.getInputStream(); String rootPath = System.getProperty("user.dir"); FileOutputStream fileOutputStream = new FileOutputStream(new File(rootPath + File.separator + artifactKey)); IOUtils.copy(inputStream, fileOutputStream); fileOutputStream.close(); inputStream.close(); File downloadedFile = new File(rootPath + File.separator + artifactKey); if (!downloadedFile.exists()) { fail("File not fount at download destination"); } downloadedFile.delete(); } catch (Exception e) { e.printStackTrace(); Status status = Status.fromThrowable(e); LOGGER.error( "Error Code : " + status.getCode() + " Description : " + status.getDescription()); fail(); } LOGGER.info("get artifact test stop................................"); }
Example 9
Source File: ArtifactStoreTest.java From modeldb with Apache License 2.0 | 4 votes |
@Test public void getArtifactFromCloudTest() { LOGGER.info("get artifact from cloud test start................................"); try { ArtifactStoreBlockingStub artifactStoreBlockingStub = ArtifactStoreGrpc.newBlockingStub(channel); StoreArtifact storeArtifact = StoreArtifact.newBuilder() .setKey("verta_logo.png") .setPath( "https://www.verta.ai/static/logo-landing-424af27a5fc184c64225f604232db39e.png") .build(); StoreArtifact.Response response = artifactStoreBlockingStub.storeArtifact(storeArtifact); String cloudFileKey = response.getArtifactStoreKey(); String cloudFilePath = response.getArtifactStorePath(); LOGGER.log( Level.INFO, "StoreArtifact.Response : \n cloudFileKey - " + cloudFileKey + " \n cloudFilePath - " + cloudFilePath); assumeTrue(cloudFileKey != null && !cloudFileKey.isEmpty()); assumeTrue(cloudFilePath != null && !cloudFilePath.isEmpty()); GetArtifact getArtifactRequest = GetArtifact.newBuilder().setKey(cloudFileKey).build(); GetArtifact.Response getArtifactResponse = artifactStoreBlockingStub.getArtifact(getArtifactRequest); ByteString responseByteString = getArtifactResponse.getContents(); InputStream inputStream = responseByteString.newInput(); String rootPath = System.getProperty("user.dir"); FileOutputStream fileOutputStream = new FileOutputStream(new File(rootPath + File.separator + cloudFileKey)); IOUtils.copy(inputStream, fileOutputStream); fileOutputStream.close(); inputStream.close(); File downloadedFile = new File(rootPath + File.separator + cloudFileKey); if (!downloadedFile.exists()) { fail("File not fount at download destination"); } downloadedFile.delete(); DeleteArtifact deleteArtifact = DeleteArtifact.newBuilder().setKey(cloudFileKey).build(); DeleteArtifact.Response deleteArtifactResponse = artifactStoreBlockingStub.deleteArtifact(deleteArtifact); assertTrue(deleteArtifactResponse.getStatus()); } catch (Exception e) { e.printStackTrace(); Status status = Status.fromThrowable(e); LOGGER.warning( "Error Code : " + status.getCode() + " Description : " + status.getDescription()); fail(); } LOGGER.info("get artifact from cloud test stop................................"); }
Example 10
Source File: AbstractInputStreamContent.java From google-http-java-client with Apache License 2.0 | 4 votes |
public void writeTo(OutputStream out) throws IOException { IOUtils.copy(getInputStream(), out, closeInputStream); out.flush(); }
Example 11
Source File: HttpResponse.java From google-http-java-client with Apache License 2.0 | 3 votes |
/** * Parses the content of the HTTP response from {@link #getContent()} and reads it into a string. * * <p>Since this method returns {@code ""} for no content, a simpler check for no content is to * check if {@link #getContent()} is {@code null}. * * <p>All content is read from the input content stream rather than being limited by the * Content-Length. For the character set, it follows the specification by parsing the "charset" * parameter of the Content-Type header or by default {@code "ISO-8859-1"} if the parameter is * missing. * * @return parsed string or {@code ""} for no content * @throws IOException I/O exception */ public String parseAsString() throws IOException { InputStream content = getContent(); if (content == null) { return ""; } ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(content, out); return out.toString(getContentCharset().name()); }
Example 12
Source File: HttpResponse.java From google-http-java-client with Apache License 2.0 | 2 votes |
/** * Writes the content of the HTTP response into the given destination output stream. * * <p>Sample usage: * * <pre> * HttpRequest request = requestFactory.buildGetRequest( * new GenericUrl("https://www.google.com/images/srpr/logo3w.png")); * OutputStream outputStream = new FileOutputStream(new File("/tmp/logo3w.png")); * try { * HttpResponse response = request.execute(); * response.download(outputStream); * } finally { * outputStream.close(); * } * </pre> * * <p>This method closes the content of the HTTP response from {@link #getContent()}. * * <p>This method does not close the given output stream. * * @param outputStream destination output stream * @throws IOException I/O exception * @since 1.9 */ public void download(OutputStream outputStream) throws IOException { InputStream inputStream = getContent(); IOUtils.copy(inputStream, outputStream); }