List of usage examples for java.nio.channels Channels newOutputStream
public static OutputStream newOutputStream(AsynchronousByteChannel ch)
From source file:com.amazonaws.services.kinesis.producer.Daemon.java
private void connectToChild() throws IOException { long start = System.nanoTime(); while (true) { try {//from w w w .java 2s. c o m inChannel = FileChannel.open(Paths.get(inPipe.getAbsolutePath()), StandardOpenOption.READ); outChannel = FileChannel.open(Paths.get(outPipe.getAbsolutePath()), StandardOpenOption.WRITE); outStream = Channels.newOutputStream(outChannel); break; } catch (IOException e) { if (inChannel != null && inChannel.isOpen()) { inChannel.close(); } if (outChannel != null && outChannel.isOpen()) { outChannel.close(); } try { Thread.sleep(100); } catch (InterruptedException e1) { } if (System.nanoTime() - start > 2e9) { throw e; } } } }
From source file:com.streamsets.pipeline.stage.cloudstorage.destination.GoogleCloudStorageTarget.java
private void handleWholeFileFormat(Batch batch, ELVars elVars) { batch.getRecords().forEachRemaining(record -> { RecordEL.setRecordInContext(elVars, record); TimeEL.setCalendarInContext(elVars, calendar); try {//w w w.j a v a2 s . c o m Date recordDate = ELUtils.getRecordTime(timeDriverElEval, elVars, gcsTargetConfig.timeDriverTemplate, record); TimeNowEL.setTimeNowInContext(elVars, recordDate); String path = partitionEval.eval(elVars, gcsTargetConfig.partitionTemplate, String.class); String fileName = fileNameEval.eval(elVars, gcsTargetConfig.dataGeneratorFormatConfig.fileNameEL, String.class); String filePath = GcsUtil .normalizePrefix(GcsUtil.normalizePrefix(gcsTargetConfig.commonPrefix) + path) + fileName; BlobId blobId = BlobId.of(gcsTargetConfig.bucketTemplate, filePath); BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); Blob blob = storage.get(blobId); if (blob != null && gcsTargetConfig.dataGeneratorFormatConfig.wholeFileExistsAction == WholeFileExistsAction.TO_ERROR) { //File already exists and error action is to Error getContext().toError(record, Errors.GCS_03, filePath); return; } //else overwrite EventRecord eventRecord = GCSEvents.FILE_TRANSFER_COMPLETE_EVENT.create(getContext()) .with(FileRefUtil.WHOLE_FILE_SOURCE_FILE_INFO, record.get(FileRefUtil.FILE_INFO_FIELD_PATH).getValueAsMap()) .withStringMap(FileRefUtil.WHOLE_FILE_TARGET_FILE_INFO, ImmutableMap.of(GCSEvents.BUCKET, blobId.getBucket(), GCSEvents.OBJECT_KEY, blobId.getName())) .create(); FileRefStreamCloseEventHandler fileRefStreamCloseEventHandler = new FileRefStreamCloseEventHandler( eventRecord); boolean errorHappened = false; try (DataGenerator dg = gcsTargetConfig.dataGeneratorFormatConfig.getDataGeneratorFactory() .getGenerator(Channels.newOutputStream(storage.writer(blobInfo)), //Close handler for populating checksum info in the event. fileRefStreamCloseEventHandler)) { dg.write(record); } catch (IOException | DataGeneratorException e) { LOG.error("Error happened when Writing to Output stream. Reason {}", e); getContext().toError(record, Errors.GCS_02, e); errorHappened = true; } if (!errorHappened) { //Put the event if the record is not sent to error. getContext().toEvent(eventRecord); } } catch (ELEvalException e) { LOG.error("Error happened when evaluating Expressions. Reason {}", e); getContext().toError(record, Errors.GCS_04, e); } catch (OnRecordErrorException e) { LOG.error("Error happened when evaluating Expressions. Reason {}", e); getContext().toError(e.getRecord(), e.getErrorCode(), e.getParams()); } }); }
From source file:cltestgrid.Upload2.java
private static void saveBlob(String blobName, String contentType, byte[] data) throws IOException { log.info("saving blob " + blobName); FileWriteChannel blobChannel = newBlobChannel(blobName, contentType); OutputStream ostream = Channels.newOutputStream(blobChannel); ostream.write(data);// w w w .j a v a 2s .c o m ostream.flush(); blobChannel.closeFinally(); }
From source file:com.google.cloud.dataflow.sdk.runners.worker.TextReaderTest.java
@Test public void testErrorOnMultipleFiles() throws Exception { File file1 = tmpFolder.newFile("foo1.avro"); File file2 = tmpFolder.newFile("foo2.avro"); Channels.newOutputStream(IOChannelUtils.create(file1.getPath(), MimeTypes.BINARY)).close(); Channels.newOutputStream(IOChannelUtils.create(file2.getPath(), MimeTypes.BINARY)).close(); TextReader<String> textReader = new TextReader<>(new File(tmpFolder.getRoot(), "*").getPath(), true, 0L, 100L, StringUtf8Coder.of(), TextIO.CompressionType.UNCOMPRESSED); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("more than 1 file matched"); textReader.iterator();//from w w w .ja v a 2s. c o m }
From source file:org.activityinfo.server.attachment.AppEngineAttachmentService.java
@Override public void upload(String key, FileItem fileItem, InputStream uploadingStream) { try {//www.ja v a2s. co m GSFileOptionsBuilder builder = new GSFileOptionsBuilder().setBucket("activityinfo-attachments") .setKey(key).setContentDisposition("attachment; filename=\"" + fileItem.getName() + "\"") .setMimeType(fileItem.getContentType()); FileService fileService = FileServiceFactory.getFileService(); AppEngineFile writableFile = fileService.createNewGSFile(builder.build()); boolean lock = true; FileWriteChannel writeChannel = fileService.openWriteChannel(writableFile, lock); OutputStream os = Channels.newOutputStream(writeChannel); ByteStreams.copy(fileItem.getInputStream(), os); os.flush(); writeChannel.closeFinally(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.alfresco.repo.content.AbstractContentWriter.java
/** * @see Channels#newOutputStream(java.nio.channels.WritableByteChannel) *//*from w ww . ja v a 2s .c om*/ public OutputStream getContentOutputStream() throws ContentIOException { try { WritableByteChannel channel = getWritableChannel(); OutputStream is = new BufferedOutputStream(Channels.newOutputStream(channel)); // done return is; } catch (Throwable e) { throw new ContentIOException("Failed to open stream onto channel: \n" + " writer: " + this, e); } }
From source file:org.apache.abdera.util.AbstractStreamWriter.java
public StreamWriter setChannel(WritableByteChannel channel) { return setOutputStream(Channels.newOutputStream(channel)); }
From source file:org.apache.axis2.transport.nhttp.Axis2HttpRequest.java
/** * Start streaming the message into the Pipe, so that the contents could be read off the source * channel returned by getSourceChannel() * @throws AxisFault on error//from w w w .j a va 2 s . c om */ public void streamMessageContents() throws AxisFault { log.debug("start streaming outgoing http request"); OutputStream out = Channels.newOutputStream(pipe.sink()); OMOutputFormat format = Util.getOMOutputFormat(msgContext); try { (msgContext.isDoingREST() ? msgContext.getEnvelope().getBody().getFirstElement() : msgContext.getEnvelope()).serializeAndConsume(out, format); } catch (XMLStreamException e) { handleException("Error serializing response message", e); } try { out.flush(); out.close(); } catch (IOException e) { handleException("Error closing outgoing message stream", e); } }
From source file:org.apache.axis2.transport.nhttp.ServerHandler.java
/** * Process a new incoming request// w ww. java 2 s. c o m * @param conn the connection */ public void requestReceived(final NHttpServerConnection conn) { HttpContext context = conn.getContext(); HttpRequest request = conn.getHttpRequest(); context.setAttribute(HttpContext.HTTP_REQUEST, request); // allocate temporary buffers to process this request context.setAttribute(REQUEST_BUFFER, ByteBuffer.allocate(2048)); context.setAttribute(RESPONSE_BUFFER, ByteBuffer.allocate(2048)); try { Pipe requestPipe = Pipe.open(); // the pipe used to process the request Pipe responsePipe = Pipe.open(); // the pipe used to process the response context.setAttribute(REQUEST_SINK_CHANNEL, requestPipe.sink()); context.setAttribute(RESPONSE_SOURCE_CHANNEL, responsePipe.source()); // create the default response to this request HttpVersion httpVersion = request.getRequestLine().getHttpVersion(); HttpResponse response = responseFactory.newHttpResponse(httpVersion, HttpStatus.SC_OK, context); response.setParams(this.params); // create a basic HttpEntity using the source channel of the response pipe BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(Channels.newInputStream(responsePipe.source())); if (httpVersion.greaterEquals(HttpVersion.HTTP_1_1)) { entity.setChunked(true); } response.setEntity(entity); // hand off processing of the request to a thread off the pool workerPool.execute( new ServerWorker(cfgCtx, conn, this, request, Channels.newInputStream(requestPipe.source()), response, Channels.newOutputStream(responsePipe.sink()))); } catch (IOException e) { handleException("Error processing request received for : " + request.getRequestLine().getUri(), e, conn); } }