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.TestReplication.java

public void testPendingReplicationRetry() throws IOException {

    MiniDFSCluster cluster = null;/*www .j a va2s.c  o  m*/
    int numDataNodes = 4;
    String testFile = "/replication-test-file";
    Path testPath = new Path(testFile);

    byte buffer[] = new byte[1024];
    for (int i = 0; i < buffer.length; i++) {
        buffer[i] = '1';
    }

    try {
        Configuration conf = new Configuration();
        conf.set("dfs.replication", Integer.toString(numDataNodes));
        //first time format
        cluster = new MiniDFSCluster(0, conf, numDataNodes, true, true, null, null);
        cluster.waitActive();
        DFSClient dfsClient = new DFSClient(new InetSocketAddress("localhost", cluster.getNameNodePort()),
                conf);

        OutputStream out = cluster.getFileSystem().create(testPath);
        out.write(buffer);
        out.close();

        waitForBlockReplication(testFile, dfsClient.namenode, numDataNodes, -1);

        // get first block of the file.
        String block = dfsClient.namenode.getBlockLocations(testFile, 0, Long.MAX_VALUE).get(0).getBlock()
                .getBlockName();

        cluster.shutdown();
        cluster = null;

        //Now mess up some of the replicas.
        //Delete the first and corrupt the next two.
        File baseDir = new File(System.getProperty("test.build.data"), "dfs/data");
        for (int i = 0; i < 25; i++) {
            buffer[i] = '0';
        }

        int fileCount = 0;
        for (int i = 0; i < 6; i++) {
            File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + block);
            LOG.info("Checking for file " + blockFile);

            if (blockFile.exists()) {
                if (fileCount == 0) {
                    LOG.info("Deleting file " + blockFile);
                    assertTrue(blockFile.delete());
                } else {
                    // corrupt it.
                    LOG.info("Corrupting file " + blockFile);
                    long len = blockFile.length();
                    assertTrue(len > 50);
                    RandomAccessFile blockOut = new RandomAccessFile(blockFile, "rw");
                    try {
                        blockOut.seek(len / 3);
                        blockOut.write(buffer, 0, 25);
                    } finally {
                        blockOut.close();
                    }
                }
                fileCount++;
            }
        }
        assertEquals(3, fileCount);

        /* Start the MiniDFSCluster with more datanodes since once a writeBlock
         * to a datanode node fails, same block can not be written to it
         * immediately. In our case some replication attempts will fail.
         */

        LOG.info("Restarting minicluster after deleting a replica and corrupting 2 crcs");
        conf = new Configuration();
        conf.set("dfs.replication", Integer.toString(numDataNodes));
        conf.set("dfs.replication.pending.timeout.sec", Integer.toString(2));
        conf.set("dfs.datanode.block.write.timeout.sec", Integer.toString(5));
        conf.set("dfs.safemode.threshold.pct", "0.75f"); // only 3 copies exist

        cluster = new MiniDFSCluster(0, conf, numDataNodes * 2, false, true, null, null);
        cluster.waitActive();

        dfsClient = new DFSClient(new InetSocketAddress("localhost", cluster.getNameNodePort()), conf);

        waitForBlockReplication(testFile, dfsClient.namenode, numDataNodes, -1);

    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}

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

@Test(expected = BadCheckpointException.class)
public void testCheckpointOrderIdNotEqualToMeta() throws Exception {
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    try {//w w w  .  j a v  a  2s .  c o m
        EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
        backingStore.close();
        writer.seek(EventQueueBackingStoreFile.INDEX_WRITE_ORDER_ID * Serialization.SIZE_OF_LONG);
        writer.writeLong(2L);
        writer.getFD().sync();
        backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    } finally {
        writer.close();
    }
}

From source file:ValidateLicenseHeaders.java

/**
 * Read the first comment upto the package ...; statement
 * //from   ww  w  .j a  v  a2s .  co m
 * @param javaFile
 */
