List of usage examples for java.io RandomAccessFile length
public native long length() throws IOException;
From source file:com.sky.drovik.player.media.DiskCache.java
public void put(long key, byte[] data, long timestamp) { // Check to see if the record already exists. Record record = null;//w w w . java 2s. c o m synchronized (mIndexMap) { record = mIndexMap.get(key); } if (record != null && data.length <= record.sizeOnDisk) { // We just replace the chunk. int currentChunk = record.chunk; try { RandomAccessFile chunkFile = getChunkFile(record.chunk); if (chunkFile != null) { chunkFile.seek(record.offset); chunkFile.write(data); synchronized (mIndexMap) { mIndexMap.put(key, new Record(currentChunk, record.offset, data.length, record.sizeOnDisk, timestamp)); } if (++mNumInsertions == 32) { // CR: 32 => constant // Flush the index file at a regular interval. To avoid // writing the entire // index each time the format could be changed to an // append-only journal with // a snapshot generated on exit. flush(); } return; } } catch (Exception e) { Log.e(TAG, "Unable to read from chunk file"); } } // Append a new chunk to the current chunk. final int chunk = mTailChunk; final RandomAccessFile chunkFile = getChunkFile(chunk); if (chunkFile != null) { try { final int offset = (int) chunkFile.length(); chunkFile.seek(offset); chunkFile.write(data); synchronized (mIndexMap) { mIndexMap.put(key, new Record(chunk, offset, data.length, data.length, timestamp)); } if (offset + data.length > CHUNK_SIZE) { ++mTailChunk; } if (++mNumInsertions == 32) { // CR: 32 => constant // Flush the index file at a regular interval. To avoid // writing the entire // index each time the format could be changed to an // append-only journal with // a snapshot generated on exit. flush(); } } catch (IOException e) { Log.e(TAG, "Unable to write new entry to chunk file"); } } else { Log.e(TAG, "getChunkFile() returned null"); } }
From source file:com.netscape.cms.logging.LogFile.java
/** * Open the log file. This creates the buffered FileWriter * *//*w ww .j av a2s .c o m*/ protected synchronized void open() throws IOException { RandomAccessFile out; try { out = new RandomAccessFile(mFile, "rw"); out.seek(out.length()); //XXX int or long? mBytesWritten = (int) out.length(); if (!Utils.isNT()) { try { Utils.exec("chmod 00640 " + mFile.getCanonicalPath()); } catch (IOException e) { CMS.debug("Unable to change file permissions on " + mFile.toString()); } } mLogWriter = new BufferedWriter(new FileWriter(out.getFD()), mBufferSize); // The first time we open, mSignature will not have been // initialized yet. That's ok, we will push our first signature // in setupSigning(). if (mLogSigning && (mSignature != null)) { try { pushSignature(); } catch (ELogException le) { ConsoleError.send(new SystemEvent(CMS.getUserMessage("CMS_LOG_ILLEGALARGUMENT", mFileName))); } } } catch (IllegalArgumentException iae) { ConsoleError.send(new SystemEvent(CMS.getUserMessage("CMS_LOG_ILLEGALARGUMENT", mFileName))); } catch (GeneralSecurityException gse) { // error with signed audit log, shutdown CMS ConsoleError .send(new SystemEvent(CMS.getUserMessage("CMS_LOG_OPEN_FAILED", mFileName, gse.toString()))); gse.printStackTrace(); shutdownCMS(); } mBytesUnflushed = 0; }
From source file:it.drwolf.ridire.session.CrawlerManager.java
private long getURICount(Job job, String whichCount, User currentUser) throws IOException, HeritrixException, DocumentException, XPathExpressionException, SAXException { // this.updateJobsList(currentUser); Pattern pURICount = Pattern.compile( "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)", Pattern.MULTILINE); String jobName = job.getName(); Job j = this.getPersistedJob(jobName); if (j == null) { return 0L; }/*from w w w. ja v a 2s . c o m*/ if (job.getChildJobName() != null && job.getChildJobName().length() > 0) { jobName = job.getChildJobName(); } String dir = this.entityManager.find(Parameter.class, Parameter.JOBS_DIR.getKey()).getValue(); long uriCountFromCrawlReport = 0L; long queuedURICount = 0L; long discoveredURICount = 0L; HttpMethod method = null; String jobStatus = this.getJobStatus(jobName); // jobName = jobName.replaceAll(" ", "\\\\ "); try { while (true) { if (jobStatus.equals(CrawlStatus.RUNNING.toString())) { RandomAccessFile progressStatistics = null; try { progressStatistics = new RandomAccessFile(this.jobsDir + CrawlerManager.FILE_SEPARATOR + jobName + CrawlerManager.FILE_SEPARATOR + "logs" + CrawlerManager.FILE_SEPARATOR + "progress-statistics.log", "r"); if (progressStatistics != null) { progressStatistics.seek(Math.max(0, progressStatistics.length() - 3000)); String line = progressStatistics.readLine(); StringBuffer buffer = new StringBuffer(); while (line != null) { buffer.append(line + "\n"); line = progressStatistics.readLine(); } String progressStatisticsContent = buffer.toString(); Matcher m = pURICount.matcher(progressStatisticsContent); int start = 0; long queuedURICountTemp = 0L; long discoveredURICountTemp = 0L; long uriCountFromCrawlReportTemp = 0L; while (m.find(start)) { start = m.end(); queuedURICountTemp = Long.parseLong(m.group(2)); discoveredURICountTemp = Long.parseLong(m.group(1)); uriCountFromCrawlReportTemp = Long.parseLong(m.group(3)); } queuedURICount += queuedURICountTemp; discoveredURICount = discoveredURICountTemp; uriCountFromCrawlReport = uriCountFromCrawlReportTemp; } } catch (FileNotFoundException e) { // TODO: handle exception } finally { if (progressStatistics != null) { progressStatistics.close(); } } break; } else if (whichCount.equalsIgnoreCase("finishedURICount")) { File reportFile = new File( dir + CrawlerManager.FILE_SEPARATOR + jobName + CrawlerManager.FILE_SEPARATOR + "reports" + CrawlerManager.FILE_SEPARATOR + "crawl-report.txt"); if (reportFile.exists() && reportFile.canRead()) { String content = FileUtils.readFileToString(reportFile); Matcher m = CrawlerManager.pFinishedURICount.matcher(content); if (m.find()) { String bytes = m.group(1); uriCountFromCrawlReport += Long.parseLong(bytes); } } Matcher m = CrawlerManager.childJobPattern.matcher(jobName); if (m.matches()) { Integer count = Integer.parseInt(m.group(1)); if (count > 1) { count--; jobName = jobName.substring(0, jobName.indexOf("__")) + "__" + count; } else if (count == 1) { jobName = jobName.substring(0, jobName.indexOf("__")); } else { break; } } else { break; } } else { return 0L; } } } finally { if (method != null) { method.releaseConnection(); } } if (whichCount.equals("discoveredUriCount")) { return discoveredURICount; } if (whichCount.equals("queuedUriCount")) { return queuedURICount; } return uriCountFromCrawlReport; }
From source file:org.commoncrawl.service.listcrawler.CacheManager.java
/** * loadCache - load local cache from disk * @param activeLogPath/*from w ww . j a v a 2s .c o m*/ * @param logFileHeader * @throws IOException */ private synchronized void loadCache(File activeLogPath, LocalLogFileHeader logFileHeader) throws IOException { RandomAccessFile file = new RandomAccessFile(getActiveLogFilePath(), "rw"); byte[] syncCheck = new byte[_header._sync.length]; try { long lastValidPos = LocalLogFileHeader.SIZE; long currentPos = lastValidPos; long endPos = file.length(); CacheItemHeader itemHeader = new CacheItemHeader(); // start read while (currentPos < endPos) { if ((endPos - currentPos) < LocalLogFileHeader.SYNC_BYTES_SIZE) break; // seek to current position ... file.seek(currentPos); boolean headerLoadFailed = false; try { // read the item header ... assuming things are good so far ... itemHeader.readHeader(file); } catch (IOException e) { LOG.error("### Item Header Load Failed With Exception:" + CCStringUtils.stringifyException(e)); headerLoadFailed = true; } if (headerLoadFailed) { LOG.error("### Item File Corrupt at position:" + currentPos + " Seeking Next Sync Point"); currentPos += LocalLogFileHeader.SYNC_BYTES_SIZE; } // if header sync bytes don't match .. then seek to next sync position ... if (headerLoadFailed || !Arrays.equals(itemHeader._sync, _header._sync)) { LOG.error("### Item File Corrupt at position:" + currentPos + " Seeking Next Sync Point"); // reseek to current pos file.seek(currentPos); // read in a sync.length buffer amount file.readFully(syncCheck); int syncLen = _header._sync.length; // start scan for next sync position ... for (int i = 0; file.getFilePointer() < endPos; i++) { int j = 0; for (; j < syncLen; j++) { if (_header._sync[j] != syncCheck[(i + j) % syncLen]) break; } if (j == syncLen) { file.seek(file.getFilePointer() - LocalLogFileHeader.SYNC_BYTES_SIZE); // position before sync break; } syncCheck[i % syncLen] = file.readByte(); } // whatever, happened file pointer is at current pos currentPos = file.getFilePointer(); if (currentPos < endPos) { LOG.info("### Item Loader Found another sync point at:" + currentPos); } else { LOG.error("### No more sync points found!"); } } else { // ok figure out next steps based on header ... // for now, just add item to our list ... _fingerprintToLocalLogPos.put(itemHeader._fingerprint, _localLogStartOffset + currentPos); // now seek past data currentPos += CacheItemHeader.SIZE + itemHeader._dataLength + ITEM_RECORD_TRAILING_BYTES; } } } finally { if (file != null) { file.close(); } } }
From source file:org.commoncrawl.service.listcrawler.CrawlList.java
public static void dumpUnCrawledItems(File dataDir, long listId, File outputFilePath, boolean includeRobotsExcludedItems) throws IOException { File fixedDataFile = new File(dataDir, LIST_VALUE_MAP_PREFIX + Long.toString(listId)); File variableDataFile = new File(dataDir, LIST_STRING_MAP_PREFIX + Long.toString(listId)); LOG.info("FixedDataFile is:" + fixedDataFile); LOG.info("VariableDataFile is:" + variableDataFile); RandomAccessFile fixedDataReader = new RandomAccessFile(fixedDataFile, "r"); RandomAccessFile stringDataReader = new RandomAccessFile(variableDataFile, "r"); JsonWriter writer = new JsonWriter(new BufferedWriter(new FileWriter(outputFilePath), 1024 * 1024 * 10)); writer.setIndent(" "); try {// www. j a v a 2 s.c om writer.beginObject(); writer.name("urls"); writer.beginArray(); try { OnDiskCrawlHistoryItem item = new OnDiskCrawlHistoryItem(); URLFP fingerprint = new URLFP(); while (fixedDataReader.getFilePointer() != fixedDataReader.length()) { long position = fixedDataReader.getFilePointer(); item.deserialize(fixedDataReader); // seek to string data stringDataReader.seek(item._stringsOffset); // and skip buffer length WritableUtils.readVInt(stringDataReader); // and read primary string String url = stringDataReader.readUTF(); // setup fingerprint fingerprint.setDomainHash(item._domainHash); fingerprint.setUrlHash(item._urlFingerprint); // any item that has not been crawled needs to be queued boolean queueItem = !item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_CRAWL_STATUS); // if item is not queued, check to see if we need to retry the item if (!queueItem && item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_CRAWL_STATUS)) { if (item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_REDIRECT_STATUS)) { queueItem = (item._redirectStatus != 0); if (!queueItem) { if (item._redirectHttpResult != 200 && item._redirectHttpResult != 404) { queueItem = true; } } } else { queueItem = (item._crawlStatus != 0); if (!queueItem) { if (item._httpResultCode != 200 && item._httpResultCode != 404) { queueItem = true; } } } } if (queueItem) { // ok if queue item is set ... writer.beginObject(); writer.name("url"); writer.value(url); writer.name("redirected"); writer.value((boolean) item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_REDIRECT_STATUS)); writer.name("lastStatus"); if (item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_REDIRECT_STATUS)) { if (item._redirectStatus == 0) { writer.value("HTTP-" + item._redirectHttpResult); } else { writer.value(CrawlURL.FailureReason.toString(item._redirectHttpResult)); } } else { if (item.isFlagSet(OnDiskCrawlHistoryItem.FLAG_HAS_CRAWL_STATUS)) { if (item._crawlStatus == 0) { writer.value("HTTP-" + item._httpResultCode); } else { writer.value(CrawlURL.FailureReason.toString(item._crawlStatus)); } } else { writer.value("UNCRAWLED"); } } writer.name("updateTime"); writer.value(item._updateTimestamp); writer.endObject(); } } } catch (IOException e) { LOG.error("Encountered Exception Queueing Items for List:" + listId + " Exception:" + CCStringUtils.stringifyException(e)); } finally { fixedDataReader.close(); stringDataReader.close(); } writer.endArray(); writer.endObject(); } catch (Exception e) { LOG.error(CCStringUtils.stringifyException(e)); throw new IOException(e); } finally { writer.flush(); writer.close(); } }
From source file:org.openmeetings.servlet.outputhandler.DownloadHandler.java
@Override protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { try {/*from w w w .j av a 2 s . co m*/ if (getUserManagement() == null || getSessionManagement() == null) { return; } httpServletRequest.setCharacterEncoding("UTF-8"); log.debug("\nquery = " + httpServletRequest.getQueryString()); log.debug("\n\nfileName = " + httpServletRequest.getParameter("fileName")); log.debug("\n\nparentPath = " + httpServletRequest.getParameter("parentPath")); String queryString = httpServletRequest.getQueryString(); if (queryString == null) { queryString = ""; } String sid = httpServletRequest.getParameter("sid"); if (sid == null) { sid = "default"; } log.debug("sid: " + sid); Long users_id = getSessionManagement().checkSession(sid); Long user_level = getUserManagement().getUserLevelByID(users_id); if (user_level != null && user_level > 0) { String room_id = httpServletRequest.getParameter("room_id"); if (room_id == null) { room_id = "default"; } String moduleName = httpServletRequest.getParameter("moduleName"); if (moduleName == null) { moduleName = "nomodule"; } String parentPath = httpServletRequest.getParameter("parentPath"); if (parentPath == null) { parentPath = "nomodule"; } String requestedFile = httpServletRequest.getParameter("fileName"); if (requestedFile == null) { requestedFile = ""; } String fileExplorerItemIdParam = httpServletRequest.getParameter("fileExplorerItemId"); Long fileExplorerItemId = null; if (fileExplorerItemIdParam != null) { fileExplorerItemId = Long.parseLong(fileExplorerItemIdParam); } // make a complete name out of domain(organisation) + roomname String roomName = room_id; // trim whitespaces cause it is a directory name roomName = StringUtils.deleteWhitespace(roomName); // Get the current User-Directory String current_dir = getServletContext().getRealPath("/"); String working_dir = ""; working_dir = current_dir + OpenmeetingsVariables.UPLOAD_DIR + File.separatorChar; // Add the Folder for the Room if (moduleName.equals("lzRecorderApp")) { working_dir = current_dir + OpenmeetingsVariables.STREAMS_DIR + File.separatorChar + "hibernate" + File.separatorChar; } else if (moduleName.equals("videoconf1")) { if (parentPath.length() != 0) { if (parentPath.equals("/")) { working_dir = working_dir + roomName + File.separatorChar; } else { working_dir = working_dir + roomName + File.separatorChar + parentPath + File.separatorChar; } } else { working_dir = current_dir + roomName + File.separatorChar; } } else if (moduleName.equals("userprofile")) { working_dir += "profiles" + File.separatorChar; logNonExistentFolder(working_dir); working_dir += ScopeApplicationAdapter.profilesPrefix + users_id + File.separatorChar; logNonExistentFolder(working_dir); } else if (moduleName.equals("remoteuserprofile")) { working_dir += "profiles" + File.separatorChar; logNonExistentFolder(working_dir); String remoteUser_id = httpServletRequest.getParameter("remoteUserid"); if (remoteUser_id == null) { remoteUser_id = "0"; } working_dir += ScopeApplicationAdapter.profilesPrefix + remoteUser_id + File.separatorChar; logNonExistentFolder(working_dir); } else if (moduleName.equals("remoteuserprofilebig")) { working_dir += "profiles" + File.separatorChar; logNonExistentFolder(working_dir); String remoteUser_id = httpServletRequest.getParameter("remoteUserid"); if (remoteUser_id == null) { remoteUser_id = "0"; } working_dir += ScopeApplicationAdapter.profilesPrefix + remoteUser_id + File.separatorChar; logNonExistentFolder(working_dir); requestedFile = this.getBigProfileUserName(working_dir); } else if (moduleName.equals("chat")) { working_dir += "profiles" + File.separatorChar; logNonExistentFolder(working_dir); String remoteUser_id = httpServletRequest.getParameter("remoteUserid"); if (remoteUser_id == null) { remoteUser_id = "0"; } working_dir += ScopeApplicationAdapter.profilesPrefix + remoteUser_id + File.separatorChar; logNonExistentFolder(working_dir); requestedFile = this.getChatUserName(working_dir); } else { working_dir = working_dir + roomName + File.separatorChar; } if (!moduleName.equals("nomodule")) { log.debug("requestedFile: " + requestedFile + " current_dir: " + working_dir); String full_path = working_dir + requestedFile; File f = new File(full_path); // If the File does not exist or is not readable show/load a // place-holder picture if (!f.exists() || !f.canRead()) { if (!f.canRead()) { log.debug("LOG DownloadHandler: The request file is not readable"); } else { log.debug( "LOG DownloadHandler: The request file does not exist / has already been deleted"); } log.debug("LOG ERROR requestedFile: " + requestedFile); // replace the path with the default picture/document if (requestedFile.endsWith(".jpg")) { log.debug("LOG endsWith d.jpg"); log.debug("LOG moduleName: " + moduleName); requestedFile = DownloadHandler.defaultImageName; if (moduleName.equals("remoteuserprofile")) { requestedFile = DownloadHandler.defaultProfileImageName; } else if (moduleName.equals("remoteuserprofilebig")) { requestedFile = DownloadHandler.defaultProfileImageNameBig; } else if (moduleName.equals("userprofile")) { requestedFile = DownloadHandler.defaultProfileImageName; } else if (moduleName.equals("chat")) { requestedFile = DownloadHandler.defaultChatImageName; } // request for an image full_path = current_dir + "default" + File.separatorChar + requestedFile; } else if (requestedFile.endsWith(".swf")) { requestedFile = DownloadHandler.defaultSWFName; // request for a SWFPresentation full_path = current_dir + "default" + File.separatorChar + DownloadHandler.defaultSWFName; } else { // Any document, must be a download request // OR a Moodle Loggedin User requestedFile = DownloadHandler.defaultImageName; full_path = current_dir + "default" + File.separatorChar + DownloadHandler.defaultImageName; } } log.debug("full_path: " + full_path); File f2 = new File(full_path); if (!f2.exists() || !f2.canRead()) { if (!f2.canRead()) { log.debug( "DownloadHandler: The request DEFAULT-file does not exist / has already been deleted"); } else { log.debug( "DownloadHandler: The request DEFAULT-file does not exist / has already been deleted"); } // no file to handle abort processing return; } // Requested file is outside OM webapp folder File curDirFile = new File(current_dir); if (!f2.getCanonicalPath().startsWith(curDirFile.getCanonicalPath())) { throw new Exception("Invalid file requested: f2.cp == " + f2.getCanonicalPath() + "; curDir.cp == " + curDirFile.getCanonicalPath()); } // Get file and handle download RandomAccessFile rf = new RandomAccessFile(full_path, "r"); // Default type - Explorer, Chrome and others int browserType = 0; // Firefox and Opera browsers if (httpServletRequest.getHeader("User-Agent") != null) { if ((httpServletRequest.getHeader("User-Agent").contains("Firefox")) || (httpServletRequest.getHeader("User-Agent").contains("Opera"))) { browserType = 1; } } log.debug("Detected browser type:" + browserType); httpServletResponse.reset(); httpServletResponse.resetBuffer(); OutputStream out = httpServletResponse.getOutputStream(); if (requestedFile.endsWith(".swf")) { // trigger download to SWF => THIS is a workaround for // Flash Player 10, FP 10 does not seem // to accept SWF-Downloads with the Content-Disposition // in the Header httpServletResponse.setContentType("application/x-shockwave-flash"); httpServletResponse.setHeader("Content-Length", "" + rf.length()); } else { httpServletResponse.setContentType("APPLICATION/OCTET-STREAM"); String fileNameResult = requestedFile; if (fileExplorerItemId != null && fileExplorerItemId > 0) { FileExplorerItem fileExplorerItem = getFileExplorerItemDaoImpl() .getFileExplorerItemsById(fileExplorerItemId); if (fileExplorerItem != null) { fileNameResult = fileExplorerItem.getFileName().substring(0, fileExplorerItem.getFileName().length() - 4) + fileNameResult.substring(fileNameResult.length() - 4, fileNameResult.length()); } } if (browserType == 0) { httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileNameResult, "UTF-8")); } else { httpServletResponse.setHeader("Content-Disposition", "attachment; filename*=UTF-8'en'" + java.net.URLEncoder.encode(fileNameResult, "UTF-8")); } httpServletResponse.setHeader("Content-Length", "" + rf.length()); } byte[] buffer = new byte[1024]; int readed = -1; while ((readed = rf.read(buffer, 0, buffer.length)) > -1) { out.write(buffer, 0, readed); } rf.close(); out.flush(); out.close(); } } else { System.out.println("ERROR DownloadHandler: not authorized FileDownload " + (new Date())); } } catch (Exception er) { log.error("Error downloading: ", er); // er.printStackTrace(); } }
From source file:com.siblinks.ws.service.impl.UploadEssayServiceImpl.java
/** * {@inheritDoc}//from w w w . j a v a2 s . c om */ @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.commoncrawl.service.pagerank.slave.PageRankUtils.java
private static void runIDReadBenchmark(String[] args) { File idsFile = new File(args[1]); URLFPV2 fingerPrint = new URLFPV2(); LOG.info("Opening ID File at path:" + idsFile.getAbsolutePath()); RandomAccessFile stream = null; try {//www .j a va 2s.c o m stream = new RandomAccessFile(idsFile, "r"); long length = stream.length(); int idCount = 0; long totalStartTime = System.currentTimeMillis(); long snapshotTime = System.currentTimeMillis(); boolean error = false; while (!error) { fingerPrint.readFields(stream); ++idCount; if (idCount % 10000 == 0) { LOG.info("Read 10000 ids in:" + (System.currentTimeMillis() - snapshotTime) + " MS"); snapshotTime = System.currentTimeMillis(); } } LOG.info("Completed Reading a Total of:" + idCount + " IDs in:" + (System.currentTimeMillis() - totalStartTime) + " MS"); } catch (IOException e) { LOG.error(CCStringUtils.stringifyException(e)); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { } } } }
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);/*from w w w . j a va 2 s . c om*/ 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:org.openmeetings.servlet.outputhandler.BackupExport.java
public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ServletContext servletCtx) throws ServletException, IOException { String sid = httpServletRequest.getParameter("sid"); if (sid == null) { sid = "default"; }/* w w w. j a v a2 s .c o m*/ log.debug("sid: " + sid); Long users_id = sessionManagement.checkSession(sid); Long user_level = userManagement.getUserLevelByID(users_id); log.debug("users_id: " + users_id); log.debug("user_level: " + user_level); if (authLevelManagement.checkAdminLevel(user_level)) { // if (true) { String includeFileOption = httpServletRequest.getParameter("includeFileOption"); boolean includeFiles = includeFileOption == null || "yes".equals(includeFileOption); String moduleName = httpServletRequest.getParameter("moduleName"); if (moduleName == null) { moduleName = "moduleName"; } log.debug("moduleName: " + moduleName); if (moduleName.equals("backup")) { /* * ##################### Create Base Folder structure */ String current_dir = servletCtx.getRealPath("/"); File working_dir = new File(new File(current_dir, OpenmeetingsVariables.UPLOAD_DIR), "backup"); if (!working_dir.exists()) { working_dir.mkdir(); } String dateString = "backup_" + CalendarPatterns.getTimeForStreamId(new Date()); File backup_dir = new File(working_dir, dateString); String requestedFile = dateString + ".zip"; File backupFile = new File(backup_dir, requestedFile); String full_path = backupFile.getAbsolutePath(); try { performExport(full_path, backup_dir, includeFiles, current_dir); RandomAccessFile rf = new RandomAccessFile(full_path, "r"); httpServletResponse.reset(); httpServletResponse.resetBuffer(); httpServletResponse.setContentType("APPLICATION/OCTET-STREAM"); httpServletResponse.setHeader("Content-Disposition", "attachment; filename=\"" + requestedFile + "\""); httpServletResponse.setHeader("Content-Length", "" + rf.length()); OutputStream out = httpServletResponse.getOutputStream(); byte[] buffer = new byte[1024]; int readed = -1; while ((readed = rf.read(buffer, 0, buffer.length)) > -1) { out.write(buffer, 0, readed); } rf.close(); out.flush(); out.close(); } catch (Exception er) { log.error("Error exporting: ", er); } if (backupFile.exists()) { // log.debug("DELETE :1: "+backupFile.getAbsolutePath()); backupFile.delete(); } deleteDirectory(backup_dir); } } else { log.debug("ERROR LangExport: not authorized FileDownload " + (new Date())); } }