@GetMapping("/file-resource") public String fileResource() { try { return "get file resource success. content: " + StreamUtils.copyToString( file.getInputStream(), Charset.forName(CharEncoding.UTF_8)); } catch (Exception e) { e.printStackTrace(); return "get resource fail: " + e.getMessage(); } }
private StreamSource readStreamSource(InputStream body) throws IOException { byte[] bytes = StreamUtils.copyToByteArray(body); return new StreamSource(new ByteArrayInputStream(bytes)); }
public String getContentString() throws MessagingException, IOException { return StreamUtils.copyToString(mimeMessage.getDataHandler().getInputStream(), Charset.forName("UTF-8")); }
@RequestMapping(value = "/", method = RequestMethod.GET) public String readGcsFile() throws IOException { return StreamUtils.copyToString( this.gcsFile.getInputStream(), Charset.defaultCharset()) + "\n"; }
@Override public MultiValueMap<String, String> read(@Nullable Class<? extends MultiValueMap<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : this.charset); String body = StreamUtils.copyToString(inputMessage.getBody(), charset); String[] pairs = StringUtils.tokenizeToStringArray(body, "&"); MultiValueMap<String, String> result = new LinkedMultiValueMap<>(pairs.length); for (String pair : pairs) { int idx = pair.indexOf('='); if (idx == -1) { result.add(URLDecoder.decode(pair, charset.name()), null); } else { String name = URLDecoder.decode(pair.substring(0, idx), charset.name()); String value = URLDecoder.decode(pair.substring(idx + 1), charset.name()); result.add(name, value); } } return result; }
@Test public void copyToString() throws Exception { Charset charset = Charset.defaultCharset(); InputStream inputStream = spy(new ByteArrayInputStream(string.getBytes(charset))); String actual = StreamUtils.copyToString(inputStream, charset); assertThat(actual, equalTo(string)); verify(inputStream, never()).close(); }
@Test public void copyString() throws Exception { Charset charset = Charset.defaultCharset(); ByteArrayOutputStream out = spy(new ByteArrayOutputStream()); StreamUtils.copy(string, charset, out); assertThat(out.toByteArray(), equalTo(string.getBytes(charset))); verify(out, never()).close(); }
/** * Load and print the Spring banner (if one is configured) to UserConsole. * @param environment the Spring environment */ static void printBanner(final Environment environment) { try { final String bannerLocation = environment.getProperty(BANNER_LOCATION_SPRING_PROPERTY_KEY); if (StringUtils.isNotBlank(bannerLocation)) { final ResourceLoader resourceLoader = new DefaultResourceLoader(); final Resource resource = resourceLoader.getResource(bannerLocation); if (resource.exists()) { final String banner = StreamUtils.copyToString( resource.getInputStream(), environment.getProperty( BANNER_CHARSET_SPRING_PROPERTY_KEY, Charset.class, StandardCharsets.UTF_8 ) ); UserConsole.getLogger().info(banner); } } } catch (final Throwable t) { System.err.println("Failed to print banner: " + t.getMessage()); t.printStackTrace(System.err); } } }
synchronized byte[] binary(String name, String profile, String label, String path) throws IOException { if (name != null && name.contains("(_)")) { // "(_)" is uncommon in a git repo name, but "/" cannot be matched // by Spring MVC name = name.replace("(_)", "/"); } if (label != null && label.contains("(_)")) { // "(_)" is uncommon in a git branch name, but "/" cannot be matched // by Spring MVC label = label.replace("(_)", "/"); } // TODO: is this line needed for side effects? prepareEnvironment(this.environmentRepository.findOne(name, profile, label)); try (InputStream is = this.resourceRepository.findOne(name, profile, label, path) .getInputStream()) { return StreamUtils.copyToByteArray(is); } }
protected void writeContent(Resource resource, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { try { InputStream in = resource.getInputStream(); try { StreamUtils.copy(in, outputMessage.getBody()); } catch (NullPointerException ex) { // ignore, see SPR-13620 } finally { try { in.close(); } catch (Throwable ex) { // ignore, see SPR-12999 } } } catch (FileNotFoundException ex) { // ignore, see SPR-12999 } }
protected void saveBundle(Resource resource) { FileWorkArea tempWorkArea = fileService.initializeWorkArea(); String fileToSave = FilenameUtils.separatorsToSystem(getResourcePath(resource.getDescription())); String tempFilename = FilenameUtils.concat(tempWorkArea.getFilePathLocation(), fileToSave); File tempFile = new File(tempFilename); InputStream ris = null; try { ris = resource.getInputStream(); out = new BufferedOutputStream(new FileOutputStream(tempFile)); StreamUtils.copy(ris, out);
@Override public void customize(InitializrMetadata metadata) { log.info("Loading initializr metadata from " + this.resource); try { String content = StreamUtils.copyToString(this.resource.getInputStream(), UTF_8); ObjectMapper objectMapper = new ObjectMapper(); InitializrMetadata anotherMetadata = objectMapper.readValue(content, InitializrMetadata.class); metadata.merge(anotherMetadata); } catch (Exception ex) { throw new IllegalStateException("Cannot merge", ex); } }
@Test public void lastModifiedWorksWithResourceThatDoesNotSupportFileBasedReading() throws Exception { Resource resource = mock(Resource.class); // underlying File is asked for so that the last modified time can be checked... // And then mock the file changing; i.e. the File says it has been modified given(resource.lastModified()).willReturn(100L, 100L, 200L); // does not support File-based reading; delegates to InputStream-style reading... //resource.getFile(); //mock.setThrowable(new FileNotFoundException()); given(resource.getInputStream()).willReturn(StreamUtils.emptyInput()); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); assertTrue("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).", scriptSource.isModified()); scriptSource.getScriptAsString(); assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); // Must now report back as having been modified assertTrue("ResourceScriptSource must report back as being modified if the underlying File resource is reporting a changed lastModified time.", scriptSource.isModified()); }
/** * Copy the contents of the given InputStream into a new byte array. * Leaves the stream open when done. * @param in the stream to copy from (may be {@code null} or empty) * @return the new byte array that has been copied to (possibly empty) * @throws IOException in case of I/O errors */ public static byte[] copyToByteArray(@Nullable InputStream in) throws IOException { if (in == null) { return new byte[0]; } ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE); copy(in, out); return out.toByteArray(); }
/** * Return the textual content of the resource at the specified location. * @param location a resource location * @return the content of the resource */ @Cacheable("initializr.project-resources") public String getTextResource(String location) { try (InputStream stream = getInputStream(location)) { return StreamUtils.copyToString(stream, UTF_8); } catch (IOException ex) { throw new IllegalStateException("Cannot get resource", ex); } }
@Override public InputStream getBody() { return (this.body != null ? this.body : StreamUtils.emptyInput()); }
/** * Return a {@link PomAssert} for this project. * @return a POM assert */ public PomAssert pomAssert() { try { return new PomAssert(StreamUtils.copyToString( new FileInputStream(file("pom.xml")), Charset.forName("UTF-8"))); } catch (IOException ex) { throw new IllegalArgumentException("Cannot resolve pom.xml", ex); } }
private String getDefaultTemplate() { Resource resource = new ClassPathResource("/org/springframework/cloud/skipper/io/generic-template.yml"); String genericTempateData = null; try { genericTempateData = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()); } catch (IOException e) { throw new IllegalArgumentException("Can't load generic template", e); } return genericTempateData; }
@Override public MultiValueMap<String, String> read(@Nullable Class<? extends MultiValueMap<String, ?>> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : this.charset); String body = StreamUtils.copyToString(inputMessage.getBody(), charset); String[] pairs = StringUtils.tokenizeToStringArray(body, "&"); MultiValueMap<String, String> result = new LinkedMultiValueMap<>(pairs.length); for (String pair : pairs) { int idx = pair.indexOf('='); if (idx == -1) { result.add(URLDecoder.decode(pair, charset.name()), null); } else { String name = URLDecoder.decode(pair.substring(0, idx), charset.name()); String value = URLDecoder.decode(pair.substring(idx + 1), charset.name()); result.add(name, value); } } return result; }