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.hdfs.server.datanode.TestTransferBlock.java

public void testTransferZeroChecksumFile() throws IOException {
    for (DataNode dn : cluster.getDataNodes()) {
        dn.useInlineChecksum = false;//  w  ww  .  ja  va2  s .c  om
    }

    // create a new file in the root, write data, do no close
    String filestr = "/testTransferZeroChecksumFile";
    DistributedFileSystem dfs = (DistributedFileSystem) fileSystem;

    DFSTestUtil.createFile(dfs, new Path(filestr), 9L, (short) 1, 0L);

    BlockPathInfo blockPathInfo = DFSTestUtil.getBlockPathInfo(filestr, cluster, dfs.getClient());

    // Delete the checksum file
    RandomAccessFile meta = new RandomAccessFile(blockPathInfo.getMetaPath(), "rw");
    meta.setLength(0);
    meta.close();

    RandomAccessFile block = new RandomAccessFile(blockPathInfo.getBlockPath(), "rw");
    block.setLength(0);
    block.close();

    int ns = cluster.getNameNode().getNamespaceID();
    DataNode dnWithBlk = null, dnWithoutBlk = null;
    for (DataNode dn : cluster.getDataNodes()) {
        FSDataset fds = (FSDataset) dn.data;
        DatanodeBlockInfo dbi = fds.getDatanodeBlockInfo(ns, blockPathInfo);
        if (dbi != null) {
            dbi.syncInMemorySize();
            dnWithBlk = dn;
        } else {
            dnWithoutBlk = dn;
        }
    }
    if (dnWithoutBlk == null || dnWithBlk == null) {
        TestCase.fail();
    }
    DatanodeInfo[] list = new DatanodeInfo[1];
    for (DatanodeInfo di : dfs.getClient().datanodeReport(DatanodeReportType.LIVE)) {
        if (dnWithoutBlk.getPort() == di.getPort()) {
            list[0] = di;
            break;
        }
    }
    blockPathInfo.setNumBytes(0);
    dnWithBlk.transferBlocks(ns, new Block[] { blockPathInfo }, new DatanodeInfo[][] { list });

    long size = -1;
    for (int i = 0; i < 3; i++) {
        try {
            size = ((FSDataset) dnWithoutBlk.data).getFinalizedBlockLength(ns, blockPathInfo);
            if (size == 0) {
                break;
            }
        } catch (IOException ioe) {
        }

        if (i != 2) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            TestCase.fail();
        }
    }
    TestCase.assertEquals(0, size);
}

From source file:com.sangupta.snowpack.SnowpackRecover.java

/**
 * Try and recover from a chunk./*from   w  w  w  .  j  ava2  s.  com*/
 * 
 * @param chunkID
 * @param chunkFile
 * @param metadataDB 
 * @return
 * @throws IOException 
 */
private static ChunkInfo recoverChunkInfo(final int chunkID, final File chunkFile,
        SnowpackMetadataDB metadataDB) throws IOException {
    // open the file for reading
    RandomAccessFile raf = new RandomAccessFile(chunkFile, "r");

    // read the length first
    int nameLength, length, terminator, headerLength, numFiles = 0;
    long offset;

    List<FlakeMetadata> metas = new ArrayList<FlakeMetadata>();

    try {
        while (raf.getFilePointer() < raf.length()) {
            offset = raf.getFilePointer();

            nameLength = raf.readInt();
            byte[] name = new byte[nameLength];
            raf.readFully(name);

            length = raf.readInt();
            raf.readLong();
            raf.skipBytes((int) length);

            terminator = raf.readByte();
            if (terminator != 0) {
                System.out.print(" invalid descriptor found...");
                return null;
            }

            headerLength = 4 + name.length + 4 + 8;

            numFiles++;

            metas.add(new FlakeMetadata(new String(name), nameLength, chunkID, offset, headerLength));
        }
    } finally {
        raf.close();
    }

    // all clear for recovery

    // save all metadata in new DB
    for (FlakeMetadata meta : metas) {
        metadataDB.save(meta);
    }

    // return chunk info
    ChunkInfo info = new ChunkInfo();
    info.chunkID = chunkID;
    info.numFiles = numFiles;
    info.writePointer = -1;

    return info;
}

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

