Open FileDescriptor for reading and writing with a Channel
I have a FileDescriptor
and would like to open it for reading and writing using a single channel. I can use streams like so to get one channel for reading, and one channel for writing, but I would prefer a single channel.
FileChannel in = new FileInputStream(fd).getChannel();
FileChannel out = new FileOutputStream(fd).getChannel();
java
add a comment |
I have a FileDescriptor
and would like to open it for reading and writing using a single channel. I can use streams like so to get one channel for reading, and one channel for writing, but I would prefer a single channel.
FileChannel in = new FileInputStream(fd).getChannel();
FileChannel out = new FileOutputStream(fd).getChannel();
java
Why? Applications should not create their own file descriptors.
– user207421
Jul 26 '16 at 5:23
I'm implementing a VPNService on android, it provides me with the FileDescriptors.
– RedHatter
Jul 26 '16 at 5:32
add a comment |
I have a FileDescriptor
and would like to open it for reading and writing using a single channel. I can use streams like so to get one channel for reading, and one channel for writing, but I would prefer a single channel.
FileChannel in = new FileInputStream(fd).getChannel();
FileChannel out = new FileOutputStream(fd).getChannel();
java
I have a FileDescriptor
and would like to open it for reading and writing using a single channel. I can use streams like so to get one channel for reading, and one channel for writing, but I would prefer a single channel.
FileChannel in = new FileInputStream(fd).getChannel();
FileChannel out = new FileOutputStream(fd).getChannel();
java
java
asked Jul 26 '16 at 4:28
RedHatterRedHatter
81811228
81811228
Why? Applications should not create their own file descriptors.
– user207421
Jul 26 '16 at 5:23
I'm implementing a VPNService on android, it provides me with the FileDescriptors.
– RedHatter
Jul 26 '16 at 5:32
add a comment |
Why? Applications should not create their own file descriptors.
– user207421
Jul 26 '16 at 5:23
I'm implementing a VPNService on android, it provides me with the FileDescriptors.
– RedHatter
Jul 26 '16 at 5:32
Why? Applications should not create their own file descriptors.
– user207421
Jul 26 '16 at 5:23
Why? Applications should not create their own file descriptors.
– user207421
Jul 26 '16 at 5:23
I'm implementing a VPNService on android, it provides me with the FileDescriptors.
– RedHatter
Jul 26 '16 at 5:32
I'm implementing a VPNService on android, it provides me with the FileDescriptors.
– RedHatter
Jul 26 '16 at 5:32
add a comment |
1 Answer
1
active
oldest
votes
Been working with FileChannel
recently, yes you can use 1 FileChannel
for reading and writing as long as you use methods that do not modify the channel's position
. In fact this is the only way you will have a consistent view of the file as write may be paged/cached by the OS that is yet to be "visible" from the other reader at least before flush.
This is excerpt from the Javadoc
File channels are safe for use by multiple concurrent threads. The
close method may be invoked at any time, as specified by the Channel
interface. Only one operation that involves the channel's position or
can change its file's size may be in progress at any given time;
attempts to initiate a second such operation while the first is still
in progress will block until the first operation completes. Other
operations, in particular those that take an explicit position, may
proceed concurrently; whether they in fact do so is dependent upon the
underlying implementation and is therefore unspecified.
I use single threaded write (that changes position
) and multithreaded reads (that do not change position
ie. read(ByteBuffer dst, long position)
)
Cheers
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38581024%2fopen-filedescriptor-for-reading-and-writing-with-a-channel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Been working with FileChannel
recently, yes you can use 1 FileChannel
for reading and writing as long as you use methods that do not modify the channel's position
. In fact this is the only way you will have a consistent view of the file as write may be paged/cached by the OS that is yet to be "visible" from the other reader at least before flush.
This is excerpt from the Javadoc
File channels are safe for use by multiple concurrent threads. The
close method may be invoked at any time, as specified by the Channel
interface. Only one operation that involves the channel's position or
can change its file's size may be in progress at any given time;
attempts to initiate a second such operation while the first is still
in progress will block until the first operation completes. Other
operations, in particular those that take an explicit position, may
proceed concurrently; whether they in fact do so is dependent upon the
underlying implementation and is therefore unspecified.
I use single threaded write (that changes position
) and multithreaded reads (that do not change position
ie. read(ByteBuffer dst, long position)
)
Cheers
add a comment |
Been working with FileChannel
recently, yes you can use 1 FileChannel
for reading and writing as long as you use methods that do not modify the channel's position
. In fact this is the only way you will have a consistent view of the file as write may be paged/cached by the OS that is yet to be "visible" from the other reader at least before flush.
This is excerpt from the Javadoc
File channels are safe for use by multiple concurrent threads. The
close method may be invoked at any time, as specified by the Channel
interface. Only one operation that involves the channel's position or
can change its file's size may be in progress at any given time;
attempts to initiate a second such operation while the first is still
in progress will block until the first operation completes. Other
operations, in particular those that take an explicit position, may
proceed concurrently; whether they in fact do so is dependent upon the
underlying implementation and is therefore unspecified.
I use single threaded write (that changes position
) and multithreaded reads (that do not change position
ie. read(ByteBuffer dst, long position)
)
Cheers
add a comment |
Been working with FileChannel
recently, yes you can use 1 FileChannel
for reading and writing as long as you use methods that do not modify the channel's position
. In fact this is the only way you will have a consistent view of the file as write may be paged/cached by the OS that is yet to be "visible" from the other reader at least before flush.
This is excerpt from the Javadoc
File channels are safe for use by multiple concurrent threads. The
close method may be invoked at any time, as specified by the Channel
interface. Only one operation that involves the channel's position or
can change its file's size may be in progress at any given time;
attempts to initiate a second such operation while the first is still
in progress will block until the first operation completes. Other
operations, in particular those that take an explicit position, may
proceed concurrently; whether they in fact do so is dependent upon the
underlying implementation and is therefore unspecified.
I use single threaded write (that changes position
) and multithreaded reads (that do not change position
ie. read(ByteBuffer dst, long position)
)
Cheers
Been working with FileChannel
recently, yes you can use 1 FileChannel
for reading and writing as long as you use methods that do not modify the channel's position
. In fact this is the only way you will have a consistent view of the file as write may be paged/cached by the OS that is yet to be "visible" from the other reader at least before flush.
This is excerpt from the Javadoc
File channels are safe for use by multiple concurrent threads. The
close method may be invoked at any time, as specified by the Channel
interface. Only one operation that involves the channel's position or
can change its file's size may be in progress at any given time;
attempts to initiate a second such operation while the first is still
in progress will block until the first operation completes. Other
operations, in particular those that take an explicit position, may
proceed concurrently; whether they in fact do so is dependent upon the
underlying implementation and is therefore unspecified.
I use single threaded write (that changes position
) and multithreaded reads (that do not change position
ie. read(ByteBuffer dst, long position)
)
Cheers
answered Nov 15 '18 at 8:29
ahasaniahasani
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38581024%2fopen-filedescriptor-for-reading-and-writing-with-a-channel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why? Applications should not create their own file descriptors.
– user207421
Jul 26 '16 at 5:23
I'm implementing a VPNService on android, it provides me with the FileDescriptors.
– RedHatter
Jul 26 '16 at 5:32