本文整理了Java中java.nio.channels.Channels.newOutputStream()
方法的一些代码示例,展示了Channels.newOutputStream()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Channels.newOutputStream()
方法的具体详情如下:
包路径:java.nio.channels.Channels
类名称:Channels
方法名:newOutputStream
[英]Returns an output stream on the given channel. The resulting stream has the following properties:
代码示例来源:origin: GoogleContainerTools/jib
/**
* Acquires an exclusive {@link FileLock} on the {@code file} and opens an {@link OutputStream} to
* write to it. The file will be created if it does not exist, or truncated to length 0 if it does
* exist. The {@link OutputStream} must be closed to release the lock.
*
* <p>The locking mechanism should not be used as a concurrency management feature. Rather, this
* should be used as a way to prevent concurrent writes to {@code file}. Concurrent attempts to
* lock {@code file} will result in {@link OverlappingFileLockException}s.
*
* @param file the file to write to
* @return an {@link OutputStream} that writes to the file
* @throws IOException if an I/O exception occurs
*/
public static OutputStream newLockingOutputStream(Path file) throws IOException {
EnumSet<StandardOpenOption> createOrTruncate =
EnumSet.of(
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
// Channel is closed by outputStream.close().
FileChannel channel = FileChannel.open(file, createOrTruncate);
// Lock is released when channel is closed.
channel.lock();
return Channels.newOutputStream(channel);
}
代码示例来源:origin: spotify/docker-client
@Override
public OutputStream getOutputStream() throws IOException {
if (!channel.isOpen()) {
throw new SocketException("Socket is closed");
}
if (outputShutdown) {
throw new SocketException("Socket output is shutdown");
}
return new FilterOutputStream(Channels.newOutputStream(channel)) {
@Override
public void close() throws IOException {
shutdownOutput();
}
};
}
代码示例来源:origin: apache/flink
LocalRecoverableFsDataOutputStream(File targetFile, File tempFile) throws IOException {
this.targetFile = checkNotNull(targetFile);
this.tempFile = checkNotNull(tempFile);
this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
this.fos = Channels.newOutputStream(fileChannel);
}
代码示例来源:origin: fabric8io/docker-maven-plugin
@Override
public OutputStream getOutputStream() throws IOException {
if (!channel.isOpen()) {
throw new SocketException("Socket is closed");
}
if (outputShutdown) {
throw new SocketException("Socket output is shutdown");
}
return new FilterOutputStream(Channels.newOutputStream(channel)) {
@Override
public void write(byte[] b, int off, int len) throws IOException {
if(log.isDebugEnabled()){
String request = new String(b, off, len, Charset.forName("UTF-8"));
String logValue = ascii().matchesAllOf(request) ? request : "not logged due to non-ASCII characters. ";
log.debug("REQUEST %s", logValue);
}
out.write(b, off, len);
}
@Override
public void close() throws IOException {
shutdownOutput();
}
};
}
代码示例来源:origin: square/okhttp
BlockingUnixSocket(File path, UnixSocketChannel channel) {
super(channel);
this.path = path;
this.in = Channels.newInputStream(new UnselectableReadableByteChannel());
this.out = Channels.newOutputStream(new UnselectableWritableByteChannel());
}
代码示例来源:origin: apache/flink
LocalRecoverableFsDataOutputStream(LocalRecoverable resumable) throws IOException {
this.targetFile = checkNotNull(resumable.targetFile());
this.tempFile = checkNotNull(resumable.tempFile());
if (!tempFile.exists()) {
throw new FileNotFoundException("File Not Found: " + tempFile.getAbsolutePath());
}
this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
if (this.fileChannel.position() < resumable.offset()) {
throw new IOException("Missing data in tmp file: " + tempFile.getAbsolutePath());
}
this.fileChannel.truncate(resumable.offset());
this.fos = Channels.newOutputStream(fileChannel);
}
代码示例来源:origin: apache/incubator-druid
StandardOpenOption.CREATE_NEW
);
final OutputStream out = Channels.newOutputStream(fileChannel)
) {
代码示例来源:origin: web3j/web3j
public UnixDomainSocket(String ipcSocketPath, int bufferSize) {
this.bufferSize = bufferSize;
try {
UnixSocketAddress address = new UnixSocketAddress(ipcSocketPath);
channel = UnixSocketChannel.open(address);
reader = new InputStreamReader(Channels.newInputStream(channel));
writer = new PrintWriter(Channels.newOutputStream(channel));
} catch (IOException e) {
throw new RuntimeException(
"Provided file socket cannot be opened: " + ipcSocketPath, e);
}
}
代码示例来源:origin: apache/incubator-druid
/**
* Create a new temporary file. All methods of the returned output stream may throw
* {@link TemporaryStorageFullException} if the temporary storage area fills up.
*
* @return output stream to the file
*
* @throws TemporaryStorageFullException if the temporary storage area is full
* @throws IOException if something goes wrong while creating the file
*/
public LimitedOutputStream createFile() throws IOException
{
if (bytesUsed.get() >= maxBytesUsed) {
throw new TemporaryStorageFullException(maxBytesUsed);
}
synchronized (files) {
if (closed) {
throw new ISE("Closed");
}
FileUtils.forceMkdir(storageDirectory);
final File theFile = new File(storageDirectory, StringUtils.format("%08d.tmp", files.size()));
final EnumSet<StandardOpenOption> openOptions = EnumSet.of(
StandardOpenOption.CREATE_NEW,
StandardOpenOption.WRITE
);
final FileChannel channel = FileChannel.open(theFile.toPath(), openOptions);
files.add(theFile);
return new LimitedOutputStream(theFile, Channels.newOutputStream(channel));
}
}
代码示例来源:origin: fabric8io/docker-maven-plugin
@Override
public OutputStream getOutputStream() throws IOException {
if (!channel.isOpen()) {
throw new SocketException("Socket is closed");
}
if (!channel.isConnected()) {
throw new SocketException("Socket is not connected");
}
if (outputShutdown) {
throw new SocketException("Socket output is shutdown");
}
return new FilterOutputStream(Channels.newOutputStream(channel)) {
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}
@Override
public void close() throws IOException {
shutdownOutput();
}
};
}
代码示例来源:origin: apache/incubator-druid
StandardOpenOption.CREATE
);
final OutputStream out = Channels.newOutputStream(fileChannel)
) {
return zip(directory, out);
代码示例来源:origin: apache/maven
props.store( Channels.newOutputStream( channel ), "Last modified on: " + new Date() );
代码示例来源:origin: org.apache.commons/commons-compress
/**
* Creates an archive {@code target} using the format {@code
* format} by recursively including all files and directories in
* {@code directory}.
*
* @param format the archive format. This uses the same format as
* accepted by {@link ArchiveStreamFactory}.
* @param target the channel to write the new archive to.
* @param directory the directory that contains the files to archive.
* @throws IOException if an I/O error occurs
* @throws ArchiveException if the archive cannot be created for other reasons
*/
public void create(String format, SeekableByteChannel target, File directory)
throws IOException, ArchiveException {
if (!prefersSeekableByteChannel(format)) {
create(format, Channels.newOutputStream(target), directory);
} else if (ArchiveStreamFactory.ZIP.equalsIgnoreCase(format)) {
create(new ZipArchiveOutputStream(target), directory);
} else if (ArchiveStreamFactory.SEVEN_Z.equalsIgnoreCase(format)) {
create(new SevenZOutputFile(target), directory);
} else {
// never reached as prefersSeekableByteChannel only returns true for ZIP and 7z
throw new ArchiveException("don't know how to handle format " + format);
}
}
代码示例来源:origin: googleapis/google-cloud-java
/** Example of writing a local file to a table. */
// [TARGET writer(WriteChannelConfiguration)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE FileSystems.getDefault().getPath(".", "my-data.csv")]
// [VARIABLE "us"]
public long writeFileToTable(String datasetName, String tableName, Path csvPath, String location)
throws IOException, InterruptedException, TimeoutException {
// [START bigquery_load_from_file]
TableId tableId = TableId.of(datasetName, tableName);
WriteChannelConfiguration writeChannelConfiguration =
WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build();
// The location must be specified; other fields can be auto-detected.
JobId jobId = JobId.newBuilder().setLocation(location).build();
TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration);
// Write data to writer
try (OutputStream stream = Channels.newOutputStream(writer)) {
Files.copy(csvPath, stream);
}
// Get load job
Job job = writer.getJob();
job = job.waitFor();
LoadStatistics stats = job.getStatistics();
return stats.getOutputRows();
// [END bigquery_load_from_file]
}
代码示例来源:origin: stackoverflow.com
AsynchronousSocketChannel clientChannel = AsynchronousSocketChannel.open();
Future connected = localSocket.connect(ourServerSocketAddress);
// later: if(future.isDone())
connected.get();
//Send something
OutputStream os = Channels.newOutputStream(clientChannel );
os.write (...)
代码示例来源:origin: org.elasticsearch/elasticsearch
private TranslogWriter(
final ChannelFactory channelFactory,
final ShardId shardId,
final Checkpoint initialCheckpoint,
final FileChannel channel,
final Path path,
final ByteSizeValue bufferSize,
final LongSupplier globalCheckpointSupplier, LongSupplier minTranslogGenerationSupplier, TranslogHeader header,
TragicExceptionHolder tragedy)
throws
IOException {
super(initialCheckpoint.generation, channel, path, header);
assert initialCheckpoint.offset == channel.position() :
"initial checkpoint offset [" + initialCheckpoint.offset + "] is different than current channel position ["
+ channel.position() + "]";
this.shardId = shardId;
this.channelFactory = channelFactory;
this.minTranslogGenerationSupplier = minTranslogGenerationSupplier;
this.outputStream = new BufferedChannelOutputStream(java.nio.channels.Channels.newOutputStream(channel), bufferSize.bytesAsInt());
this.lastSyncedCheckpoint = initialCheckpoint;
this.totalOffset = initialCheckpoint.offset;
assert initialCheckpoint.minSeqNo == SequenceNumbers.NO_OPS_PERFORMED : initialCheckpoint.minSeqNo;
this.minSeqNo = initialCheckpoint.minSeqNo;
assert initialCheckpoint.maxSeqNo == SequenceNumbers.NO_OPS_PERFORMED : initialCheckpoint.maxSeqNo;
this.maxSeqNo = initialCheckpoint.maxSeqNo;
assert initialCheckpoint.trimmedAboveSeqNo == SequenceNumbers.UNASSIGNED_SEQ_NO : initialCheckpoint.trimmedAboveSeqNo;
this.globalCheckpointSupplier = globalCheckpointSupplier;
this.seenSequenceNumbers = Assertions.ENABLED ? new HashMap<>() : null;
this.tragedy = tragedy;
}
代码示例来源:origin: AsamK/signal-cli
public void save() {
if (fileChannel == null) {
return;
}
ObjectNode rootNode = jsonProcessor.createObjectNode();
rootNode.put("username", username)
.put("deviceId", deviceId)
.put("isMultiDevice", isMultiDevice)
.put("password", password)
.put("registrationLockPin", registrationLockPin)
.put("signalingKey", signalingKey)
.put("preKeyIdOffset", preKeyIdOffset)
.put("nextSignedPreKeyId", nextSignedPreKeyId)
.put("profileKey", Base64.encodeBytes(profileKey))
.put("registered", registered)
.putPOJO("axolotlStore", signalProtocolStore)
.putPOJO("groupStore", groupStore)
.putPOJO("contactStore", contactStore)
.putPOJO("threadStore", threadStore)
;
try {
fileChannel.position(0);
jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode);
fileChannel.truncate(fileChannel.position());
fileChannel.force(false);
} catch (Exception e) {
System.err.println(String.format("Error saving file: %s", e.getMessage()));
}
}
代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit
@SuppressWarnings("resource" /* java 7 */)
private File toTemp(final int type, final byte[] buf, final int pos,
final int len) throws IOException, FileNotFoundException {
boolean delete = true;
File tmp = newTempFile();
try {
FileOutputStream fOut = new FileOutputStream(tmp);
try {
OutputStream out = fOut;
if (config.getFSyncObjectFiles())
out = Channels.newOutputStream(fOut.getChannel());
DeflaterOutputStream cOut = compress(out);
writeHeader(cOut, type, len);
cOut.write(buf, pos, len);
cOut.finish();
} finally {
if (config.getFSyncObjectFiles())
fOut.getChannel().force(true);
fOut.close();
}
delete = false;
return tmp;
} finally {
if (delete)
FileUtils.delete(tmp, FileUtils.RETRY);
}
}
代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit
OutputStream out = fOut;
if (config.getFSyncObjectFiles())
out = Channels.newOutputStream(fOut.getChannel());
DeflaterOutputStream cOut = compress(out);
SHA1OutputStream dOut = new SHA1OutputStream(cOut, md);
代码示例来源:origin: org.elasticsearch/elasticsearch
/**
* Writes this header with the latest format into the file channel
*/
void write(final FileChannel channel) throws IOException {
// This output is intentionally not closed because closing it will close the FileChannel.
@SuppressWarnings({"IOResourceOpenedButNotSafelyClosed", "resource"})
final BufferedChecksumStreamOutput out = new BufferedChecksumStreamOutput(
new OutputStreamStreamOutput(java.nio.channels.Channels.newOutputStream(channel)));
CodecUtil.writeHeader(new OutputStreamDataOutput(out), TRANSLOG_CODEC, CURRENT_VERSION);
// Write uuid
final BytesRef uuid = new BytesRef(translogUUID);
out.writeInt(uuid.length);
out.writeBytes(uuid.bytes, uuid.offset, uuid.length);
// Write primary term
out.writeLong(primaryTerm);
// Checksum header
out.writeInt((int) out.getChecksum());
out.flush();
channel.force(true);
assert channel.position() == headerSizeInBytes :
"Header is not fully written; header size [" + headerSizeInBytes + "], channel position [" + channel.position() + "]";
}
}
内容来源于网络,如有侵权,请联系作者删除!