@Test(expected = BadCheckpointException.class)
public void testTruncateMeta() throws Exception {
    EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    backingStore.close();//from w  w w  .ja  va2  s  .co m
    Assert.assertTrue(checkpoint.exists());
    File metaFile = Serialization.getMetaDataFile(checkpoint);
    Assert.assertTrue(metaFile.length() != 0);
    RandomAccessFile writer = new RandomAccessFile(metaFile, "rw");
    writer.setLength(0);
    writer.getFD().sync();
    writer.close();
    backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestTransferBlock.java

public void testTransferZeroChecksumFileInlineChecksum() throws IOException {
    for (DataNode dn : cluster.getDataNodes()) {
        dn.useInlineChecksum = true;/*from  w  ww.j a v a  2  s . com*/
    }

    // create a new file in the root, write data, do no close
    String filestr = "/testTransferZeroChecksumFile";
    DistributedFileSystem dfs = (DistributedFileSystem) fileSystem;

    DFSTestUtil.createFile(dfs, new Path(filestr), 9L, (short) 1, 0L);

    LocatedBlocks locations = cluster.getNameNode().getBlockLocations(filestr, 0, Long.MAX_VALUE);
    LocatedBlock locatedblock = locations.getLocatedBlocks().get(0);

    int ns = cluster.getNameNode().getNamespaceID();
    DataNode dnWithBlk = null, dnWithoutBlk = null;
    for (DataNode dn : cluster.getDataNodes()) {
        FSDataset fds = (FSDataset) dn.data;
        DatanodeBlockInfo dbi = fds.getDatanodeBlockInfo(ns, locatedblock.getBlock());

        if (dbi != null) {
            RandomAccessFile block = new RandomAccessFile(dbi.getBlockDataFile().file.toString(), "rw");
            block.setLength(0);
            block.close();

            dbi.syncInMemorySize();
            dnWithBlk = dn;
        } else {
            dnWithoutBlk = dn;
        }
    }
    if (dnWithoutBlk == null || dnWithBlk == null) {
        TestCase.fail();
    }
    DatanodeInfo[] list = new DatanodeInfo[1];
    for (DatanodeInfo di : dfs.getClient().datanodeReport(DatanodeReportType.LIVE)) {
        if (dnWithoutBlk.getPort() == di.getPort()) {
            list[0] = di;
            break;
        }
    }
    locatedblock.getBlock().setNumBytes(0);
    dnWithBlk.transferBlocks(ns, new Block[] { locatedblock.getBlock() }, new DatanodeInfo[][] { list });

    long size = -1;
    for (int i = 0; i < 3; i++) {
        try {
            size = ((FSDataset) dnWithoutBlk.data).getFinalizedBlockLength(ns, locatedblock.getBlock());
            if (size == 0) {
                break;
            }
        } catch (IOException ioe) {
        }

        if (i != 2) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            TestCase.fail();
        }
    }
    TestCase.assertEquals(0, size);
}

From source file:jm.web.Archivo.java

