Last active
August 29, 2015 14:23
-
-
Save lukhnos/5bc1749cd8ed6507e62c to your computer and use it in GitHub Desktop.
libcore.io.Posix.preadBytes bug reproducible sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To reproduce the bug: | |
// | |
// j2objc PreadBug.java | |
// j2objcc PreadBug.m | |
// ./a.out PreadBug | |
// | |
// You'll see the following exception: | |
// | |
// java.io.IOException: pread failed: EBADF (Bad file descriptor) | |
import java.io.*; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.FileChannel; | |
public class PreadBug { | |
public static void main(String[] args) { | |
try { | |
byte[] bytesToWrite = "hello, world!".getBytes("UTF-8"); | |
File tmpFile = File.createTempFile("preadbug-", ".tmp"); | |
FileOutputStream fos = new FileOutputStream(tmpFile); | |
fos.write(bytesToWrite); | |
fos.close(); | |
ByteBuffer buf = ByteBuffer.allocate(bytesToWrite.length); | |
RandomAccessFile raf = new RandomAccessFile(tmpFile, "r"); | |
FileChannel channel = raf.getChannel(); | |
int bytesRead = channel.read(buf, 0); | |
channel.close(); | |
String dstString = new String(buf.array(), "UTF-8"); | |
System.out.println(dstString); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi