List of usage examples for java.io RandomAccessFile write
public void write(byte b[], int off, int len) throws IOException
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 . ja v a2s . 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; }
From source file:org.commoncrawl.service.listcrawler.CrawlList.java
void resetSubDomainCounts() throws IOException { LOG.info("*** LIST:" + getListId() + " Reset SubDomain Queued Counts."); if (_subDomainMetadataFile.exists()) { LOG.info("*** LIST:" + getListId() + " FILE EXISTS ."); RandomAccessFile file = new RandomAccessFile(_subDomainMetadataFile, "rw"); DataInputBuffer inputBuffer = new DataInputBuffer(); DataOutputBuffer outputBuffer = new DataOutputBuffer(CrawlListMetadata.Constants.FixedDataSize); try {/*from ww w .jav a 2 s . c o m*/ // skip version file.read(); // read item count int itemCount = file.readInt(); LOG.info("*** LIST:" + getListId() + " SUBDOMAIN ITEM COUNT:" + itemCount); CrawlListMetadata newMetadata = new CrawlListMetadata(); for (int i = 0; i < itemCount; ++i) { long orignalPos = file.getFilePointer(); file.readFully(outputBuffer.getData(), 0, CrawlListMetadata.Constants.FixedDataSize); inputBuffer.reset(outputBuffer.getData(), CrawlListMetadata.Constants.FixedDataSize); try { newMetadata.deserialize(inputBuffer, new BinaryProtocol()); } catch (Exception e) { LOG.error("-----Failed to Deserialize Metadata at Index:" + i + " Exception:" + CCStringUtils.stringifyException(e)); } // ok reset everything except hashes and first/last url pointers int urlCount = newMetadata.getUrlCount(); long firstRecordOffset = newMetadata.getFirstRecordOffset(); long lastRecordOffset = newMetadata.getLastRecordOffset(); String domainName = newMetadata.getDomainName(); long domainHash = newMetadata.getDomainHash(); // reset newMetadata.clear(); // restore newMetadata.setUrlCount(urlCount); newMetadata.setFirstRecordOffset(firstRecordOffset); newMetadata.setLastRecordOffset(lastRecordOffset); newMetadata.setDomainName(domainName); newMetadata.setDomainHash(domainHash); // serialize it ... outputBuffer.reset(); newMetadata.serialize(outputBuffer, new BinaryProtocol()); // write it back to disk file.seek(orignalPos); // and rewrite it ... file.write(outputBuffer.getData(), 0, CrawlListMetadata.Constants.FixedDataSize); } } finally { file.close(); } LOG.info("*** LIST:" + getListId() + " DONE RESETTIGN SUBDOMAIN METADATA QUEUE COUNTS"); } }
From source file:au.org.ala.layers.intersect.Grid.java
public void replaceValues(Map<Integer, Integer> translation) { long length = ((long) nrows) * ((long) ncols); Integer minv = null;/*from w w w.j a v a 2 s . c o m*/ Integer maxv = null; for (Integer i : translation.values()) { if (minv == null || i < minv) minv = i; if (maxv == null || i > maxv) maxv = i; } RandomAccessFile afile = null; RandomAccessFile out = null; File f2 = new File(filename + ".GRI"); File newGrid = new File(filename + ".gri.new"); try { //read of random access file can throw an exception out = new RandomAccessFile(newGrid, "rw"); if (!f2.exists()) { afile = new RandomAccessFile(filename + ".gri", "r"); } else { afile = new RandomAccessFile(filename + ".GRI", "r"); } byte[] b = new byte[65536]; byte[] bout = new byte[65536]; long i = 0; long max = 0; long len; float v; float ndv = (float) nodatavalue; while ((len = afile.read(b)) > 0) { ByteBuffer bb = ByteBuffer.wrap(b); ByteBuffer bbout = ByteBuffer.wrap(bout); if (byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); bbout.order(ByteOrder.LITTLE_ENDIAN); } if (datatype.equalsIgnoreCase("UBYTE")) { throw new Exception("UBYTE translation not supported"); } else if (datatype.equalsIgnoreCase("BYTE")) { throw new Exception("BYTE translation not supported"); } else if (datatype.equalsIgnoreCase("SHORT")) { max += len / 2; max = Math.min(max, length); for (; i < max; i++) { v = bb.getShort(); if (v != ndv && translation.get((int) (v * rescale)) == null) { v = v; } if (v != ndv && translation.get((int) (v * rescale)) != null) v = translation.get((int) (v * rescale)); bbout.putShort((short) v); } } else if (datatype.equalsIgnoreCase("INT")) { max += len / 4; max = Math.min(max, length); for (; i < max; i++) { v = bb.getInt(); if (v != ndv && translation.get((int) (v * rescale)) != null) v = translation.get((int) (v * rescale)); bbout.putInt((int) v); } } else if (datatype.equalsIgnoreCase("LONG")) { max += len / 8; max = Math.min(max, length); for (; i < max; i++) { v = bb.getLong(); if (v != ndv && translation.get((int) (v * rescale)) != null) v = translation.get((int) (v * rescale)); bbout.putLong((long) v); } } else if (datatype.equalsIgnoreCase("FLOAT")) { throw new Exception("FLOAT translation not supported"); } else if (datatype.equalsIgnoreCase("DOUBLE")) { throw new Exception("DOUBLE translation not supported"); } else { max += len / 4; for (; i < max; i++) { // should not happen; catch anyway... } } out.write(bout, 0, (int) len); } writeHeader(filename + ".new", xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols, minv, maxv, datatype, nodatavalue + ""); } catch (Exception e) { logger.error("An error has occurred getting grid class stats", e); } finally { if (afile != null) { try { afile.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } if (out != null) { try { out.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } try { if (!new File(filename + ".gri.old").exists()) FileUtils.moveFile(new File(filename + ".gri"), new File(filename + ".gri.old")); if (!new File(filename + ".grd.old").exists()) FileUtils.moveFile(new File(filename + ".grd"), new File(filename + ".grd.old")); FileUtils.moveFile(new File(filename + ".gri.new"), new File(filename + ".gri")); FileUtils.moveFile(new File(filename + ".new.grd"), new File(filename + ".grd")); } catch (Exception e) { logger.error(e.getMessage(), e); } }
From source file:okuyama.imdst.util.FileBaseDataMap.java
/** * put Method.<br>// www . j av a 2s . co m * * @param key * @param value * @param hashCode This is a key value hash code */ public void put(String key, String value, int hashCode) { /* long start1 = 0L; long start2 = 0L; long start3 = 0L; long start4 = 0L; long end1 = 0L; long end2 = 0L; long end3 = 0L; long end4 = 0L; */ try { //start1 = System.nanoTime(); //start2 = System.nanoTime(); File file = dataFileList[hashCode % numberOfDataFiles]; StringBuilder buf = new StringBuilder(this.keyDataLength); boolean callMapSizeCalc = true; if (key != null && key.equals(FileBaseDataMap.sizeSaveKey)) callMapSizeCalc = false; //TODO:??? buf.append(this.fillCharacter(key, keyDataLength)); //buf.append(this.fillCharacter(value, oneDataLength)); CacheContainer accessor = (CacheContainer) innerCache.get(file.getAbsolutePath()); RandomAccessFile raf = null; BufferedWriter wr = null; if (accessor == null || accessor.isClosed == true) { raf = new RandomAccessFile(file, "rwd"); wr = new BufferedWriter(new FileWriter(file, true)); accessor = new CacheContainer(); accessor.raf = raf; accessor.wr = wr; accessor.file = file; innerCache.put(file.getAbsolutePath(), accessor); } else { raf = accessor.raf; wr = accessor.wr; } //end2 = System.nanoTime(); //start3 = System.nanoTime(); // KeyData Write File for (int tryIdx = 0; tryIdx < 2; tryIdx++) { try { // Key?? long[] dataLineNoRet = this.getLinePoint(key, raf); //end3 = System.nanoTime(); //start4 = System.nanoTime(); if (dataLineNoRet[0] == -1) { wr.write(buf.toString()); SystemUtil.diskAccessSync(wr); wr.write(value); SystemUtil.diskAccessSync(wr); // ???????????? int valueSize = value.length(); byte[] fillByte = new byte[1]; fillByte[0] = new Integer(FileBaseDataMap.paddingSymbol).byteValue(); int paddingSize = (oneDataLength - valueSize); int writeSetCount = paddingSize / 512; int singleWriteCount = paddingSize % 512; for (int i = 0; i < writeSetCount; i++) { wr.write(FileBaseDataMap.paddingSymbolSetString); if ((i % 14) == 0) SystemUtil.diskAccessSync(wr); } SystemUtil.diskAccessSync(wr); byte[] fillBytes = new byte[singleWriteCount]; for (int i = 0; i < singleWriteCount; i++) { fillBytes[i] = fillByte[0]; } wr.write(new String(fillBytes)); SystemUtil.diskAccessSync(wr); // The size of an increment if (callMapSizeCalc) this.getAndIncrement(); } else { // ?????1 boolean increMentFlg = false; if (dataLineNoRet[1] == -1) increMentFlg = true; //if (this.get(key, hashCode) == null) increMentFlg = true; raf.seek(dataLineNoRet[0] * (lineDataSize)); raf.write(buf.toString().getBytes(), 0, keyDataLength); raf.write(value.getBytes()); // ???????????? int valueSize = value.length(); byte[] fillByte = new byte[1]; fillByte[0] = new Integer(FileBaseDataMap.paddingSymbol).byteValue(); int paddingSize = (oneDataLength - valueSize); int writeSetCount = paddingSize / (4096); int singleWriteCount = paddingSize % (4096); for (int i = 0; i < writeSetCount; i++) { raf.write(FileBaseDataMap.fillStream.toByteArray()); } byte[] remainderPaddingBytes = new byte[singleWriteCount]; for (int i = 0; i < singleWriteCount; i++) { remainderPaddingBytes[i] = fillByte[0]; } if (remainderPaddingBytes.length > 0) raf.write(remainderPaddingBytes); if (callMapSizeCalc) { if (increMentFlg) this.getAndIncrement(); } } //end4 = System.nanoTime(); break; } catch (IOException ie) { // IOException???1????? if (tryIdx == 1) throw ie; try { if (raf != null) raf.close(); if (wr != null) wr.close(); raf = new RandomAccessFile(file, "rwd"); wr = new BufferedWriter(new FileWriter(file, true)); accessor = new CacheContainer(); accessor.raf = raf; accessor.wr = wr; accessor.file = file; innerCache.put(file.getAbsolutePath(), accessor); } catch (Exception e) { throw e; } } } } catch (Exception e2) { e2.printStackTrace(); } //end1 = System.nanoTime(); //if (ImdstDefine.fileBaseMapTimeDebug) { // System.out.println("1="+(end1 - start1) + " 2="+(end2 - start2) + " 3="+(end3 - start3) + " 4="+(end4 - start4)); //} }