本文整理了Java中java.nio.channels.FileChannel.transferTo()
方法的一些代码示例,展示了FileChannel.transferTo()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileChannel.transferTo()
方法的具体详情如下:
包路径:java.nio.channels.FileChannel
类名称:FileChannel
方法名:transferTo
[英]Reads up to count bytes from this channel's file starting at position and writes them to target. No bytes are transferred if position is larger than the size of this channel's file. Less than count bytes are transferred if there less bytes available from this channel's file or if the target channel is non-blocking and has less than count bytes free in its input buffer.
Note that this channel's position is not modified.
[中]从该通道的文件中读取最多个字节(从位置开始),并将其写入目标。如果位置大于此通道文件的大小,则不传输字节。如果此通道的文件中可用字节数较少,或者如果目标通道为非阻塞通道且其输入缓冲区中的可用字节数少于count字节数,则传输少于count字节数的字节数。
请注意,此通道的位置未修改。
代码示例来源:origin: stackoverflow.com
public void copy(File src, File dst) throws IOException {
FileInputStream inStream = new FileInputStream(src);
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
}
代码示例来源:origin: square/okio
@Override public long read(Buffer sink, long byteCount) throws IOException {
if (!channel.isOpen()) throw new IllegalStateException("closed");
if (position == channel.size()) return -1L;
long read = channel.transferTo(position, byteCount, sink);
position += read;
return read;
}
代码示例来源:origin: sannies/mp4parser
public void getBox(WritableByteChannel writableByteChannel) throws IOException {
writableByteChannel.write((ByteBuffer) header.rewind());
FileChannel fc = new FileInputStream(dataFile).getChannel();
fc.transferTo(0, dataFile.lastModified(), writableByteChannel);
fc.close();
}
代码示例来源:origin: stackoverflow.com
import java.io.*;
import java.nio.channels.*;
import javax.xml.bind.DatatypeConverter;
public class EncodeDecode {
public static void main(String[] args) throws Exception {
File file = new File("/bin/ls");
byte[] bytes = loadFile(file, new ByteArrayOutputStream()).toByteArray();
String encoded = DatatypeConverter.printBase64Binary(bytes);
System.out.println(encoded);
byte[] decoded = DatatypeConverter.parseBase64Binary(encoded);
// check
for (int i = 0; i < bytes.length; i++) {
assert bytes[i] == decoded[i];
}
}
private static <T extends OutputStream> T loadFile(File file, T out)
throws IOException {
FileChannel in = new FileInputStream(file).getChannel();
try {
assert in.size() == in.transferTo(0, in.size(), Channels.newChannel(out));
return out;
} finally {
in.close();
}
}
}
代码示例来源:origin: stackoverflow.com
FileChannel toChannel = null;
try {
fromChannel = fromFile.getChannel();
toChannel = toFile.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
} finally {
try {
代码示例来源:origin: org.apache.xmlbeans/xmlbeans
FileChannel target = ((FileOutputStream) output).getChannel();
FileChannel source = ((FileInputStream) input).getChannel();
source.transferTo(0, Integer.MAX_VALUE, target);
代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel
FileInputStream fileInputStream = (FileInputStream)inputStream;
FileChannel fileChannel = fileInputStream.getChannel();
fileChannel.transferTo(
start, length, Channels.newChannel(outputStream));
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* A raw file copy function -- this is not public since no error checks are made as to the
* consistency of the file being copied. Use instead:
* @see IOUtils#cp(java.io.File, java.io.File, boolean)
* @param source The source file. This is guaranteed to exist, and is guaranteed to be a file.
* @param target The target file.
* @throws IOException Throws an exception if the copy fails.
*/
private static void copyFile(File source, File target) throws IOException {
FileChannel sourceChannel = new FileInputStream( source ).getChannel();
FileChannel targetChannel = new FileOutputStream( target ).getChannel();
// allow for the case that it doesn't all transfer in one go (though it probably does for a file cp)
long pos = 0;
long toCopy = sourceChannel.size();
while (toCopy > 0) {
long bytes = sourceChannel.transferTo(pos, toCopy, targetChannel);
pos += bytes;
toCopy -= bytes;
}
sourceChannel.close();
targetChannel.close();
}
代码示例来源:origin: stackoverflow.com
try (FileInputStream in = new FileInputStream(file);
FileChannel channel = in.getChannel()) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
channel.transferTo(0, channel.size(), Channels.newChannel(out));
return out.toByteArray();
代码示例来源:origin: stackoverflow.com
new FileInputStream(file).getChannel().transferTo(otherChannel);
代码示例来源:origin: plutext/docx4j
@Override
public void copyTo(OutputStream stream) throws IOException {
// Wrap the OutputSteam as a channel
WritableByteChannel out = Channels.newChannel(stream);
// Now do the transfer
channel.transferTo(0, channel.size(), out);
}
代码示例来源:origin: looly/hutool
/**
* 拷贝文件流,使用NIO
*
* @param in 输入
* @param out 输出
* @return 拷贝的字节数
* @throws IORuntimeException IO异常
*/
public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException {
Assert.notNull(in, "FileInputStream is null!");
Assert.notNull(out, "FileOutputStream is null!");
final FileChannel inChannel = in.getChannel();
final FileChannel outChannel = out.getChannel();
try {
return inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
代码示例来源:origin: apache/rocketmq
static public void copyFile(String source, String target) throws IOException {
File sf = new File(source);
if (!sf.exists()) {
throw new IllegalArgumentException("source file does not exist.");
}
File tf = new File(target);
tf.getParentFile().mkdirs();
if (!tf.exists() && !tf.createNewFile()) {
throw new RuntimeException("failed to create target file.");
}
FileChannel sc = null;
FileChannel tc = null;
try {
tc = new FileOutputStream(tf).getChannel();
sc = new FileInputStream(sf).getChannel();
sc.transferTo(0, sc.size(), tc);
} finally {
if (null != sc) {
sc.close();
}
if (null != tc) {
tc.close();
}
}
}
代码示例来源:origin: alibaba/nacos
response.setDateHeader("Last-Modified", lastModified);
} else {
fis = new FileInputStream(file);
response.setDateHeader("Last-Modified", file.lastModified());
out.close();
} else {
fis.getChannel().transferTo(0L, fis.getChannel().size(),
Channels.newChannel(response.getOutputStream()));
代码示例来源:origin: org.apache.poi/poi
@Override
public void copyTo(OutputStream stream) throws IOException {
// Wrap the OutputSteam as a channel
try (WritableByteChannel out = Channels.newChannel(stream)) {
// Now do the transfer
channel.transferTo(0, channel.size(), out);
}
}
代码示例来源:origin: looly/hutool
/**
* 拷贝文件流,使用NIO
*
* @param in 输入
* @param out 输出
* @return 拷贝的字节数
* @throws IORuntimeException IO异常
*/
public static long copy(FileInputStream in, FileOutputStream out) throws IORuntimeException {
Assert.notNull(in, "FileInputStream is null!");
Assert.notNull(out, "FileOutputStream is null!");
final FileChannel inChannel = in.getChannel();
final FileChannel outChannel = out.getChannel();
try {
return inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
代码示例来源:origin: spotbugs/spotbugs
/**
* Copy a File
*
* @param in
* to Copy From
* @param out
* to Copy To
* @throws IOException
*/
private static void copyFile(File in, File out) throws IOException {
try (FileInputStream inStream = new FileInputStream(in);
FileOutputStream outStream = new FileOutputStream(out);) {
FileChannel inChannel = inStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outStream.getChannel());
}
}
代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel
public static void write(MimeResponse mimeResponse, File file)
throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(file)) {
FileChannel fileChannel = fileInputStream.getChannel();
long contentLength = fileChannel.size();
if (mimeResponse instanceof ResourceResponse) {
ResourceResponse resourceResponse =
(ResourceResponse)mimeResponse;
setContentLength(resourceResponse, contentLength);
}
fileChannel.transferTo(
0, contentLength,
Channels.newChannel(mimeResponse.getPortletOutputStream()));
}
}
代码示例来源:origin: google/guava
long copied;
do {
copied = sourceChannel.transferTo(position, ZERO_COPY_CHUNK_SIZE, to);
position += copied;
sourceChannel.position(position);
} while (copied > 0 || position < sourceChannel.size());
return position - oldPosition;
代码示例来源:origin: spockframework/spock
private static void doCopyFile(File source, File target) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(source);
out = new FileOutputStream(target);
// instead of checking transferred size, we'll compare file sizes in checkSameSize()
in.getChannel().transferTo(0, Long.MAX_VALUE, out.getChannel());
} finally {
IoUtil.closeQuietly(in, out);
}
}
内容来源于网络,如有侵权,请联系作者删除!