use of java.io.RandomAccessFile in project j2objc by google.
the class OldRandomAccessFileTest method test_write$BII.
/**
* java.io.RandomAccessFile#write(byte[], int, int)
*/
public void test_write$BII() throws Exception {
RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
byte[] rbuf = new byte[4000];
byte[] testBuf = null;
int bytesRead;
try {
raf.write(testBuf, 1, 1);
fail("Test 1: NullPointerException expected.");
} catch (NullPointerException e) {
// Expected.
}
testBuf = testString.getBytes();
try {
raf.write(testBuf, -1, 10);
fail("Test 2: IndexOutOfBoundsException expected.");
} catch (IndexOutOfBoundsException expected) {
}
try {
raf.write(testBuf, 0, -1);
fail("Test 3: IndexOutOfBoundsException expected.");
} catch (IndexOutOfBoundsException expected) {
}
try {
raf.write(testBuf, 5, testLength);
fail("Test 4: IndexOutOfBoundsException expected.");
} catch (IndexOutOfBoundsException expected) {
}
// Positive test: The following write should not fail.
try {
raf.write(testBuf, 3, testLength - 5);
} catch (Exception e) {
fail("Test 5: Unexpected exception: " + e.getMessage());
}
raf.close();
// Writing nothing to a closed file should not fail either.
try {
raf.write(new byte[0]);
} catch (IOException e) {
fail("Test 6: Unexpected IOException: " + e.getMessage());
}
// Writing something to a closed file should fail.
try {
raf.write(testString.getBytes());
fail("Test 7: IOException expected.");
} catch (IOException e) {
// Expected.
}
FileInputStream fis = new java.io.FileInputStream(fileName);
bytesRead = fis.read(rbuf, 0, testLength);
assertEquals("Test 8: Incorrect number of bytes written or read;", testLength - 5, bytesRead);
assertEquals("Test 9: Incorrect bytes written or read; ", testString.substring(3, testLength - 2), new String(rbuf, 0, bytesRead));
}
use of java.io.RandomAccessFile in project j2objc by google.
the class OldRandomAccessFileTest method test_readUnsignedByte.
/**
* java.io.RandomAccessFile#readUnsignedByte()
*/
public void test_readUnsignedByte() throws IOException {
RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
raf.writeByte(-1);
raf.seek(0);
assertEquals("Test 1: Incorrect value written or read;", 255, raf.readUnsignedByte());
try {
raf.readUnsignedByte();
fail("Test 2: EOFException expected.");
} catch (EOFException e) {
// Expected.
}
raf.close();
try {
raf.readUnsignedByte();
fail("Test 3: IOException expected.");
} catch (IOException e) {
// Expected.
}
}
use of java.io.RandomAccessFile in project j2objc by google.
the class OldRandomAccessFileTest method test_read_writeShort.
/**
* java.io.RandomAccessFile#readShort()
* java.io.RandomAccessFile#writeShort(short)
*/
public void test_read_writeShort() throws IOException {
RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
raf.writeShort(Short.MIN_VALUE);
raf.writeShort('T');
raf.writeShort(Short.MAX_VALUE);
raf.writeShort(Short.MIN_VALUE - 1);
raf.writeShort(Short.MAX_VALUE + 1);
raf.seek(0);
assertEquals("Test 1: Incorrect value written or read;", Short.MIN_VALUE, raf.readShort());
assertEquals("Test 2: Incorrect value written or read;", 'T', raf.readShort());
assertEquals("Test 3: Incorrect value written or read;", Short.MAX_VALUE, raf.readShort());
assertEquals("Test 4: Incorrect value written or read;", 0x7fff, raf.readShort());
assertEquals("Test 5: Incorrect value written or read;", (short) 0x8000, raf.readShort());
try {
raf.readShort();
fail("Test 6: EOFException expected.");
} catch (EOFException e) {
// Expected.
}
raf.close();
try {
raf.writeShort('E');
fail("Test 7: IOException expected.");
} catch (IOException e) {
// Expected.
}
try {
raf.readShort();
fail("Test 8: IOException expected.");
} catch (IOException e) {
// Expected.
}
}
use of java.io.RandomAccessFile in project j2objc by google.
the class OldRandomAccessFileTest method test_ConstructorLjava_lang_StringLjava_lang_String.
/**
* java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
* java.lang.String)
*/
public void test_ConstructorLjava_lang_StringLjava_lang_String() throws IOException {
RandomAccessFile raf = null;
File tmpFile = new File(fileName);
try {
raf = new java.io.RandomAccessFile(fileName, "r");
fail("Test 1: FileNotFoundException expected.");
} catch (FileNotFoundException e) {
// Expected.
} catch (IllegalArgumentException e) {
fail("Test 2: Unexpected IllegalArgumentException: " + e.getMessage());
}
try {
// Checking the remaining valid mode parameters.
try {
raf = new java.io.RandomAccessFile(fileName, "rwd");
} catch (IllegalArgumentException e) {
fail("Test 3: Unexpected IllegalArgumentException: " + e.getMessage());
}
raf.close();
try {
raf = new java.io.RandomAccessFile(fileName, "rws");
} catch (IllegalArgumentException e) {
fail("Test 4: Unexpected IllegalArgumentException: " + e.getMessage());
}
raf.close();
try {
raf = new java.io.RandomAccessFile(fileName, "rw");
} catch (IllegalArgumentException e) {
fail("Test 5: Unexpected IllegalArgumentException: " + e.getMessage());
}
raf.close();
// Checking an invalid mode parameter.
try {
raf = new java.io.RandomAccessFile(fileName, "i");
fail("Test 6: IllegalArgumentException expected.");
} catch (IllegalArgumentException e) {
// Expected.
}
// Checking for NoWritableChannelException.
raf = new java.io.RandomAccessFile(fileName, "r");
FileChannel fcr = raf.getChannel();
try {
fcr.lock(0L, Long.MAX_VALUE, false);
fail("Test 7: NonWritableChannelException expected.");
} catch (NonWritableChannelException e) {
// Expected.
}
} finally {
if (raf != null)
raf.close();
if (tmpFile.exists())
tmpFile.delete();
}
}
use of java.io.RandomAccessFile in project j2objc by google.
the class OldRandomAccessFileTest method test_read_writeInt.
/**
* java.io.RandomAccessFile#readInt()
* java.io.RandomAccessFile#writeInt(char)
*/
public void test_read_writeInt() throws IOException {
RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
raf.writeInt(Integer.MIN_VALUE);
raf.writeInt('T');
raf.writeInt(Integer.MAX_VALUE);
raf.writeInt(Integer.MIN_VALUE - 1);
raf.writeInt(Integer.MAX_VALUE + 1);
raf.seek(0);
assertEquals("Test 1: Incorrect value written or read;", Integer.MIN_VALUE, raf.readInt());
assertEquals("Test 2: Incorrect value written or read;", 'T', raf.readInt());
assertEquals("Test 3: Incorrect value written or read;", Integer.MAX_VALUE, raf.readInt());
assertEquals("Test 4: Incorrect value written or read;", 0x7fffffff, raf.readInt());
assertEquals("Test 5: Incorrect value written or read;", 0x80000000, raf.readInt());
try {
raf.readInt();
fail("Test 6: EOFException expected.");
} catch (EOFException e) {
// Expected.
}
raf.close();
try {
raf.writeInt('E');
fail("Test 7: IOException expected.");
} catch (IOException e) {
// Expected.
}
try {
raf.readInt();
fail("Test 8: IOException expected.");
} catch (IOException e) {
// Expected.
}
}
Aggregations