static void parseHeader(File javaFile) throws IOException {
    totalCount++;
    RandomAccessFile raf = new RandomAccessFile(javaFile, "rw");
    String line = raf.readLine();
    StringBuffer tmp = new StringBuffer();
    long endOfHeader = 0;
    boolean packageOrImport = false;
    while (line != null) {
        long nextEOH = raf.getFilePointer();
        line = line.trim();
        // Ignore any single line comments
        if (line.startsWith("//")) {
            line = raf.readLine();
            continue;
        }

        // If this is a package/import/class/interface statement break
        if (line.startsWith("package") || line.startsWith("import") || line.indexOf("class") >= 0
                || line.indexOf("interface") >= 0) {
            packageOrImport = true;
            break;
        }

        // Update the current end of header marker
        endOfHeader = nextEOH;

        if (line.startsWith("/**"))
            tmp.append(line.substring(3));
        else if (line.startsWith("/*"))
            tmp.append(line.substring(2));
        else if (line.startsWith("*"))
            tmp.append(line.substring(1));
        else
            tmp.append(line);
        tmp.append(' ');
        line = raf.readLine();
    }
    raf.close();

    if (tmp.length() == 0 || packageOrImport == false) {
        addDefaultHeader(javaFile);
        return;
    }

    String text = tmp.toString();
    // Replace all duplicate whitespace with a single space
    text = text.replaceAll("[\\s*]+", " ");
    text = text.toLowerCase().trim();
    // Replace any copyright date0-date1,date2 with copyright ...
    text = text.replaceAll(COPYRIGHT_REGEX, "...");
    if (tmp.length() == 0) {
        addDefaultHeader(javaFile);
        return;
    }
    // Search for a matching header
    boolean matches = false;
    String matchID = null;
    Iterator iter = licenseHeaders.entrySet().iterator();
    escape: while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        String key = (String) entry.getKey();
        List list = (List) entry.getValue();
        Iterator jiter = list.iterator();
        while (jiter.hasNext()) {
            LicenseHeader lh = (LicenseHeader) jiter.next();
            if (text.startsWith(lh.text)) {
                matches = true;
                matchID = lh.id;
                lh.count++;
                lh.usage.add(javaFile);
                if (log.isLoggable(Level.FINE))
                    log.fine(javaFile + " matches copyright key=" + key + ", id=" + lh.id);
                break escape;
            }
        }
    }
    text = null;
    tmp.setLength(0);
    if (matches == false)
        invalidheaders.add(javaFile);
    else if (matchID.startsWith("jboss") && matchID.endsWith("#0") == false) {
        // This is a legacy jboss head that needs to be updated to the default
        replaceHeader(javaFile, endOfHeader);
        jbossCount++;
    }
}

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

/**
 * Source:// ww w. j  a va 2  s .  co  m
 * http://stackoverflow.com/questions/4349075/bitmapfactory-decoderesource
 * -returns-a-mutable-bitmap-in-android-2-2-and-an-immu
 * 
 * Converts a immutable bitmap to a mutable bitmap. This operation doesn't
 * allocates more memory that there is already allocated.
 * 
 * @param imgIn
 *            - Source image. It will be released, and should not be used
 *            more
 * @return a copy of imgIn, but immutable.
 */
