Example usage for java.io RandomAccessFile close

List of usage examples for java.io RandomAccessFile close

Introduction

In this page you can find the example usage for java.io RandomAccessFile close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this random access file stream and releases any system resources associated with the stream.

Usage

From source file:org.apache.hadoop.hive.ql.io.TestRCFile.java

@Test
public void testReadCorruptFile() throws IOException, SerDeException {
    cleanup();//  w w w .j  av a2 s .  co  m

    byte[][] record = { null, null, null, null, null, null, null, null };

    RCFileOutputFormat.setColumnNumber(conf, expectedFieldsData.length);
    RCFile.Writer writer = new RCFile.Writer(fs, conf, file, null, new DefaultCodec());
    BytesRefArrayWritable bytes = new BytesRefArrayWritable(record.length);
    final int recCount = 100;
    Random rand = new Random();
    for (int recIdx = 0; recIdx < recCount; recIdx++) {
        for (int i = 0; i < record.length; i++) {
            record[i] = new Integer(rand.nextInt()).toString().getBytes("UTF-8");
        }
        for (int i = 0; i < record.length; i++) {
            BytesRefWritable cu = new BytesRefWritable(record[i], 0, record[i].length);
            bytes.set(i, cu);
        }
        writer.append(bytes);
        bytes.clear();
    }
    writer.close();

    // Insert junk in middle of file. Assumes file is on local disk.
    RandomAccessFile raf = new RandomAccessFile(file.toUri().getPath(), "rw");
    long corruptOffset = raf.length() / 2;
    LOG.info("corrupting " + raf + " at offset " + corruptOffset);
    raf.seek(corruptOffset);
    raf.writeBytes("junkjunkjunkjunkjunkjunkjunkjunk");
    raf.close();

    // Set the option for tolerating corruptions. The read should succeed.
    Configuration tmpConf = new Configuration(conf);
    tmpConf.setBoolean("hive.io.rcfile.tolerate.corruptions", true);
    RCFile.Reader reader = new RCFile.Reader(fs, file, tmpConf);

    LongWritable rowID = new LongWritable();

    while (true) {
        boolean more = reader.next(rowID);
        if (!more) {
            break;
        }
        BytesRefArrayWritable cols = new BytesRefArrayWritable();
        reader.getCurrentRow(cols);
        cols.resetValid(8);
    }

    reader.close();
}

From source file:io.pcp.parfait.dxm.FileByteBufferFactory.java

