package java7.file;

import libs.junit.UnitTestRunner;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Email: beyondblog@outlook.com
 * Date: 15/3/19
 * Time: 上午7:01
 * Description:
 * java/io/RandomAccessFile#open(Ljava/lang/String;I)V
 * java/io/RandomAccessFile#read0()I
 * java/io/RandomAccessFile#readBytes([BII)I
 * java/io/RandomAccessFile#write0(I)V
 * java/io/RandomAccessFile#writeBytes([BII)V
 * java/io/RandomAccessFile#getFilePointer()J
 * java/io/RandomAccessFile#seek0(J)V
 * java/io/RandomAccessFile#length()J
 * java/io/RandomAccessFile#setLength(J)V
 * java/io/RandomAccessFile.initIDs()V
 * java/io/RandomAccessFile#close0()V
 */
public class RandomAccessFileTest {
    public static void main(String[] args) throws Exception {
        UnitTestRunner.run(RandomAccessFileTest.class);
    }

    @Test
    public void writeTest() throws Exception {
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile("RandomAccessFileWriteTest", "rw");
            //write data
            randomAccessFile.writeBytes("hello");
            randomAccessFile.writeInt(54);
            randomAccessFile.writeBytes("world");
            randomAccessFile.writeInt(-57);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            randomAccessFile.close();
        }
    }

    @Test
    public void readTest() throws Exception {
        RandomAccessFile randomAccessFile = new RandomAccessFile("RandomAccessFileWriteTest", "rw");
        try {
            byte data[] = new byte[5];
            randomAccessFile.read(data);
            String result = new String(data);
            Assert.assertTrue(result.equals("hello"));
            int intValue = randomAccessFile.readInt();
            Assert.assertTrue(intValue == 54);
            randomAccessFile.read(data);
            result = new String(data);
            Assert.assertTrue(result.equals("world"));
            intValue = randomAccessFile.readInt();
            Assert.assertTrue(intValue == -57);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            randomAccessFile.close();
        }
    }

    @Test
    public void seekTest() throws Exception {
        RandomAccessFile randomAccessFile = new RandomAccessFile("RandomAccessFileWriteTest", "rw");
        try {
            //write data
            randomAccessFile.writeBytes("hello");
            randomAccessFile.writeInt(54);
            randomAccessFile.writeBytes("world");
            randomAccessFile.writeInt(-57);
            randomAccessFile.seek(9);
            byte data[] = new byte[5];
            randomAccessFile.read(data);
            String result = new String(data);
            //long seek = randomAccessFile.getFilePointer();
            //Assert.assertTrue(seek == 14);
            Assert.assertTrue(result.equals("world"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            randomAccessFile.close();
        }
    }
}

@@ -795,16 +795,21 @@
 eTest %7B%0A
+    %0A
     publ
@@ -962,32 +962,34 @@
 teTest() throws 
+IO
 Exception %7B%0A    
@@ -984,32 +984,37 @@
 ption %7B%0A        
+try (
 RandomAccessFile
@@ -1036,59 +1036,8 @@
 le =
- null;%0A        try %7B%0A            randomAccessFile =
  new
@@ -1080,33 +1080,35 @@
 riteTest%22, %22rw%22)
-;
+) %7B
 %0A            //w
@@ -1308,133 +1308,8 @@
 7);%0A
-        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D finally %7B%0A            randomAccessFile.close();%0A
     
@@ -1357,32 +1357,34 @@
 adTest() throws 
+IO
 Exception %7B%0A    
@@ -1379,32 +1379,37 @@
 ption %7B%0A        
+try (
 RandomAccessFile
@@ -1479,37 +1479,25 @@
 Test%22, %22rw%22)
-;%0A        try
+)
  %7B%0A         
@@ -2008,133 +2008,8 @@
 7);%0A
-        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D finally %7B%0A            randomAccessFile.close();%0A
     
@@ -2085,16 +2085,21 @@
         
+try (
 RandomAc
@@ -2185,21 +2185,9 @@
 rw%22)
-;%0A        try
+)
  %7B%0A 
@@ -2726,133 +2726,8 @@
 ));%0A
-        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D finally %7B%0A            randomAccessFile.close();%0A
     
@@ -2738,10 +2738,15 @@
 %7D%0A    %7D%0A
+    %0A
 %7D%0A

use java7 TWR