Search in sources :

Example 41 with RandomAccessFile

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));
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) IOException(java.io.IOException) NonWritableChannelException(java.nio.channels.NonWritableChannelException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Example 42 with RandomAccessFile

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.
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 43 with RandomAccessFile

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.
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 44 with RandomAccessFile

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();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) NonWritableChannelException(java.nio.channels.NonWritableChannelException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 45 with RandomAccessFile

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.
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) 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