Java Code Examples for java.nio.channels.Channels#newWriter()
The following examples show how to use
java.nio.channels.Channels#newWriter() .
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: ChannelsTest.java From j2objc with Apache License 2.0 | 6 votes |
public void testNewWriterWritableByteChannelString_InputNull() throws IOException { this.fouts = new FileOutputStream(tmpFile); WritableByteChannel wbChannel = Channels.newChannel(this.fouts); Writer testWriter = Channels.newWriter(wbChannel, Charset.forName( CODE_SET).newEncoder(), //$NON-NLS-1$ 1); String writebuf = ""; //$NON-NLS-1$ for (int val = 0; val < this.writebufSize / 2; val++) { writebuf = writebuf + ((char) (val + 64)); } // can write to buffer testWriter.write(writebuf); testWriter.flush(); testWriter.close(); }
Example 2
Source File: GCSClientTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test @InSequence(1) public void testCreateGsObj() throws IOException { GcsFilename filename = new GcsFilename(bucket, OBJECT_NAME); GcsFileOptions option = new GcsFileOptions.Builder() .mimeType("text/html") .acl("public-read") .build(); try (GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, option)) { PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8")); out.println(CONTENT); out.flush(); writeChannel.waitForOutstandingWrites(); writeChannel.write(ByteBuffer.wrap(MORE_WORDS.getBytes())); assertEquals(filename, writeChannel.getFilename()); } assertEquals(bucket, filename.getBucketName()); assertEquals(OBJECT_NAME, filename.getObjectName()); }
Example 3
Source File: JournaledChannelPoolTest.java From emissary with Apache License 2.0 | 5 votes |
private void writeText(final KeyedOutput ko, final String text) throws IOException { try (BufferedWriter bw = new BufferedWriter(Channels.newWriter(ko, "UTF-8"))) { bw.write(text); bw.flush(); ko.commit(); } }
Example 4
Source File: TransferSummaryReportImpl.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
protected Writer createUnderlyingLogWriter(String transferId) { if (reportFile == null) { reportFile = createTransferRecord(transferId); } ContentWriter contentWriter = contentService.getWriter(reportFile, ContentModel.PROP_CONTENT, true); contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML); contentWriter.setEncoding("UTF-8"); return Channels.newWriter(contentWriter.getWritableChannel(), "UTF-8"); }
Example 5
Source File: RepoTransferProgressMonitorImpl.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected Writer createUnderlyingLogWriter(String transferId) { NodeRef node = new NodeRef(transferId); ContentWriter contentWriter = contentService.getWriter(node, ContentModel.PROP_CONTENT, true); contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML); contentWriter.setEncoding("UTF-8"); return Channels.newWriter(contentWriter.getWritableChannel(), "UTF-8"); }
Example 6
Source File: FileSystemsTest.java From beam with Apache License 2.0 | 5 votes |
private void createFileWithContent(Path path, String content) throws Exception { try (Writer writer = Channels.newWriter( localFileSystem.create( LocalResourceId.fromPath(path, false /* isDirectory */), CreateOptions.StandardCreateOptions.builder().setMimeType(MimeTypes.TEXT).build()), StandardCharsets.UTF_8.name())) { writer.write(content); } }
Example 7
Source File: LocalFileSystemTest.java From beam with Apache License 2.0 | 5 votes |
private void createFileWithContent(Path path, String content) throws Exception { try (Writer writer = Channels.newWriter( localFileSystem.create( LocalResourceId.fromPath(path, false /* isDirectory */), StandardCreateOptions.builder().setMimeType(MimeTypes.TEXT).build()), StandardCharsets.UTF_8.name())) { writer.write(content); } }
Example 8
Source File: ReportExport.java From zap-extensions with Apache License 2.0 | 5 votes |
@SuppressWarnings("resource") private static File write(String path, String str, Boolean append) throws Exception { File f = new File(path); try (Writer writer = Channels.newWriter( new FileOutputStream(f.getAbsoluteFile(), append).getChannel(), StandardCharsets.UTF_8.name())) { writer.append(str); } return f; }
Example 9
Source File: ChannelsTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testnewWriterCharsetError() throws Exception { this.fouts = new FileOutputStream(tmpFile); WritableByteChannel wbChannel = Channels.newChannel(this.fouts); try { Channels.newWriter(wbChannel, Charset.forName(BAD_CODE_SET) .newEncoder(), -1); fail(); } catch (UnsupportedCharsetException e) { // correct } }
Example 10
Source File: ChannelsTest.java From j2objc with Apache License 2.0 | 5 votes |
public void testNewWriterWritableByteChannelString() throws IOException { this.fouts = new FileOutputStream(tmpFile); WritableByteChannel wbChannel = Channels.newChannel(this.fouts); Writer testWriter = Channels.newWriter(wbChannel, CODE_SET); //$NON-NLS-1$ Writer testWriter_s = Channels.newWriter(wbChannel, Charset.forName( CODE_SET).newEncoder(), //$NON-NLS-1$ -1); String writebuf = ""; //$NON-NLS-1$ for (int val = 0; val < this.writebufSize / 2; val++) { writebuf = writebuf + ((char) (val + 64)); } byte[] bit = new byte[1]; bit[0] = 80; this.fouts.write(bit); this.assertFileSizeSame(tmpFile, 1); // writer continues to write after '1',what the fouts write testWriter.write(writebuf); testWriter.flush(); this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); // testwriter_s does not know if testwrite writes testWriter_s.write(writebuf); testWriter.flush(); this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); // testwriter_s even does not know if himself writes? testWriter_s.write(writebuf); testWriter.flush(); this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); // close the fouts, no longer writable for testWriter for (int val = 0; val < this.writebufSize; val++) { writebuf = writebuf + ((char) (val + 64)); } this.fouts.close(); testWriter_s.write(writebuf); testWriter.flush(); this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1); }
Example 11
Source File: GCSClientTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test @InSequence(4) public void testOptionsAndMetadata() throws IOException { GcsFilename filename = new GcsFilename(bucket, OBJECT_NAME + "4"); GcsFileOptions option = new GcsFileOptions.Builder() .acl("public-read") .cacheControl("Cache-Control: public, max-age=3600") .contentEncoding("Content-Encoding: gzip") .contentDisposition("Content-Disposition: attachment") .mimeType("text/html") .addUserMetadata("userKey", "UserMetadata") .build(); GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, option); try (PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"))) { out.println(CONTENT); out.flush(); } GcsFileMetadata metaData = gcsService.getMetadata(filename); GcsFileOptions option2 = metaData.getOptions(); try { assertEquals(filename, metaData.getFilename()); } finally { gcsService.delete(filename); } assertEquals("Cache-Control: public, max-age=3600", option2.getCacheControl()); assertEquals("Content-Encoding: gzip", option2.getContentEncoding()); assertEquals("Content-Disposition: attachment", option2.getContentDisposition()); assertEquals("text/html", option2.getMimeType()); assertEquals("Content-Encoding: gzip", option2.getContentEncoding()); Map<String, String> userMetadata = option2.getUserMetadata(); assertEquals(1, userMetadata.size()); String key = userMetadata.keySet().iterator().next(); assertEquals("UserMetadata", userMetadata.get(key)); assertEquals("public-read", option2.getAcl()); }
Example 12
Source File: NetcatSource.java From mt-flume with Apache License 2.0 | 4 votes |
@Override public void run() { logger.debug("Starting connection handler"); Event event = null; try { Reader reader = Channels.newReader(socketChannel, "utf-8"); Writer writer = Channels.newWriter(socketChannel, "utf-8"); CharBuffer buffer = CharBuffer.allocate(maxLineLength); buffer.flip(); // flip() so fill() sees buffer as initially empty while (true) { // this method blocks until new data is available in the socket int charsRead = fill(buffer, reader); logger.debug("Chars read = {}", charsRead); // attempt to process all the events in the buffer int eventsProcessed = processEvents(buffer, writer); logger.debug("Events processed = {}", eventsProcessed); if (charsRead == -1) { // if we received EOF before last event processing attempt, then we // have done everything we can break; } else if (charsRead == 0 && eventsProcessed == 0) { if (buffer.remaining() == buffer.capacity()) { // If we get here it means: // 1. Last time we called fill(), no new chars were buffered // 2. After that, we failed to process any events => no newlines // 3. The unread data in the buffer == the size of the buffer // Therefore, we are stuck because the client sent a line longer // than the size of the buffer. Response: Drop the connection. logger.warn("Client sent event exceeding the maximum length"); counterGroup.incrementAndGet("events.failed"); writer.write("FAILED: Event exceeds the maximum length (" + buffer.capacity() + " chars, including newline)\n"); writer.flush(); break; } } } socketChannel.close(); counterGroup.incrementAndGet("sessions.completed"); } catch (IOException e) { counterGroup.incrementAndGet("sessions.broken"); } logger.debug("Connection handler exiting"); }
Example 13
Source File: IntegrationTestHelper.java From dataflow-java with Apache License 2.0 | 2 votes |
/** * Make sure we can get to the output for single-file test results. * * Also write a sentinel value to the file. This protects against the possibility of prior * test output causing a newly failing test to appear to succeed. * * @param outputPath * @throws IOException */ public void touchOutput(String outputPath) throws IOException { try (Writer writer = Channels.newWriter(gcsUtil.create(GcsPath.fromUri(outputPath), "text/plain"), "UTF-8")) { writer.write("output will go here"); } }