protected String getTemplate(String path) throws IOException { Resource resource = getResource(path); if (resource == null) { throw new IllegalStateException("Template resource [" + path + "] not found"); } InputStreamReader reader = (this.charset != null ? new InputStreamReader(resource.getInputStream(), this.charset) : new InputStreamReader(resource.getInputStream())); return FileCopyUtils.copyToString(reader); }
protected String getTemplate(String path) throws IOException { Resource resource = getResource(path); if (resource == null) { throw new IllegalStateException("Template resource [" + path + "] not found"); } InputStreamReader reader = new InputStreamReader(resource.getInputStream(), getDefaultCharset()); return FileCopyUtils.copyToString(reader); }
@Override public InputStream getAsciiStream() throws SQLException { try { if (this.content != null) { return new ByteArrayInputStream(this.content.getBytes(StandardCharsets.US_ASCII)); } else if (this.characterStream != null) { String tempContent = FileCopyUtils.copyToString(this.characterStream); return new ByteArrayInputStream(tempContent.getBytes(StandardCharsets.US_ASCII)); } else { return (this.asciiStream != null ? this.asciiStream : StreamUtils.emptyInput()); } } catch (IOException ex) { throw new SQLException("Failed to read stream content: " + ex); } }
@Override public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } Reader reader = this.resource.getReader(); return FileCopyUtils.copyToString(reader); }
protected String getTemplate(String path) throws IOException { Resource resource = getResource(path); if (resource == null) { throw new IllegalStateException("Template resource [" + path + "] not found"); } InputStreamReader reader = (this.charset != null ? new InputStreamReader(resource.getInputStream(), this.charset) : new InputStreamReader(resource.getInputStream())); return FileCopyUtils.copyToString(reader); }
@Override public String getScriptAsString() throws IOException { synchronized (this.lastModifiedMonitor) { this.lastModified = retrieveLastModifiedTime(); } Reader reader = this.resource.getReader(); return FileCopyUtils.copyToString(reader); }
private void checkExceptionFromInvalidValueType(Throwable ex) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ex.printStackTrace(new PrintStream(baos)); String dump = FileCopyUtils.copyToString(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()))); assertTrue(dump.contains("someMessageSource")); assertTrue(dump.contains("useCodeAsDefaultMessage")); }
@Test public void copyToString() throws IOException { String content = "content"; StringReader in = new StringReader(content); String result = FileCopyUtils.copyToString(in); assertEquals(content, result); }
private String fetchMacro(String name) throws Exception { ClassPathResource resource = new ClassPathResource("test.ftl", getClass()); assertTrue(resource.exists()); String all = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); all = all.replace("\r\n", "\n"); String[] macros = StringUtils.delimitedListToStringArray(all, "\n\n"); for (String macro : macros) { if (macro.startsWith(name)) { return macro.substring(macro.indexOf("\n")).trim(); } } return null; }
@Override public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException { ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK); String buf = FileCopyUtils.copyToString(filterRequest.getReader()); assertEquals(requestBody, buf); } };
@Test public void testByteArrayResource() throws IOException { Resource resource = new ByteArrayResource("testString".getBytes()); assertTrue(resource.exists()); assertFalse(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertEquals(resource, new ByteArrayResource("testString".getBytes())); }
@Test public void testInputStreamResource() throws IOException { InputStream is = new ByteArrayInputStream("testString".getBytes()); Resource resource = new InputStreamResource(is); assertTrue(resource.exists()); assertTrue(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertEquals(resource, new InputStreamResource(is)); }
@Test public void readStreamSource() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); StreamSource result = (StreamSource) converter.read(StreamSource.class, inputMessage); String s = FileCopyUtils.copyToString(new InputStreamReader(result.getInputStream())); assertThat("Invalid result", s, isSimilarTo(BODY)); }
@Test public void testByteArrayResourceWithDescription() throws IOException { Resource resource = new ByteArrayResource("testString".getBytes(), "my description"); assertTrue(resource.exists()); assertFalse(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertTrue(resource.getDescription().contains("my description")); assertEquals(resource, new ByteArrayResource("testString".getBytes())); }
@Test public void testInputStreamResourceWithDescription() throws IOException { InputStream is = new ByteArrayInputStream("testString".getBytes()); Resource resource = new InputStreamResource(is, "my description"); assertTrue(resource.exists()); assertTrue(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertTrue(resource.getDescription().contains("my description")); assertEquals(resource, new InputStreamResource(is)); }
@Test public void resourceInjection() throws IOException { System.setProperty("logfile", "do_not_delete_me.txt"); try (AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ResourceInjectionBean.class)) { ResourceInjectionBean resourceInjectionBean = ac.getBean(ResourceInjectionBean.class); Resource resource = new ClassPathResource("do_not_delete_me.txt"); assertEquals(resource, resourceInjectionBean.resource); assertEquals(resource.getURL(), resourceInjectionBean.url); assertEquals(resource.getURI(), resourceInjectionBean.uri); assertEquals(resource.getFile(), resourceInjectionBean.file); assertArrayEquals(FileCopyUtils.copyToByteArray(resource.getInputStream()), FileCopyUtils.copyToByteArray(resourceInjectionBean.inputStream)); assertEquals(FileCopyUtils.copyToString(new EncodedResource(resource).getReader()), FileCopyUtils.copyToString(resourceInjectionBean.reader)); } finally { System.getProperties().remove("logfile"); } }
@Test public void readSAXSource() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8")); inputMessage.getHeaders().setContentType(new MediaType("application", "xml")); SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage); InputSource inputSource = result.getInputSource(); String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream())); assertThat("Invalid result", s, isSimilarTo(BODY)); }
@Test public void setContentAndGetReader() throws IOException { byte[] bytes = "body".getBytes(Charset.defaultCharset()); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertEquals("body", FileCopyUtils.copyToString(request.getReader())); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); assertEquals("body", FileCopyUtils.copyToString(request.getReader())); }
@Test public void testGenerateNodeIdRandomlyCreatesFileWithNodeIdAndPrivateKey() throws Exception { File nodeIdPropertiesFile = new File("database-test/nodeId.properties"); //Cleanup previous nodeId.properties file (if exists) //noinspection ResultOfMethodCallIgnored nodeIdPropertiesFile.delete(); new GenerateNodeIdRandomly("database-test").getNodePrivateKey(); assertTrue(nodeIdPropertiesFile.exists()); String contents = FileCopyUtils.copyToString(new FileReader(nodeIdPropertiesFile)); String[] lines = StringUtils.tokenizeToStringArray(contents, "\n"); assertEquals(4, lines.length); assertTrue(lines[0].startsWith("#Generated NodeID.")); assertTrue(lines[1].startsWith("#")); assertTrue(lines[2].startsWith("nodeIdPrivateKey=")); assertEquals("nodeIdPrivateKey=".length() + 64, lines[2].length()); assertTrue(lines[3].startsWith("nodeId=")); assertEquals("nodeId=".length() + 128, lines[3].length()); //noinspection ResultOfMethodCallIgnored nodeIdPropertiesFile.delete(); }
@Test public void testGenerateNodeIdFromPropsFileReadsExistingFile() throws Exception { // Create temporary nodeId.properties file ECKey key = new ECKey(); String expectedNodePrivateKey = Hex.toHexString(key.getPrivKeyBytes()); String expectedNodeId = Hex.toHexString(key.getNodeId()); createNodeIdPropertiesFile("database-test", key); new GetNodeIdFromPropsFile("database-test").getNodePrivateKey(); assertTrue(nodeIdPropertiesFile.exists()); String contents = FileCopyUtils.copyToString(new FileReader(nodeIdPropertiesFile)); String[] lines = StringUtils.tokenizeToStringArray(contents, "\n"); assertEquals(4, lines.length); assertTrue(lines[0].startsWith("#Generated NodeID.")); assertTrue(lines[1].startsWith("#")); assertTrue(lines[2].startsWith("nodeIdPrivateKey=" + expectedNodePrivateKey)); assertTrue(lines[3].startsWith("nodeId=" + expectedNodeId)); }