List of usage examples for java.io RandomAccessFile setLength
public native void setLength(long newLength) throws IOException;
From source file:org.apache.hadoop.hive.service.HSSessionItem.java
public boolean uploadModule(String user, String moduleName, byte[] module) throws HiveServerException { boolean res = true; String fname = getHome() + "/pl/lib/" + user + "/" + moduleName; RandomAccessFile raf; File f;//w w w . j a v a 2 s.co m String dname = getHome() + "/pl/lib/" + user; File d = new File(dname); if (!d.exists()) { if (!d.mkdir()) { l4j.error(getSessionName() + " try to mkdir " + dname + " failed."); throw new HiveServerException("Create user library failed."); } } File i = new File(getHome() + "/pl/lib/" + user + "/__init__.py"); if (!i.exists()) { try { i.createNewFile(); } catch (java.io.IOException oe) { } } try { f = new File(fname); if (!f.exists()) { if (!f.createNewFile()) { l4j.error("Try to create file " + fname + " failed."); throw new HiveServerException("Create user package failed."); } } } catch (java.io.IOException ex) { l4j.error(getSessionName() + " try to create file " + fname + " failed w/ " + ex); return false; } if (module.length == 0) { if (!f.delete()) { l4j.error("Try to delete file " + fname + " failed."); throw new HiveServerException("Delete user package " + fname + " failed."); } else { return true; } } try { raf = new RandomAccessFile(f, "rw"); } catch (java.io.FileNotFoundException ex) { l4j.error(getSessionName() + " try to open file " + fname + " failed, not found."); return false; } try { raf.setLength(0); raf.seek(0); raf.write(module); } catch (java.io.IOException ex) { l4j.error(getSessionName() + " try to truncate/write file " + fname + "failed w/ " + ex); return false; } return res; }
From source file:org.apache.catalina.servlets.DefaultServlet.java
/** * Handle a partial PUT. New content specified in request is appended to * existing content in oldRevisionContent (if present). This code does * not support simultaneous partial updates to the same resource. * * @param req Description of the Parameter * @param range Description of the Parameter * @param path Description of the Parameter * @return Description of the Return Value * @throws IOException Description of the Exception *//*w ww. j a v a 2s. com*/ protected File executePartialPut(HttpServletRequest req, Range range, String path) throws IOException { // Append data specified in ranges to existing content for this // resource - create a temp. file on the local filesystem to // perform this operation File tempDir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir"); // Convert all '/' characters to '.' in resourcePath String convertedResourcePath = path.replace('/', '.'); File contentFile = new File(tempDir, convertedResourcePath); if (contentFile.createNewFile()) { // Clean up contentFile when Tomcat is terminated contentFile.deleteOnExit(); } RandomAccessFile randAccessContentFile = new RandomAccessFile(contentFile, "rw"); Resource oldResource = null; try { Object obj = getResources().lookup(path); if (obj instanceof Resource) { oldResource = (Resource) obj; } } catch (NamingException e) { } // Copy data in oldRevisionContent to contentFile if (oldResource != null) { BufferedInputStream bufOldRevStream = new BufferedInputStream(oldResource.streamContent(), BUFFER_SIZE); int numBytesRead; byte[] copyBuffer = new byte[BUFFER_SIZE]; while ((numBytesRead = bufOldRevStream.read(copyBuffer)) != -1) { randAccessContentFile.write(copyBuffer, 0, numBytesRead); } bufOldRevStream.close(); } randAccessContentFile.setLength(range.length); // Append data in request input stream to contentFile randAccessContentFile.seek(range.start); int numBytesRead; byte[] transferBuffer = new byte[BUFFER_SIZE]; BufferedInputStream requestBufInStream = new BufferedInputStream(req.getInputStream(), BUFFER_SIZE); while ((numBytesRead = requestBufInStream.read(transferBuffer)) != -1) { randAccessContentFile.write(transferBuffer, 0, numBytesRead); } randAccessContentFile.close(); requestBufInStream.close(); return contentFile; }
From source file:org.commoncrawl.service.listcrawler.CrawlHistoryManager.java
/** * /*from w ww .j av a2s . c om*/ * @return a sorted map of urlfp to item * @throws IOException */ TreeMap<URLFP, ProxyCrawlHistoryItem> loadLocalLogItemMap() throws IOException { TreeMap<URLFP, ProxyCrawlHistoryItem> itemMap = new TreeMap<URLFP, ProxyCrawlHistoryItem>(); LOG.info("Reading Local Log File"); RandomAccessFile file = new RandomAccessFile(getActiveLogFilePath(), "rw"); // valid length indicator ... long validLength = 0; try { // skip header ... file.seek(LocalLogFileHeader.SIZE); validLength = file.getFilePointer(); // ok walk n items ... for (int itemIdx = 0; itemIdx < _header._itemCount && file.getChannel().position() <= _header._fileSize; ++itemIdx) { try { ProxyCrawlHistoryItem item = readItem(file); // update valid length ... validLength = file.getFilePointer(); // ok compute fingerprint for item ... URLFP fingerprintObject = URLUtils.getURLFPFromURL(item.getOriginalURL(), true); if (fingerprintObject == null) { LOG.error("Could not compute fingerprint for URL:" + item.getOriginalURL()); } else { itemMap.put(fingerprintObject, item); } } catch (IOException e) { LOG.error(CCStringUtils.stringifyException(e)); try { if (!seekToNextSyncBytesPos(file)) { LOG.error("Hit EOF While Seeking for next SyncByte Sequence!"); break; } else { LOG.info("Seek to Next SyncByte Succeeded! Continuing Load"); } } catch (IOException e2) { LOG.error(CCStringUtils.stringifyException(e2)); LOG.error("Got IO Exception Reading SyncBytes - Bailing!"); break; } } } } finally { if (file.length() > validLength) { LOG.warn("File Length is:" + file.length() + " Truncating Length to:" + validLength); file.setLength(validLength); } file.close(); } LOG.info("Done Reading Local Log File"); return itemMap; }
From source file:org.opencms.webdav.CmsWebdavServlet.java
/** * Handle a partial PUT.<p>/*from ww w .j a v a 2s .c om*/ * * New content specified in request is appended to * existing content in oldRevisionContent (if present). This code does * not support simultaneous partial updates to the same resource.<p> * * @param req the servlet request we are processing * @param range the range of the content in the file * @param path the path where to find the resource * * @return the new content file with the appended data * * @throws IOException if an input/output error occurs */ protected File executePartialPut(HttpServletRequest req, CmsWebdavRange range, String path) throws IOException { // Append data specified in ranges to existing content for this // resource - create a temp. file on the local filesystem to // perform this operation File tempDir = (File) getServletContext().getAttribute(ATT_SERVLET_TEMPDIR); // Convert all '/' characters to '.' in resourcePath String convertedResourcePath = path.replace('/', '.'); File contentFile = new File(tempDir, convertedResourcePath); contentFile.createNewFile(); RandomAccessFile randAccessContentFile = new RandomAccessFile(contentFile, "rw"); InputStream oldResourceStream = null; try { I_CmsRepositoryItem item = m_session.getItem(path); oldResourceStream = new ByteArrayInputStream(item.getContent()); } catch (CmsException e) { if (LOG.isErrorEnabled()) { LOG.error(Messages.get().getBundle().key(Messages.LOG_ITEM_NOT_FOUND_1, path), e); } } // Copy data in oldRevisionContent to contentFile if (oldResourceStream != null) { int numBytesRead; byte[] copyBuffer = new byte[BUFFER_SIZE]; while ((numBytesRead = oldResourceStream.read(copyBuffer)) != -1) { randAccessContentFile.write(copyBuffer, 0, numBytesRead); } oldResourceStream.close(); } randAccessContentFile.setLength(range.getLength()); // Append data in request input stream to contentFile randAccessContentFile.seek(range.getStart()); int numBytesRead; byte[] transferBuffer = new byte[BUFFER_SIZE]; BufferedInputStream requestBufInStream = new BufferedInputStream(req.getInputStream(), BUFFER_SIZE); while ((numBytesRead = requestBufInStream.read(transferBuffer)) != -1) { randAccessContentFile.write(transferBuffer, 0, numBytesRead); } randAccessContentFile.close(); requestBufInStream.close(); return contentFile; }
From source file:org.gss_project.gss.server.rest.Webdav.java
/** * Handle a partial PUT. New content specified in request is appended to * existing content in oldRevisionContent (if present). This code does not * support simultaneous partial updates to the same resource. * * @param req/*from ww w .j a v a 2 s .c o m*/ * @param range * @param path * @return * @throws IOException * @throws RpcException * @throws InsufficientPermissionsException * @throws ObjectNotFoundException */ protected File executePartialPut(HttpServletRequest req, Range range, String path) throws IOException, RpcException, ObjectNotFoundException, InsufficientPermissionsException { // Append data specified in ranges to existing content for this // resource - create a temporary file on the local file system to // perform this operation. File tempDir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir"); // Convert all '/' characters to '.' in resourcePath String convertedResourcePath = path.replace('/', '.'); File contentFile = new File(tempDir, convertedResourcePath); if (contentFile.createNewFile()) // Clean up contentFile when Tomcat is terminated. contentFile.deleteOnExit(); RandomAccessFile randAccessContentFile = new RandomAccessFile(contentFile, "rw"); User user = getUser(req); User owner = getOwner(req); FileHeader oldResource = null; try { Object obj = getService().getResourceAtPath(owner.getId(), path, true); if (obj instanceof FileHeader) oldResource = (FileHeader) obj; } catch (ObjectNotFoundException e) { // Do nothing. } // Copy data in oldRevisionContent to contentFile if (oldResource != null) { InputStream contents = getService().getFileContents(user.getId(), oldResource.getId()); BufferedInputStream bufOldRevStream = new BufferedInputStream(contents, BUFFER_SIZE); int numBytesRead; byte[] copyBuffer = new byte[BUFFER_SIZE]; while ((numBytesRead = bufOldRevStream.read(copyBuffer)) != -1) randAccessContentFile.write(copyBuffer, 0, numBytesRead); bufOldRevStream.close(); } randAccessContentFile.setLength(range.length); // Append data in request input stream to contentFile randAccessContentFile.seek(range.start); int numBytesRead; byte[] transferBuffer = new byte[BUFFER_SIZE]; BufferedInputStream requestBufInStream = new BufferedInputStream(req.getInputStream(), BUFFER_SIZE); while ((numBytesRead = requestBufInStream.read(transferBuffer)) != -1) randAccessContentFile.write(transferBuffer, 0, numBytesRead); randAccessContentFile.close(); requestBufInStream.close(); return contentFile; }