の API を分析していますFileOutputStream
。
getChannel()
メソッドは を返すことができますnull
か? もしそうなら、どのような状況で?
の API を分析していますFileOutputStream
。
getChannel()
メソッドは を返すことができますnull
か? もしそうなら、どのような状況で?
メソッドFileOutputStream
getChannel()
コード
public FileChannel getChannel() {
synchronized (this) {
if (channel == null) {
channel = FileChannelImpl.open(fd, false, true, this, append);
/*
* Increment fd's use count. Invoking the channel's close()
* method will result in decrementing the use count set for
* the channel.
*/
fd.incrementAndGetUseCount();
}
return channel;
}
}
呼び出しFileChannelImpl.open()
、このコードは常に新しいオブジェクトを作成します
public static FileChannel open(FileDescriptor fd,
boolean readable, boolean writable,
Object parent, boolean append)
{
return new FileChannelImpl(fd, readable, writable, parent, append);
}
public FileChannel getChannel() {
synchronized (this) {
if (channel == null) {
channel = FileChannelImpl.open(fd, false, true, append, this);
fd.incrementAndGetUseCount();
}
return channel;
}
}
null
返品不可の番組。
FileChannelImpl#openのソースを見てください。
メソッドなOpen()
return new FileChannelImpl(...)
ので、新しい参照が作成されますが、それはできませんnull
Java ソースから:
/**
* Returns the unique {@link java.nio.channels.FileChannel FileChannel}
* object associated with this file output stream. </p>
*
* <p> The initial {@link java.nio.channels.FileChannel#position()
* </code>position<code>} of the returned channel will be equal to the
* number of bytes written to the file so far unless this stream is in
* append mode, in which case it will be equal to the size of the file.
* Writing bytes to this stream will increment the channel's position
* accordingly. Changing the channel's position, either explicitly or by
* writing, will change this stream's file position.
*
* @return the file channel associated with this file output stream
*
* @since 1.4
* @spec JSR-51
*/
public FileChannel getChannel() {
synchronized (this) {
if (channel == null) {
channel = FileChannelImpl.open(fd, false, true, append, this);
/*
* Increment fd's use count. Invoking the channel's close()
* method will result in decrementing the use count set for
* the channel.
*/
fd.incrementAndGetUseCount();
}
return channel;
}
}
そのため、返品できませんnull
。