Skip to content

Instantly share code, notes, and snippets.

@lukhnos
Last active August 29, 2015 14:23
Show Gist options
  • Save lukhnos/5bc1749cd8ed6507e62c to your computer and use it in GitHub Desktop.
Save lukhnos/5bc1749cd8ed6507e62c to your computer and use it in GitHub Desktop.
libcore.io.Posix.preadBytes bug reproducible sample
// 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();
}
}
}
@Tuktig
Copy link

Tuktig commented Aug 19, 2015

Hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment