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.namenode.TestCheckpoint.java

/**
 * Test that a fault while downloading edits does not prevent future
 * checkpointing//from   w ww .  ja va  2 s  . co  m
 */
@Test(timeout = 30000)
public void testEditFailureBeforeRename() throws IOException {
    Configuration conf = new HdfsConfiguration();
    SecondaryNameNode secondary = null;
    MiniDFSCluster cluster = null;
    FileSystem fs = null;
    try {
        cluster = new MiniDFSCluster.Builder(conf).numDataNodes(numDatanodes).build();
        cluster.waitActive();
        fs = cluster.getFileSystem();
        secondary = startSecondaryNameNode(conf);
        DFSTestUtil.createFile(fs, new Path("tmpfile0"), 1024, (short) 1, 0l);
        secondary.doCheckpoint();

        // Cause edit rename to fail during next checkpoint
        Mockito.doThrow(new IOException("Injecting failure before edit rename")).when(faultInjector)
                .beforeEditsRename();
        DFSTestUtil.createFile(fs, new Path("tmpfile1"), 1024, (short) 1, 0l);

        try {
            secondary.doCheckpoint();
            fail("Fault injection failed.");
        } catch (IOException ioe) {
            GenericTestUtils.assertExceptionContains("Injecting failure before edit rename", ioe);
        }
        Mockito.reset(faultInjector);
        // truncate the tmp edits file to simulate a partial download
        for (StorageDirectory sd : secondary.getFSImage().getStorage().dirIterable(NameNodeDirType.EDITS)) {
            File[] tmpEdits = sd.getCurrentDir().listFiles(tmpEditsFilter);
            assertTrue("Expected a single tmp edits file in directory " + sd.toString(), tmpEdits.length == 1);
            RandomAccessFile randFile = new RandomAccessFile(tmpEdits[0], "rw");
            randFile.setLength(0);
            randFile.close();
        }
        // Next checkpoint should succeed
        secondary.doCheckpoint();
    } finally {
        if (secondary != null) {
            secondary.shutdown();
        }
        if (fs != null) {
            fs.close();
        }
        cleanup(secondary);
        secondary = null;
        cleanup(cluster);
        cluster = null;
        Mockito.reset(faultInjector);
    }
}

From source file:org.apache.james.mailrepository.file.MBoxMailRepository.java

/**
 * Quickly find a message by using the stored message offsets
 * /*from  w w  w. ja  v  a  2 s.  co  m*/
 * @param key
 *            The key of the message to find
 */
private MimeMessage selectMessage(final String key) {
    MimeMessage foundMessage = null;
    // Can we find the key first
    if (mList == null || !mList.containsKey(key)) {
        // Not initiailised so no point looking
        if ((getLogger().isDebugEnabled())) {
            String logBuffer = this.getClass().getName() + " mList - key not found " + mboxFile;

            getLogger().debug(logBuffer);
        }
        return foundMessage;
    }
    long messageStart = mList.get(key);
    if ((getLogger().isDebugEnabled())) {
        String logBuffer = this.getClass().getName() + " Load message starting at offset " + messageStart
                + " from file " + mboxFile;

        getLogger().debug(logBuffer);
    }
    // Now try and find the position in the file
    RandomAccessFile ins = null;
    try {
        ins = new RandomAccessFile(mboxFile, "r");
        if (messageStart != 0) {
            ins.seek(messageStart - 1);
        }
        MessageAction op = new MessageAction() {
            public boolean isComplete() {
                return true;
            }

            public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {
                try {
                    if (key.equals(generateKeyValue(bodyText))) {
                        getLogger()
                                .debug(this.getClass().getName() + " Located message. Returning MIME message");
                        return convertTextToMimeMessage(bodyText);
                    }
                } catch (NoSuchAlgorithmException e) {
                    getLogger().error("MD5 not supported! ", e);
                }
                return null;
            }
        };
        foundMessage = this.parseMboxFile(ins, op);
    } catch (FileNotFoundException e) {
        getLogger().error("Unable to save(open) file (File not found) " + mboxFile, e);
    } catch (IOException e) {
        getLogger().error("Unable to write file (General I/O problem) " + mboxFile, e);
    } finally {
        if (foundMessage == null) {
            if ((getLogger().isDebugEnabled())) {
                String logBuffer = this.getClass().getName() + " select - message not found " + mboxFile;

                getLogger().debug(logBuffer);
            }
        }
        if (ins != null)
            try {
                ins.close();
            } catch (IOException e) {
                getLogger().error("Unable to close file (General I/O problem) " + mboxFile, e);
            }
    }
    return foundMessage;
}

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

/**
 * deserialize metadata from disk /*from w  w w  .  j  av a 2s.  co  m*/
 * 
 * @throws IOException
 */
void loadMetadataFromDisk() throws IOException {

    // skip metadata load if sub-domain metadata file is missing... 
    // in this case, metadata will be rebuilt during subdomain metadata rescan ... 
    if (_subDomainMetadataFile.exists()) {

        RandomAccessFile file = new RandomAccessFile(_listMetadataFile, "rw");
        try {
            _metadata.deserialize(file, new BinaryProtocol());
            int urlCount = _metadata.getUrlCount();
            _metadata.clear();
            _metadata.setUrlCount(urlCount);
        } finally {
            file.close();
        }
    }
}

From source file:com.xperia64.rompatcher.MainActivity.java

public void patch(final boolean c, final boolean d, final boolean r, final String ed) {
    final ProgressDialog myPd_ring = ProgressDialog.show(MainActivity.this,
            getResources().getString(R.string.wait), getResources().getString(R.string.wait_desc), true);
    myPd_ring.setCancelable(false);/*  w  w  w  .j  a v a2  s  .  c o m*/
    new Thread(new Runnable() {
        public void run() {
            if (new File(Globals.patchToApply).exists() && new File(Globals.fileToPatch).exists()
                    && !Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".ecm")) {
                String msg = getResources().getString(R.string.success);
                if (!new File(Globals.fileToPatch).canWrite()) {
                    Globals.msg = msg = "Can not write to output file. If you are on KitKat or Lollipop, move the file to your internal storage.";
                    return;
                }
                if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ups")) {
                    int e = upsPatchRom(Globals.fileToPatch, Globals.patchToApply, Globals.fileToPatch + ".new",
                            r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_UPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta3")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".vcdiff")) {
                    RandomAccessFile f = null;
                    try {
                        f = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    StringBuilder s = new StringBuilder();
                    try {
                        if (f.length() >= 9) {
                            for (int i = 0; i < 8; i++) {
                                s.append((char) f.readByte());
                                f.seek(i + 1);
                            }
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        f.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    // Header of xdelta patch determines version
                    if (s.toString().equals("%XDELTA%") || s.toString().equals("%XDZ000%")
                            || s.toString().equals("%XDZ001%") || s.toString().equals("%XDZ002%")
                            || s.toString().equals("%XDZ003%") || s.toString().equals("%XDZ004%")) {
                        int e = xdelta1PatchRom(Globals.fileToPatch, Globals.patchToApply,
                                Globals.fileToPatch + ".new");
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_XDELTA1);
                        }
                    } else {
                        int e = xdelta3PatchRom(Globals.fileToPatch, Globals.patchToApply,
                                Globals.fileToPatch + ".new");
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_XDELTA3);
                        }
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bps")) {
                    int e = bpsPatchRom(Globals.fileToPatch, Globals.patchToApply, Globals.fileToPatch + ".new",
                            r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_BPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dps")) {
                    int e = dpsPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_DPS);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bsdiff")) {
                    int e = bsdiffPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_BSDIFF);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".aps")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    // Wow.
                    byte[] gbaSig = { 0x41, 0x50, 0x53, 0x31, 0x00 };
                    byte[] n64Sig = { 0x41, 0x50, 0x53, 0x31, 0x30 };
                    byte[] realSig = new byte[5];
                    RandomAccessFile raf = null;
                    System.out.println("APS Patch");
                    try {
                        raf = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    try {
                        raf.read(realSig);
                        raf.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                    if (Arrays.equals(realSig, gbaSig)) {
                        System.out.println("GBA APS");
                        APSGBAPatcher aa = new APSGBAPatcher();
                        aa.crcTableInit();
                        int e = 0;
                        try {
                            e = aa.ApplyPatch(Globals.patchToApply, Globals.fileToPatch, r);
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                            e = -5;
                        }
                        System.out.println("e: " + e);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_APSGBA);
                        }
                    } else if (Arrays.equals(realSig, n64Sig)) {
                        System.out.println("N64 APS");
                        int e = apsN64PatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_APSN64);
                        }
                    } else {
                        msg = parseError(-131, -10000);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ppf")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e = ppfPatchRom(Globals.fileToPatch, Globals.patchToApply, r ? 1 : 0);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_PPF);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".patch")) {
                    int e = xdelta1PatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_XDELTA1);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".asm")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e;
                    if (Globals.asar)
                        e = asarPatchRom(Globals.fileToPatch, Globals.patchToApply, r ? 1 : 0);
                    else
                        e = asmPatchRom(Globals.fileToPatch, Globals.patchToApply);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_ASM);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dldi")) {
                    File f = new File(Globals.fileToPatch);
                    File f2 = new File(Globals.fileToPatch + ".bak");
                    try {
                        Files.copy(f, f2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int e = dldiPatchRom(Globals.fileToPatch, Globals.patchToApply);
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_DLDI);
                    }
                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xpc")) {
                    int e = xpcPatchRom(Globals.fileToPatch, Globals.patchToApply,
                            Globals.fileToPatch + ".new");
                    if (e != 0) {
                        msg = parseError(e, Globals.TYPE_XPC);
                    }

                } else if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".nmp")) {

                    String drm = MainActivity.this.getPackageName();
                    System.out.println("Drm is: " + drm);
                    if (drm.equals("com.xperia64.rompatcher.donation")) {
                        if (c) {
                            File f = new File(Globals.fileToPatch);
                            File f2 = new File(Globals.fileToPatch + ".bak");
                            try {
                                Files.copy(f, f2);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        NitroROMFilesystem fs;
                        try {
                            fs = new NitroROMFilesystem(Globals.fileToPatch);
                            ROM.load(fs);
                            RealPatch.patch(Globals.patchToApply, new Object());
                            ROM.close();
                        } catch (Exception e1) {
                            // TODO Auto-generated catch block
                            //e1.printStackTrace();
                            Globals.msg = msg = String.format(getResources().getString(R.string.nmpDefault),
                                    e1.getStackTrace()[0].getFileName(), e1.getStackTrace()[0].getLineNumber());
                        }
                        if (c && d && !TextUtils.isEmpty(ed)) {
                            File f = new File(Globals.fileToPatch);
                            File f3 = new File(Globals.fileToPatch + ".bak");
                            File f2 = new File(
                                    Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/') + 1)
                                            + ed);
                            f.renameTo(f2);
                            f3.renameTo(f);
                        }
                    } else {
                        Globals.msg = msg = getResources().getString(R.string.drmwarning);
                        MainActivity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
                                b.setTitle(getResources().getString(R.string.drmwarning));
                                b.setIcon(
                                        ResourcesCompat.getDrawable(getResources(), R.drawable.icon_pro, null));
                                b.setMessage(getResources().getString(R.string.drmwarning_desc));
                                b.setCancelable(false);
                                b.setNegativeButton(getResources().getString(android.R.string.cancel),
                                        new OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface arg0, int arg1) {
                                            }
                                        });
                                b.setPositiveButton(getResources().getString(R.string.nagInfo),
                                        new OnClickListener() {

                                            @Override
                                            public void onClick(DialogInterface arg0, int arg1) {
                                                try {
                                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
                                                            "market://details?id=com.xperia64.rompatcher.donation")));
                                                } catch (android.content.ActivityNotFoundException anfe) {
                                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
                                                            "http://play.google.com/store/apps/details?id=com.xperia64.rompatcher.donation")));
                                                }
                                            }
                                        });
                                b.create().show();
                            }
                        });
                    }

                } else {
                    RandomAccessFile f = null;
                    try {
                        f = new RandomAccessFile(Globals.patchToApply, "r");
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                        Globals.msg = msg = getResources().getString(R.string.fnf);
                        return;
                    }
                    StringBuilder s = new StringBuilder();
                    try {
                        if (f.length() >= 6) {
                            for (int i = 0; i < 5; i++) {
                                s.append((char) f.readByte());
                                f.seek(i + 1);
                            }
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        f.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    int e;
                    // Two variants of IPS, the normal PATCH type, then this weird one called IPS32 with messily hacked in 32 bit offsets
                    if (s.toString().equals("IPS32")) {
                        e = ips32PatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_IPS);
                        }
                    } else {
                        e = ipsPatchRom(Globals.fileToPatch, Globals.patchToApply);
                        if (e != 0) {
                            msg = parseError(e, Globals.TYPE_IPS);
                        }
                    }

                }
                if (Globals.patchToApply.toLowerCase(Locale.US).endsWith(".ups")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xdelta3")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".vcdiff")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".patch")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bps")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".bsdiff")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".dps")
                        || Globals.patchToApply.toLowerCase(Locale.US).endsWith(".xpc")) {
                    File oldrom = new File(Globals.fileToPatch);
                    File bkrom = new File(Globals.fileToPatch + ".bak");
                    oldrom.renameTo(bkrom);
                    File newrom = new File(Globals.fileToPatch + ".new");
                    newrom.renameTo(oldrom);
                }
                if (!c) {
                    File f = new File(Globals.fileToPatch + ".bak");
                    if (f.exists()) {
                        f.delete();
                    }
                } else {
                    if (d) {
                        File one = new File(Globals.fileToPatch + ".bak");
                        File two = new File(Globals.fileToPatch);
                        File three = new File(
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/') + 1)
                                        + ed);
                        two.renameTo(three);
                        File four = new File(Globals.fileToPatch);
                        one.renameTo(four);
                    }
                }
                Globals.msg = msg;
            } else if (Globals.fileToPatch.toLowerCase(Locale.US).endsWith(".ecm")) {
                int e = 0;
                String msg = getResources().getString(R.string.success);
                if (c) {

                    if (d) {
                        e = ecmPatchRom(Globals.fileToPatch,
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('/')) + ed, 1);
                    } else {
                        //new File(Globals.fileToPatch).renameTo(new File(Globals.fileToPatch+".bak"));
                        e = ecmPatchRom(Globals.fileToPatch,
                                Globals.fileToPatch.substring(0, Globals.fileToPatch.lastIndexOf('.')), 1);
                    }
                } else {
                    e = ecmPatchRom(Globals.fileToPatch, "", 0);
                }
                if (e != 0) {
                    msg = parseError(e, Globals.TYPE_ECM);
                }
                Globals.msg = msg;
            } else {
                Globals.msg = getResources().getString(R.string.fnf);
            }

        }
    }).start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                while (Globals.msg.equals("")) {
                    Thread.sleep(25);
                }
                ;
                myPd_ring.dismiss();
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast t = Toast.makeText(staticThis, Globals.msg, Toast.LENGTH_SHORT);
                        t.show();
                        Globals.msg = "";
                    }
                });
                if (Globals.msg.equals(getResources().getString(R.string.success))) // Don't annoy people who did something wrong even further
                {
                    final SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(MainActivity.this);
                    int x = prefs.getInt("purchaseNag", 5);
                    if (x != -1) {
                        if ((isPackageInstalled("com.xperia64.timidityae", MainActivity.this)
                                || isPackageInstalled("com.xperia64.rompatcher.donation", MainActivity.this))) {
                            prefs.edit().putInt("purchaseNag", -1);
                        } else {
                            if (x >= 5) {

                                prefs.edit().putInt("purchaseNag", 0).commit();
                                /*runOnUiThread(new Runnable() {
                                    public void run() {
                                AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
                                b.setTitle("Like ROM Patcher?");
                                b.setIcon(getResources().getDrawable(R.drawable.icon_pro));
                                b.setMessage(getResources().getString(R.string.nagMsg));
                                b.setCancelable(false);
                                b.setNegativeButton(getResources().getString(android.R.string.cancel), new OnClickListener(){@Override public void onClick(DialogInterface arg0, int arg1) {}});
                                b.setPositiveButton(getResources().getString(R.string.nagInfo), new OnClickListener(){
                                        
                                  @Override
                                  public void onClick(DialogInterface arg0, int arg1) {
                                     try  {
                                         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.xperia64.rompatcher.donation")));
                                     } catch (android.content.ActivityNotFoundException anfe) {
                                         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.xperia64.rompatcher.donation")));
                                     }
                                  }
                                   });
                                      b.create().show();
                                       }
                                   }); // end of UIThread*/
                            } else {
                                prefs.edit().putInt("purchaseNag", x + 1).commit();
                            }
                        }
                    }
                }

            } catch (Exception e) {
            }
        }
    }).start();
}

From source file:com.siblinks.ws.service.impl.UploadEssayServiceImpl.java

/**
 * {@inheritDoc}/*from   w w  w  .  j  av  a  2s .com*/
 */
@Override
@RequestMapping(value = "/getFileReivewUploadEssay/{eid}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> getFileReivewUploadEssay(@PathVariable(value = "eid") final String eid)
        throws IOException {

    RandomAccessFile randomAccessFile = null;
    ResponseEntity<byte[]> responseEntity = null;
    try {
        // Get file Essay
        Object[] queryParams = { eid };
        List<Object> readObject = dao.readObjects(SibConstants.SqlMapper.SQL_GET_IMAGE_UPLOAD_ESSAY,
                queryParams);
        if (((Map) readObject.get(0)).get(Parameters.URLREVIEW) != null) {

            // Read file Essay
            String path = ((Map) readObject.get(0)).get(Parameters.URLREVIEW).toString();
            randomAccessFile = new RandomAccessFile(path, "r");
            byte[] r = new byte[(int) randomAccessFile.length()];
            randomAccessFile.readFully(r);
            responseEntity = new ResponseEntity<byte[]>(r, new HttpHeaders(), HttpStatus.OK);
        } else {
            responseEntity = new ResponseEntity<byte[]>(HttpStatus.NO_CONTENT);
        }
    } catch (Exception e) {
        // File essay not found
        logger.debug(e.getMessage(), e.getCause());
        responseEntity = new ResponseEntity<byte[]>(HttpStatus.NO_CONTENT);
    } finally {
        try {
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
        } catch (IOException io) {
            // Do nothing
        }
    }
    return responseEntity;
}

From source file:org.apache.james.mailrepository.file.MBoxMailRepository.java

/**
 * Load the message keys and file pointer offsets from disk
 *///w w w.j a v  a  2s.  c om
private synchronized void loadKeys() {
    if (mList != null) {
        return;
    }
    RandomAccessFile ins = null;
    try {
        ins = new RandomAccessFile(mboxFile, "r");
        long initialCapacity = (ins.length() > MLISTPRESIZEFACTOR ? ins.length() / MLISTPRESIZEFACTOR : 0);
        if (initialCapacity < DEFAULTMLISTCAPACITY) {
            initialCapacity = DEFAULTMLISTCAPACITY;
        }
        if (initialCapacity > Integer.MAX_VALUE) {
            initialCapacity = Integer.MAX_VALUE - 1;
        }
        this.mList = new Hashtable<String, Long>((int) initialCapacity);
        this.parseMboxFile(ins, new MessageAction() {
            public boolean isComplete() {
                return false;
            }

            public MimeMessage messageAction(String messageSeparator, String bodyText, long messageStart) {
                try {
                    String key = generateKeyValue(bodyText);
                    mList.put(key, messageStart);
                    if ((getLogger().isDebugEnabled())) {
                        getLogger().debug(this.getClass().getName() + " Key " + key + " at " + messageStart);
                    }

                } catch (NoSuchAlgorithmException e) {
                    getLogger().error("MD5 not supported! ", e);
                }
                return null;
            }
        });
        // System.out.println("Done Load keys!");
    } catch (FileNotFoundException e) {
        getLogger().error("Unable to save(open) file (File not found) " + mboxFile, e);
        this.mList = new Hashtable<String, Long>((int) DEFAULTMLISTCAPACITY);
    } catch (IOException e) {
        getLogger().error("Unable to write file (General I/O problem) " + mboxFile, e);
    } finally {
        if (ins != null)
            try {
                ins.close();
            } catch (IOException e) {
                getLogger().error("Unable to close file (General I/O problem) " + mboxFile, e);
            }
    }
}

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

/**
 * serialize metadata to disk //from w  w w  .j a va2  s.  c o  m
 * @throws IOException
 */
void writeSubDomainMetadataToDisk(CrawlListMetadata subDomainData) throws IOException {

    DataOutputBuffer outputBuffer = new DataOutputBuffer(CrawlListMetadata.Constants.FixedDataSize);

    subDomainData.serialize(outputBuffer, new BinaryProtocol());

    if (outputBuffer.getLength() > CrawlListMetadata.Constants.FixedDataSize) {
        LOG.error("ListMetadata Serialize for List:" + subDomainData.getDomainName() + " > FixedDataSize!!!");
        outputBuffer.reset();
        subDomainData.setDomainName("<<CORRUPT>>");
        subDomainData.serialize(outputBuffer, new BinaryProtocol());
    }

    synchronized (_subDomainMetadataFile) {
        RandomAccessFile file = new RandomAccessFile(_subDomainMetadataFile, "rw");
        try {
            if (subDomainData.getSubDomainDataOffset() == 0) {
                throw new IOException("Data Offset Zero during write!");
            }
            file.seek(subDomainData.getSubDomainDataOffset());
            file.write(outputBuffer.getData(), 0, outputBuffer.getLength());
        } finally {
            file.close();
        }
    }
}

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

private void dumpFixedDataFile() {
    try {//  w w w .  j  a va 2  s . c o  m
        RandomAccessFile fixedDataReader = new RandomAccessFile(_fixedDataFile, "rw");

        try {
            OnDiskCrawlHistoryItem item = new OnDiskCrawlHistoryItem();
            int index = 0;
            while (fixedDataReader.getFilePointer() != fixedDataReader.length()) {
                item.deserialize(fixedDataReader);
                LOG.info("Item at Index:" + index++ + " Domain:" + item._domainHash + " URLFP:"
                        + item._urlFingerprint);
            }
        } finally {
            fixedDataReader.close();
        }
    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
    }

}

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

public CrawlListMetadata getSubDomainMetadataByRootDomain(String rootDomainName) throws IOException {
    long domainHash = URLFingerprint.generate64BitURLFPrint(rootDomainName);

    CrawlListMetadata metadata = new CrawlListMetadata();
    synchronized (_subDomainMetadataFile) {
        RandomAccessFile file = new RandomAccessFile(_subDomainMetadataFile, "rw");
        try {// ww  w  .j  a va  2 s .  c om
            int dataOffset = getOffsetForSubDomainData(domainHash);
            if (dataOffset == 0) {
                throw new IOException("Data Offset Zero for host:" + rootDomainName);
            }
            file.seek(dataOffset);
            metadata.readFields(file);
            // set the data offset on the way out so that updates write to the proper location 
            metadata.setSubDomainDataOffset(dataOffset);
        } finally {
            file.close();
        }
    }
    return metadata;
}