public static Bitmap convertBitmapToMutable(Bitmap imgIn) {
    try {
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        File file = new File(MyApp.context.getFilesDir() + File.separator + "temp.tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = imgIn.getWidth();
        int height = imgIn.getHeight();
        Config type = imgIn.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
        imgIn.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        imgIn.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        imgIn = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        imgIn.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temporary file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return imgIn;
}

From source file:org.commoncrawl.service.listcrawler.CacheManager.java

/**
 * readLogFileHeader - from File//from w  w w  .  j av  a 2 s  .  c o m
 * 
 * @param logFileName
 * @return
 * @throws IOException
 */
private static LocalLogFileHeader readLogFileHeader(File logFileName) throws IOException {

    LocalLogFileHeader headerOut = new LocalLogFileHeader();
    RandomAccessFile file = new RandomAccessFile(logFileName, "r");
    try {
        headerOut = readLogFileHeader(file);
    } finally {
        file.close();
    }
    return headerOut;
}

From source file:org.ambiance.codec.YEncDecoder.java

/**
 * Decode a single file/*w  w  w. j  a  v  a 2 s .  c  o  m*/
 */
public void decode(File input) throws DecoderException {
    try {
        YEncFile yencFile = new YEncFile(input);

        // Get the output file
        StringBuffer outputName = new StringBuffer(outputDirName);
        outputName.append(File.separator);
        outputName.append(yencFile.getHeader().getName());
        RandomAccessFile output = new RandomAccessFile(outputName.toString(), "rw");

        // Place the pointer to the begining of data to decode
        long pos = yencFile.getDataBegin();
        yencFile.getInput().seek(pos);

        // Bufferise the file
        // TODO - A Amliorer
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (pos < yencFile.getDataEnd()) {
            baos.write(yencFile.getInput().read());
            pos++;
        }

        byte[] buff = decoder.decode(baos.toByteArray());

        // Write and close output
        output.write(buff);
        output.close();

        // Warn if CRC32 check is not OK
        CRC32 crc32 = new CRC32();
        crc32.update(buff);
        if (!yencFile.getTrailer().getCrc32().equals(Long.toHexString(crc32.getValue()).toUpperCase()))
            throw new DecoderException("Error in CRC32 check.");

    } catch (YEncException ye) {
        throw new DecoderException("Input file is not a YEnc file or contains error : " + ye.getMessage());
    } catch (FileNotFoundException fnfe) {
        throw new DecoderException("Enable to create output file : " + fnfe.getMessage());
    } catch (IOException ioe) {
        throw new DecoderException("Enable to read input file : " + ioe.getMessage());
    }
}

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

@Test(expected = BadCheckpointException.class)
public void testIncompleteCheckpoint() throws Exception {
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");

    try {/*from ww  w. ja va2  s  .c  o m*/
        EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
        backingStore.close();
        writer.seek(EventQueueBackingStoreFile.INDEX_CHECKPOINT_MARKER * Serialization.SIZE_OF_LONG);
        writer.writeLong(EventQueueBackingStoreFile.CHECKPOINT_INCOMPLETE);
        writer.getFD().sync();
        backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    } finally {
        writer.close();
    }
}

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

public boolean isConversionNeeded(StorageDirectory sd) throws IOException {
    File oldF = new File(sd.getRoot(), "storage");
    if (!oldF.exists())
        return false;
    // check the layout version inside the storage file
    // Lock and Read old storage file
    RandomAccessFile oldFile = new RandomAccessFile(oldF, "rws");
    FileLock oldLock = oldFile.getChannel().tryLock();
    try {//from   w  w w  .  j  ava2s  .c  o m
        oldFile.seek(0);
        int oldVersion = oldFile.readInt();
        if (oldVersion < LAST_PRE_UPGRADE_LAYOUT_VERSION)
            return false;
    } finally {
        oldLock.release();
        oldFile.close();
    }
    return true;
}

From source file:com.ieasy.basic.util.file.FileUtils.java

/**
 * ??//from w  w w  . j a  v  a 2  s .  c o m
 * 
 * @param fileName
 *            ??
 */
public static void readFileByRandomAccess(String fileName) {
    RandomAccessFile randomFile = null;
    try {
        System.out.println("??");
        // ????
        randomFile = new RandomAccessFile(fileName, "r");
        // 
        long fileLength = randomFile.length();
        // ?
        int beginIndex = (fileLength > 4) ? 4 : 0;
        // ?beginIndex?
        randomFile.seek(beginIndex);
        byte[] bytes = new byte[10];
        int byteread = 0;
        // 10?10
        // ?byteread
        while ((byteread = randomFile.read(bytes)) != -1) {
            System.out.write(bytes, 0, byteread);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (randomFile != null) {
            try {
                randomFile.close();
            } catch (IOException e1) {
            }
        }
    }
}

From source file:com.example.android.vault.EncryptedDocument.java

/**
 * Decrypt and read content section of this document, writing it into the
 * given pipe.//from w  w w  .  j  a  va2  s.c  o m
 * <p/>
 * Pipe is left open, so caller is responsible for calling
 * {@link ParcelFileDescriptor#close()} or
 * {@link ParcelFileDescriptor#closeWithError(String)}.
 *
 * @param contentOut write end of a pipe.
 * @throws DigestException if content fails MAC check. Some or all content
 *                         may have already been written to the pipe when the MAC is
 *                         validated.
 */
public void readContent(ParcelFileDescriptor contentOut) throws IOException, GeneralSecurityException {
    final RandomAccessFile f = new RandomAccessFile(mFile, "r");
    try {
        assertMagic(f);

        if (f.length() <= CONTENT_OFFSET) {
            throw new IOException("Document has no content");
        }

        // Skip over metadata section
        f.seek(CONTENT_OFFSET);
        readSection(f, new FileOutputStream(contentOut.getFileDescriptor()));

    } finally {
        f.close();
    }
}