public ByteBuffer build(int length) throws IOException {
    RandomAccessFile fos = null;
    try {/*from  w ww .  j a v a2  s. c  o  m*/
        File parent = file.getParentFile();
        if (parent == null) {
            throw new RuntimeException("Could not find parent of output file " + file.getCanonicalPath());
        } else if (parent.exists()) {
            file.delete(); /* directory update visible to MMV PMDA */
            if (file.exists()) {
                throw new RuntimeException("Could not delete existing file " + file.getCanonicalPath());
            }
        } else if (!parent.mkdirs()) {
            throw new RuntimeException("Could not create output directory " + parent.getCanonicalPath());
        }
        fos = new RandomAccessFile(file, "rw");
        fos.setLength(length);
        ByteBuffer tempDataFile = fos.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
        tempDataFile.order(ByteOrder.nativeOrder());
        fos.close();

        return tempDataFile;
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
}

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

private void doTestCorruptCheckpointMeta(boolean backup) throws Exception {
    Map<String, String> overrides = Maps.newHashMap();
    overrides.put(FileChannelConfiguration.USE_DUAL_CHECKPOINTS, String.valueOf(backup));
    channel = createFileChannel(overrides);
    channel.start();//from w w w. ja  v a 2s .c  o m
    Assert.assertTrue(channel.isOpen());
    Set<String> in = putEvents(channel, "restart", 10, 100);
    Assert.assertEquals(100, in.size());
    forceCheckpoint(channel);
    if (backup) {
        Thread.sleep(2000);
    }
    channel.stop();
    File checkpoint = new File(checkpointDir, "checkpoint");
    RandomAccessFile writer = new RandomAccessFile(Serialization.getMetaDataFile(checkpoint), "rw");
    writer.seek(10);
    writer.writeLong(new Random().nextLong());
    writer.getFD().sync();
    writer.close();
    channel = createFileChannel(overrides);
    channel.start();
    Assert.assertTrue(channel.isOpen());
    Assert.assertTrue(!backup || channel.checkpointBackupRestored());
    Set<String> out = consumeChannel(channel);
    compareInputAndOut(in, out);
}

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

private void doTestBadCheckpointVersion(boolean backup) throws Exception {
    Map<String, String> overrides = Maps.newHashMap();
    overrides.put(FileChannelConfiguration.USE_DUAL_CHECKPOINTS, String.valueOf(backup));
    channel = createFileChannel(overrides);
    channel.start();//from w ww.  j av a 2s.  c  o m
    Assert.assertTrue(channel.isOpen());
    Set<String> in = putEvents(channel, "restart", 10, 100);
    Assert.assertEquals(100, in.size());
    forceCheckpoint(channel);
    if (backup) {
        Thread.sleep(2000);
    }
    channel.stop();
    File checkpoint = new File(checkpointDir, "checkpoint");
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    writer.seek(EventQueueBackingStoreFile.INDEX_VERSION * Serialization.SIZE_OF_LONG);
    writer.writeLong(2L);
    writer.getFD().sync();
    writer.close();
    channel = createFileChannel(overrides);
    channel.start();
    Assert.assertTrue(channel.isOpen());
    Assert.assertTrue(!backup || channel.checkpointBackupRestored());
    Set<String> out = consumeChannel(channel);
    compareInputAndOut(in, out);
}

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

private void testFastReplay(boolean shouldCorruptCheckpoint, boolean useFastReplay) throws Exception {
    Map<String, String> overrides = Maps.newHashMap();
    overrides.put(FileChannelConfiguration.USE_FAST_REPLAY, String.valueOf(useFastReplay));
    channel = createFileChannel(overrides);
    channel.start();/*from   w w  w . j  a va 2  s . c  o m*/
    Assert.assertTrue(channel.isOpen());
    Set<String> in = putEvents(channel, "restart", 10, 100);
    Assert.assertEquals(100, in.size());
    forceCheckpoint(channel);
    channel.stop();
    if (shouldCorruptCheckpoint) {
        File checkpoint = new File(checkpointDir, "checkpoint");
        RandomAccessFile writer = new RandomAccessFile(Serialization.getMetaDataFile(checkpoint), "rw");
        writer.seek(10);
        writer.writeLong(new Random().nextLong());
        writer.getFD().sync();
        writer.close();
    }
    channel = createFileChannel(overrides);
    channel.start();
    Assert.assertTrue(channel.isOpen());
    Set<String> out = consumeChannel(channel);
    if (useFastReplay && shouldCorruptCheckpoint) {
        Assert.assertTrue(channel.didFastReplay());
    } else {
        Assert.assertFalse(channel.didFastReplay());
    }
    compareInputAndOut(in, out);
}

From source file:ape.CorruptFileCommand.java

/**
 * This method is the actual method used to corrupt data/file
 *//*from   www.j  av  a  2  s  .c  o m*/
public boolean corrupt(String corruptAddress) throws IOException {
    FileInputStream fin;
    byte[] buf;
    int count;

    try {

        RandomAccessFile tmp = new RandomAccessFile(corruptAddress, "rw");
        tmp.seek(offset);

        if (size <= 0) {
            System.out.println("ERROR: The size parameter must be positive");
            Main.logger.info("ERROR: The size parameter must be positive");
            return false;
        }

        buf = new byte[size];

        count = 0;
        if ((count = tmp.read(buf, 0, size)) == -1) {
            System.out.println("The file chosen is smaller than the corruption size (" + size + " bytes)");
            Main.logger.info("The file chosen is smaller than the corruption size (" + size + " bytes)");
            return false;
        }

        for (int i = 0; i < count; i++) {
            buf[i] = 0x3;
        }

        tmp.seek(0);
        tmp.close();
    } catch (FileNotFoundException e1) {
        System.out.println("Cannot open the file on the path given");
        Main.logger.info("Cannot open the file on the path given");
        e1.printStackTrace();
        Main.logger.info(e1);
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    RandomAccessFile raf;
    try {
        raf = new RandomAccessFile(corruptAddress, "rw");
        try {
            raf.seek(offset);

            raf.write(buf, 0, count);
            raf.seek(0);
            raf.close();
        } catch (IOException e) {
            System.out.println("Corrupting file failed");
            Main.logger.info("Corrupting file failed");
            e.printStackTrace();
            Main.logger.info(e);
            return false;
        }

        return true;
    } catch (FileNotFoundException e1) {
        System.out.println("Cannot open the file on the path: " + corruptAddress);
        Main.logger.info("Cannot open the file on the path: " + corruptAddress);
        e1.printStackTrace();
        Main.logger.info(e1);
        return false;
    }
}

From source file:jp.andeb.obbutil.ObbUtilMain.java

private static boolean doAdd(String[] args) {
    final CommandLine commandLine;
    try {/*w w  w . java 2 s  .com*/
        final CommandLineParser parser = new GnuParser();
        commandLine = parser.parse(OPTIONS_FOR_ADD, args);
    } catch (MissingArgumentException e) {
        System.err.println("??????: " + e.getOption().getOpt());
        printUsage(PROGNAME);
        return false;
    } catch (MissingOptionException e) {
        System.err.println("??????: " + e.getMissingOptions());
        printUsage(PROGNAME);
        return false;
    } catch (UnrecognizedOptionException e) {
        System.err.println("????: " + e.getOption());
        printUsage(PROGNAME);
        return false;
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        printUsage(PROGNAME);
        return false;
    }

    final String pkgName = commandLine.getOptionValue(PACKAGE_NAME.getOpt());
    final String versionStr = commandLine.getOptionValue(OBB_VERSION.getOpt());
    final Integer version = toInteger(versionStr);
    if (version == null) {
        System.err.println("??????: " + versionStr);
        printUsage(PROGNAME);
        return false;
    }
    final boolean isOverlay = commandLine.hasOption(OVERLAY_FLAG.getOpt());
    final String saltStr = commandLine.getOptionValue(SALT.getOpt());
    final byte[] salt;
    if (saltStr == null) {
        salt = null;
    } else {
        salt = toByteArray(saltStr, ObbInfoV1.SALT_LENGTH);
        if (salt == null) {
            System.err.println("????: " + saltStr);
            printUsage(PROGNAME);
            return false;
        }
    }

    final String[] nonRecognizedArgs = commandLine.getArgs();
    if (nonRecognizedArgs.length == 0) {
        System.err.println("????????");
        printUsage(PROGNAME);
        return false;
    }
    if (nonRecognizedArgs.length != 1) {
        System.err.println("???????");
        printUsage(PROGNAME);
        return false;
    }

    final File targetFile = new File(nonRecognizedArgs[0]);
    final RandomAccessFile targetRaFile;
    try {
        targetRaFile = new RandomAccessFile(targetFile, "rw");
    } catch (FileNotFoundException e) {
        System.err.println("????: " + targetFile.getPath());
        return false;
    }
    try {
        try {
            final ObbInfoV1 info = ObbInfoV1.fromFile(targetRaFile);
            System.err.println(
                    "?? OBB ???????: " + info.toString());
            return false;
        } catch (IOException e) {
            System.err
                    .println("????????: " + targetFile.getPath());
            return false;
        } catch (NotObbException e) {
            // 
        }

        int flag = 0;
        if (isOverlay) {
            flag |= ObbInfoV1.FLAG_OVERLAY;
        }
        if (salt != null) {
            flag |= ObbInfoV1.FLAG_SALTED;
        }
        final ObbInfoV1 obbInfo = new ObbInfoV1(flag, salt, pkgName, version.intValue());
        final ByteBuffer obbInfoBytes = obbInfo.toBytes();
        // ???
        targetRaFile.setLength(targetRaFile.length() + obbInfoBytes.remaining());
        targetRaFile.seek(targetRaFile.length() - obbInfoBytes.remaining());
        targetRaFile.write(obbInfoBytes.array(), obbInfoBytes.arrayOffset(), obbInfoBytes.remaining());
    } catch (IOException e) {
        System.err.println("OBB ?????????: " + targetFile.getPath());
        return false;
    } finally {
        try {
            targetRaFile.close();
        } catch (IOException e) {
            System.err.println("OBB ?????????: " + targetFile.getPath());
            return false;
        }
    }
    System.err.println("OBB ??????????: " + targetFile.getPath());
    return true;
}

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

@Test
public void testCorruptCheckpointVersionMostSignificant4Bytes() throws Exception {
    Map<String, String> overrides = Maps.newHashMap();
    channel = createFileChannel(overrides);
    channel.start();//from  w  w  w  .j ava2 s .  com
    Assert.assertTrue(channel.isOpen());
    Set<String> in = putEvents(channel, "restart", 10, 100);
    Assert.assertEquals(100, in.size());
    forceCheckpoint(channel);
    channel.stop();
    File checkpoint = new File(checkpointDir, "checkpoint");
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    writer.seek(EventQueueBackingStoreFile.INDEX_VERSION * Serialization.SIZE_OF_LONG);
    writer.write(new byte[] { (byte) 1, (byte) 5 });
    writer.getFD().sync();
    writer.close();
    channel = createFileChannel(overrides);
    channel.start();
    Assert.assertTrue(channel.isOpen());
    Set<String> out = consumeChannel(channel);
    Assert.assertTrue(channel.didFullReplayDueToBadCheckpointException());
    compareInputAndOut(in, out);
}

From source file:pydio.sdk.java.http.AjxpFileBody.java

public void writeTo(OutputStream out) {

    InputStream in;/*from w  ww .  j a v a2  s .  c om*/
    //int bufsize = Integer.parseInt(StateHolder.getInstance().getLocalConfig(Pydio.LOCAL_CONFIG_BUFFER_SIZE));

    try {
        if (this.chunkSize > 0) {
            RandomAccessFile raf = new RandomAccessFile(getFile(), "r");
            int start = chunkIndex * this.chunkSize;

            int count = 0;
            int limit = chunkSize;
            byte[] buffer = new byte[bufsize];

            if (chunkIndex == (totalChunks - 1)) {
                limit = lastChunkSize;
            }

            raf.seek(start);

            while (count < limit) {

                if (count + bufsize > limit) {
                    if (count == 0) {
                        bufsize = limit;
                    } else {
                        bufsize = limit - count;
                    }
                }

                raf.read(buffer, 0, bufsize);
                out.write(buffer, 0, bufsize);
                count += bufsize;
            }
            raf.close();
        } else {
            in = new FileInputStream(getFile());
            byte[] buf = new byte[bufsize];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
        }
        this.chunkIndex++;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.flume.channel.file.TestFileChannelRestart.java

@Test
public void testCorruptCheckpointCompleteMarkerMostSignificant4Bytes() throws Exception {
    Map<String, String> overrides = Maps.newHashMap();
    channel = createFileChannel(overrides);
    channel.start();//from ww  w.j  a  v a 2s  .  c o m
    Assert.assertTrue(channel.isOpen());
    Set<String> in = putEvents(channel, "restart", 10, 100);
    Assert.assertEquals(100, in.size());
    forceCheckpoint(channel);
    channel.stop();
    File checkpoint = new File(checkpointDir, "checkpoint");
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    writer.seek(EventQueueBackingStoreFile.INDEX_CHECKPOINT_MARKER * Serialization.SIZE_OF_LONG);
    writer.write(new byte[] { (byte) 1, (byte) 5 });
    writer.getFD().sync();
    writer.close();
    channel = createFileChannel(overrides);
    channel.start();
    Assert.assertTrue(channel.isOpen());
    Set<String> out = consumeChannel(channel);
    Assert.assertTrue(channel.didFullReplayDueToBadCheckpointException());
    compareInputAndOut(in, out);
}