public String getArchivo(String path, int clave) {
    this._archivoNombre = "";
    try {/*from   www.  ja va  2  s. com*/
        ResultSet res = this.consulta("select * from tbl_archivo where id_archivo=" + clave + ";");
        if (res.next()) {
            this._archivoNombre = (res.getString("nombre") != null) ? res.getString("nombre") : "";
            try {
                this._archivo = new File(path, this._archivoNombre);
                if (!this._archivo.exists()) {
                    byte[] bytes = (res.getString("archivo") != null) ? res.getBytes("archivo") : null;
                    RandomAccessFile archivo = new RandomAccessFile(path + this._archivoNombre, "rw");
                    archivo.write(bytes);
                    archivo.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            res.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return this._archivoNombre;
}

From source file:com.phonegap.FileUtils.java

/**
 * Write contents of file.//from  www. ja v  a 2  s .c o m
 * 
 * @param filename         The name of the file.
 * @param data            The contents of the file.
 * @param offset         The position to begin writing the file.         
 * @throws FileNotFoundException, IOException
 */
public long write(String filename, String data, long offset) throws FileNotFoundException, IOException {
    RandomAccessFile file = new RandomAccessFile(filename, "rw");
    file.seek(offset);
    file.writeBytes(data);
    file.close();

    return data.length();
}

From source file:org.apache.bookkeeper.bookie.EntryLogTest.java

@Test(timeout = 60000)
public void testCorruptEntryLog() throws Exception {
    File tmpDir = createTempDir("bkTest", ".dir");
    File curDir = Bookie.getCurrentDirectory(tmpDir);
    Bookie.checkDirectoryStructure(curDir);

    int gcWaitTime = 1000;
    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setGcWaitTime(gcWaitTime);//from  ww w  . j av  a  2 s .c om
    conf.setLedgerDirNames(new String[] { tmpDir.toString() });
    Bookie bookie = new Bookie(conf);
    // create some entries
    EntryLogger logger = ((InterleavedLedgerStorage) bookie.ledgerStorage).entryLogger;
    logger.addEntry(1, generateEntry(1, 1));
    logger.addEntry(3, generateEntry(3, 1));
    logger.addEntry(2, generateEntry(2, 1));
    logger.flush();
    // now lets truncate the file to corrupt the last entry, which simulates a partial write
    File f = new File(curDir, "0.log");
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    raf.setLength(raf.length() - 10);
    raf.close();
    // now see which ledgers are in the log
    logger = new EntryLogger(conf, bookie.getLedgerDirsManager());

    EntryLogMetadata meta = logger.getEntryLogMetadata(0L);
    LOG.info("Extracted Meta From Entry Log {}", meta);
    assertNotNull(meta.getLedgersMap().get(1L));
    assertNull(meta.getLedgersMap().get(2L));
    assertNotNull(meta.getLedgersMap().get(3L));
}

From source file:org.red5.server.service.ShutdownServer.java

/**
 * Starts internal server listening for shutdown requests.
 *//*from w w w. j  a v a  2  s . com*/
public void start() {
    // dump to stdout
    System.out.printf("Token: %s%n", token);
    // write out the token to a file so that red5 may be shutdown external to this VM instance.
    try {
        // delete existing file
        Files.deleteIfExists(Paths.get(shutdownTokenFileName));
        // write to file
        Path path = Files.createFile(Paths.get(shutdownTokenFileName));
        File tokenFile = path.toFile();
        RandomAccessFile raf = new RandomAccessFile(tokenFile, "rws");
        raf.write(token.getBytes());
        raf.close();
    } catch (Exception e) {
        log.warn("Exception handling token file", e);
    }
    while (!shutdown.get()) {
        try (ServerSocket serverSocket = new ServerSocket(port);
                Socket clientSocket = serverSocket.accept();
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));) {
            log.info("Connected - local: {} remote: {}", clientSocket.getLocalSocketAddress(),
                    clientSocket.getRemoteSocketAddress());
            String inputLine = in.readLine();
            if (inputLine != null && token.equals(inputLine)) {
                log.info("Shutdown request validated using token");
                out.println("Ok");
                shutdownOrderly();
            } else {
                out.println("Bye");
            }
        } catch (BindException be) {
            log.error("Cannot bind to port: {}, ensure no other instances are bound or choose another port",
                    port, be);
            shutdownOrderly();
        } catch (IOException e) {
            log.warn("Exception caught when trying to listen on port {} or listening for a connection", port,
                    e);
        }
    }
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffReader.java

public static boolean isMMMultipageTiff(String directory) throws IOException {
    File dir = new File(directory);
    File[] children = dir.listFiles();
    File testFile = null;//  ww  w . ja  v  a  2s.c om
    for (File child : children) {
        if (child.isDirectory()) {
            File[] grandchildren = child.listFiles();
            for (File grandchild : grandchildren) {
                if (grandchild.getName().endsWith(".tif")) {
                    testFile = grandchild;
                    break;
                }
            }
        } else if (child.getName().endsWith(".tif") || child.getName().endsWith(".TIF")) {
            testFile = child;
            break;
        }
    }
    if (testFile == null) {
        throw new IOException("Unexpected file structure: is this an MM dataset?");
    }
    RandomAccessFile ra;
    try {
        ra = new RandomAccessFile(testFile, "r");
    } catch (FileNotFoundException ex) {
        ReportingUtils.logError(ex);
        return false;
    }
    FileChannel channel = ra.getChannel();
    ByteBuffer tiffHeader = ByteBuffer.allocate(36);
    ByteOrder bo;
    channel.read(tiffHeader, 0);
    char zeroOne = tiffHeader.getChar(0);
    if (zeroOne == 0x4949) {
        bo = ByteOrder.LITTLE_ENDIAN;
    } else if (zeroOne == 0x4d4d) {
        bo = ByteOrder.BIG_ENDIAN;
    } else {
        throw new IOException("Error reading Tiff header");
    }
    tiffHeader.order(bo);
    int summaryMDHeader = tiffHeader.getInt(32);
    channel.close();
    ra.close();
    if (summaryMDHeader == MultipageTiffWriter.SUMMARY_MD_HEADER) {
        return true;
    }
    return false;
}

From source file:jm.web.Archivo.java

public String getArchivo(String path, String tabla, String clave, String campoNombre, String campoBytea) {
    this._archivoNombre = "";
    try {/*w  w w . j a  va  2  s.c  om*/
        ResultSet res = this.consulta(
                "select * from " + tabla + " where " + tabla.replace("tbl_", "id_") + "=" + clave + ";");
        if (res.next()) {
            this._archivoNombre = res.getString(campoNombre) != null ? res.getString(campoNombre) : "";
            if (this._archivoNombre.compareTo("") != 0) {
                try {
                    this._archivo = new File(path, this._archivoNombre);
                    if (!this._archivo.exists()) {
                        byte[] bytes = (res.getString(campoBytea) != null) ? res.getBytes(campoBytea) : null;
                        RandomAccessFile archivo = new RandomAccessFile(path + this._archivoNombre, "rw");
                        archivo.write(bytes);
                        archivo.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            res.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return this._archivoNombre;
}