Search in sources :

Example 36 with RandomAccessFile

use of java.io.RandomAccessFile in project j2objc by google.

the class OldRandomAccessFileTest method test_readFully$B_writeBytesLjava_lang_String.

/**
     * java.io.RandomAccessFile#writeBytes(java.lang.String)
     * java.io.RandomAccessFile#readFully(byte[])
     */
public void test_readFully$B_writeBytesLjava_lang_String() throws IOException {
    byte[] buf = new byte[testLength];
    RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
    raf.writeBytes(testString);
    raf.seek(0);
    try {
        raf.readFully(null);
        fail("Test 1: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected.
    }
    raf.readFully(buf);
    assertEquals("Test 2: Incorrect bytes written or read;", testString, new String(buf));
    try {
        raf.readFully(buf);
        fail("Test 3: EOFException expected.");
    } catch (EOFException e) {
    // Expected.
    }
    raf.close();
    try {
        raf.writeBytes("Already closed.");
        fail("Test 4: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    try {
        raf.readFully(buf);
        fail("Test 5: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 37 with RandomAccessFile

use of java.io.RandomAccessFile in project j2objc by google.

the class OldRandomAccessFileTest method test_seekJ.

/**
     * java.io.RandomAccessFile#seek(long)
     */
public void test_seekJ() throws IOException {
    // Test for method void java.io.RandomAccessFile.seek(long)
    RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
    try {
        raf.seek(-1);
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    raf.write(testString.getBytes(), 0, testLength);
    raf.seek(12);
    assertEquals("Test 3: Seek failed to set file pointer.", 12, raf.getFilePointer());
    raf.close();
    try {
        raf.seek(1);
        fail("Test 4: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException)

Example 38 with RandomAccessFile

use of java.io.RandomAccessFile in project j2objc by google.

the class OldRandomAccessFileTest method test_ConstructorLjava_io_FileLjava_lang_String.

/**
     * java.io.RandomAccessFile#RandomAccessFile(java.io.File,
     *        java.lang.String)
     */
public void test_ConstructorLjava_io_FileLjava_lang_String() throws Exception {
    RandomAccessFile raf = null;
    File tmpFile = new File(fileName);
    try {
        raf = new java.io.RandomAccessFile(tmpFile, "r");
        fail("Test 1: FileNotFoundException expected.");
    } catch (FileNotFoundException e) {
    // Expected.
    } catch (IllegalArgumentException e) {
        fail("Test 2: Unexpected IllegalArgumentException: " + e.getMessage());
    }
    tmpFile.createNewFile();
    try {
        // Checking the remaining valid mode parameters.
        try {
            raf = new java.io.RandomAccessFile(tmpFile, "rwd");
        } catch (IllegalArgumentException e) {
            fail("Test 3: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        try {
            raf = new java.io.RandomAccessFile(tmpFile, "rws");
        } catch (IllegalArgumentException e) {
            fail("Test 4: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        try {
            raf = new java.io.RandomAccessFile(tmpFile, "rw");
        } catch (IllegalArgumentException e) {
            fail("Test 5: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        // Checking an invalid mode parameter.
        try {
            raf = new java.io.RandomAccessFile(tmpFile, "i");
            fail("Test 6: IllegalArgumentException expected.");
        } catch (IllegalArgumentException e) {
        // Expected.
        }
    } finally {
        if (raf != null)
            raf.close();
        tmpFile.delete();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) FileNotFoundException(java.io.FileNotFoundException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 39 with RandomAccessFile

use of java.io.RandomAccessFile in project j2objc by google.

the class OldRandomAccessFileTest method test_readUnsignedShort.

/**
     * java.io.RandomAccessFile#readUnsignedShort()
     */
public void test_readUnsignedShort() throws IOException {
    RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
    raf.writeShort(-1);
    raf.seek(0);
    assertEquals("Test 1: Incorrect value written or read;", 65535, raf.readUnsignedShort());
    try {
        raf.readUnsignedShort();
        fail("Test 2: EOFException expected.");
    } catch (EOFException e) {
    // Expected.
    }
    raf.close();
    try {
        raf.readUnsignedShort();
        fail("Test 3: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 40 with RandomAccessFile

use of java.io.RandomAccessFile in project j2objc by google.

the class OldRandomAccessFileTest method test_skipBytesI.

/**
     * java.io.RandomAccessFile#skipBytes(int)
     */
public void test_skipBytesI() throws IOException {
    byte[] buf = new byte[5];
    RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
    raf.writeBytes("HelloWorld");
    raf.seek(0);
    assertTrue("Test 1: Nothing should be skipped if parameter is less than zero", raf.skipBytes(-1) == 0);
    assertEquals("Test 4: Incorrect number of bytes skipped; ", 5, raf.skipBytes(5));
    raf.readFully(buf);
    assertEquals("Test 3: Failed to skip bytes.", "World", new String(buf, 0, 5));
    raf.seek(0);
    assertEquals("Test 4: Incorrect number of bytes skipped; ", 10, raf.skipBytes(20));
    raf.close();
    try {
        raf.skipBytes(1);
        fail("Test 5: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException)

Aggregations

RandomAccessFile (java.io.RandomAccessFile)916 IOException (java.io.IOException)446 File (java.io.File)366 FileChannel (java.nio.channels.FileChannel)139 FileNotFoundException (java.io.FileNotFoundException)88 Test (org.junit.Test)82 ByteBuffer (java.nio.ByteBuffer)81 FileLock (java.nio.channels.FileLock)66 EOFException (java.io.EOFException)50 FileOutputStream (java.io.FileOutputStream)50 FileInputStream (java.io.FileInputStream)39 InputStream (java.io.InputStream)36 MappedByteBuffer (java.nio.MappedByteBuffer)36 Random (java.util.Random)27 ByteArrayInputStream (java.io.ByteArrayInputStream)25 BufferedInputStream (java.io.BufferedInputStream)22 DataInputStream (java.io.DataInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 Configuration (org.apache.hadoop.conf.Configuration)17 DataOutputStream (java.io.DataOutputStream)14