@Override public ByteSource asByteSource(Charset charset) { if (charset.equals(this.charset)) { return ByteSource.this; } return super.asByteSource(charset); }
@Override public ByteSource asByteSource(Charset charset) { if (charset.equals(this.charset)) { return ByteSource.this; } return super.asByteSource(charset); }
@Override public ByteSource asByteSource(Charset charset) { if (charset.equals(this.charset)) { return ByteSource.this; } return super.asByteSource(charset); }
@Override public ByteSource createSource(byte[] data) throws IOException { return factory.createSource(new String(data, Charsets.UTF_8)).asByteSource(Charsets.UTF_8); }
public void testRoundTripSources() throws Exception { File asciiFile = getTestFile("ascii.txt"); ByteSource byteSource = Files.asByteSource(asciiFile); assertSame(byteSource, byteSource.asCharSource(Charsets.UTF_8).asByteSource(Charsets.UTF_8)); }
@Override public ByteSource asByteSource(Charset charset) { if (charset.equals(this.charset)) { return ByteSource.this; } return super.asByteSource(charset); }
@Override public ByteSource asByteSource(Charset charset) { if (charset.equals(this.charset)) { return ByteSource.this; } return super.asByteSource(charset); }
private static ByteSource toByteSourceFromPairs(List<NameValuePair> params, MediaType contentType) { Charset charset = contentType.charset().or(DEFAULT_WWW_FORM_DATA_CHARSET); String formString = URLEncodedUtils.format(params, charset); return CharSource.wrap(formString).asByteSource(charset); }
@Override public ByteSource createSource(byte[] data) throws IOException { return factory.createSource(new String(data, Charsets.UTF_8)) .asByteSource(Charsets.UTF_8); }
@Override void compile(SoyFileSet.Builder sfsBuilder) throws IOException { sfsBuilder.setAllowExternalCalls(allowExternalCalls); SoyFileSet sfs = sfsBuilder.build(); ImmutableMap<String, String> parseInfo = sfs.generateParseInfo(javaPackage, javaClassNameSource); if (outputSrcJar == null) { for (Map.Entry<String, String> entry : parseInfo.entrySet()) { File outputFile = new File(outputDirectory, entry.getKey()); BaseUtils.ensureDirsExistInPath(outputFile.getPath()); Files.asCharSink(outputFile, UTF_8).write(entry.getValue()); } } else { String resourcePath = javaPackage.replace('.', '/') + "/"; try (SoyJarFileWriter writer = new SoyJarFileWriter(new FileOutputStream(outputSrcJar))) { for (Map.Entry<String, String> entry : parseInfo.entrySet()) { writer.writeEntry( resourcePath + entry.getKey(), CharSource.wrap(entry.getValue()).asByteSource(UTF_8)); } } } } }
@Override void compile(SoyFileSet.Builder sfsBuilder) throws IOException { sfsBuilder.setAllowExternalCalls(allowExternalCalls); SoyFileSet sfs = sfsBuilder.build(); ImmutableMap<String, String> parseInfo = sfs.generateParseInfo(javaPackage, javaClassNameSource); if (outputSrcJar == null) { for (Map.Entry<String, String> entry : parseInfo.entrySet()) { File outputFile = new File(outputDirectory, entry.getKey()); BaseUtils.ensureDirsExistInPath(outputFile.getPath()); Files.asCharSink(outputFile, UTF_8).write(entry.getValue()); } } else { String resourcePath = javaPackage.replace('.', '/') + "/"; try (SoyJarFileWriter writer = new SoyJarFileWriter(new FileOutputStream(outputSrcJar))) { for (Map.Entry<String, String> entry : parseInfo.entrySet()) { writer.writeEntry( resourcePath + entry.getKey(), CharSource.wrap(entry.getValue()).asByteSource(UTF_8)); } } } } }
public void test_toCharSource_noBomUtf8() throws IOException { byte[] bytes = {'H', 'e', 'l', 'l', 'o'}; ByteSource byteSource = ByteSource.wrap(bytes); CharSource charSource = UnicodeBom.toCharSource(byteSource); String str = charSource.read(); assertEquals(str, "Hello"); assertTrue(charSource.asByteSource(StandardCharsets.UTF_8).contentEquals(byteSource)); assertEquals(charSource.toString().startsWith("UnicodeBom"), true); }
protected static ByteSource prepareContent(@Nullable JsonObject content, Path scratchDir) throws IOException { if (content == null) { return ByteSource.empty(); } @Nullable String text = asStringOrNull(content.getAsJsonPrimitive("text")); if (text == null) { return ByteSource.empty(); } @Nullable String encoding = asStringOrNull(content.getAsJsonPrimitive("encoding")); ByteSource decodedSource; if ("base64".equalsIgnoreCase(encoding)) { decodedSource = BaseEncoding.base64().decodingSource(CharSource.wrap(text)); } else { decodedSource = CharSource.wrap(text).asByteSource(StandardCharsets.UTF_8); } // do this stuff if you want to cache the response content bytes on disk // File tempFile = File.createTempFile("response-content", ".tmp", scratchDir.toFile()); // decodedSource.copyTo(Files.asByteSink(tempFile)); // return Files.asByteSource(tempFile); return decodedSource; } }
ByteSource data = CharSource.wrap(text).asByteSource(adjustedCharset); MediaType adjustedContentType = mediaType.withCharset(adjustedCharset); return TypedContent.identity(data, adjustedContentType);
private static InputStream stringToInputStream(String contents) throws IOException { return CharSource.wrap(contents).asByteSource(Charsets.UTF_8).openBufferedStream(); }
writer.writeEntry( Names.javaFileName(namespace, fileName), files.get(file.getFilePath()).asCharSource().asByteSource(UTF_8));
writer.writeEntry( Names.javaFileName(namespace, fileName), files.get(file.getFilePath()).asCharSource().asByteSource(UTF_8));
public void testRoundTripSources() throws Exception { File asciiFile = getTestFile("ascii.txt"); ByteSource byteSource = Files.asByteSource(asciiFile); assertSame(byteSource, byteSource.asCharSource(Charsets.UTF_8).asByteSource(Charsets.UTF_8)); }
/** Confirms an upload is logged as expected. */ @Test public void testLogUpload() throws IOException { String contentsString = "some contents"; InputStream responseContent = CharSource.wrap(contentsString).asByteSource(UTF_8).openStream(); BatchJobUploadResponse response = new BatchJobUploadResponse( responseContent, statusCode, statusMessage, contentsString.length(), URI.create(url)); ArgumentCaptor<RemoteCallReturn> returnCaptor = ArgumentCaptor.forClass(RemoteCallReturn.class); batchJobLogger.logUpload(contentsString, URI.create(url), response, exception); verify(loggerDelegate).logRequestSummary(returnCaptor.capture()); RemoteCallReturn capturedReturn = returnCaptor.getValue(); assertEquals(exception, capturedReturn.getException()); RequestInfo requestInfo = capturedReturn.getRequestInfo(); assertEquals(url, requestInfo.getUrl()); assertEquals("clientCustomerId", requestInfo.getContextName()); assertNull(requestInfo.getContextValue()); assertThat(requestInfo.getPayload(), containsString(contentsString)); assertThat(requestInfo.getServiceName(), containsString("upload")); ResponseInfo responseInfo = capturedReturn.getResponseInfo(); assertNull(responseInfo.getRequestId()); assertThat(responseInfo.getPayload(), startsWith(String.valueOf(response.getHttpStatus()))); assertThat(responseInfo.getPayload(), containsString(response.getHttpResponseMessage())); verify(loggerDelegate).logRequestDetails(returnCaptor.capture()); assertSame( "The same RemoteCallReturn object was not passed to request details and request summary", capturedReturn, returnCaptor.getValue()); }