List of usage examples for java.io RandomAccessFile write
public void write(byte b[]) throws IOException
From source file:org.kalypso.shape.shp.SHPFile.java
/** * Adds a new shape entry to the end of the file.<br> * This method is atomic in that sense that if an exception occurs while a shape convertet to bytes, inetad a * Null-Shape will be written.//from w ww.java 2 s .c o m */ public SHXRecord addShape(final ISHPGeometry shape, final int recordNumber) throws IOException, SHPException { if (shape == null) throw new SHPException("shape == null not allowed. Add SHPNullShape instead."); if (!(shape instanceof SHPNullShape) && shape.getType() != getShapeType()) throw new SHPException("Cannot add shape, wrong type."); final RandomAccessFile raf = getRandomAccessFile(); final long currentFileLength = raf.length(); /* Convert shape into bytes and write them in one go */ final byte[] bytes = writeRecordAsBytes(recordNumber, shape); raf.seek(currentFileLength); raf.write(bytes); /* Update header */ final SHPEnvelope shapeMbr = shape.getEnvelope(); expandMbr(shapeMbr); /* Create and return index record */ final int contentLength = bytes.length - RECORD_HEADER_BYTES; return new SHXRecord((int) currentFileLength / 2, contentLength / 2); }
From source file:org.apache.hadoop.hdfs.server.namenode.TestFSEditLogLoader.java
@Test public void testDisplayRecentEditLogOpCodes() throws IOException { // start a cluster Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = null;//from ww w .j a va 2 s.co m FileSystem fileSys = null; cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES) .enableManagedDfsDirsRedundancy(false).build(); cluster.waitActive(); fileSys = cluster.getFileSystem(); final FSNamesystem namesystem = cluster.getNamesystem(); FSImage fsimage = namesystem.getFSImage(); for (int i = 0; i < 20; i++) { fileSys.mkdirs(new Path("/tmp/tmp" + i)); } StorageDirectory sd = fsimage.getStorage().dirIterator(NameNodeDirType.EDITS).next(); cluster.shutdown(); File editFile = FSImageTestUtil.findLatestEditsLog(sd).getFile(); assertTrue("Should exist: " + editFile, editFile.exists()); // Corrupt the edits file. long fileLen = editFile.length(); RandomAccessFile rwf = new RandomAccessFile(editFile, "rw"); rwf.seek(fileLen - 40); for (int i = 0; i < 20; i++) { rwf.write(FSEditLogOpCodes.OP_DELETE.getOpCode()); } rwf.close(); StringBuilder bld = new StringBuilder(); bld.append("^Error replaying edit log at offset \\d+. "); bld.append("Expected transaction ID was \\d+\n"); bld.append("Recent opcode offsets: (\\d+\\s*){4}$"); try { cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES) .enableManagedDfsDirsRedundancy(false).format(false).build(); fail("should not be able to start"); } catch (IOException e) { assertTrue("error message contains opcodes message", e.getMessage().matches(bld.toString())); } }
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;//from ww w.j a v a 2 s. com 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:org.kuali.coeus.sys.framework.controller.interceptor.PerformanceMeasurementFilter.java
private void insertLine(File file, HttpSample httpSample) throws IOException { RandomAccessFile randomAccessFile = null; try {//w w w . j a v a 2 s . c om randomAccessFile = new RandomAccessFile(file, "rw"); randomAccessFile.skipBytes((int) (file.length() - "\n</httpSamples>".getBytes().length)); randomAccessFile.write("\n".getBytes()); randomAccessFile.write(httpSample.toXML().getBytes()); randomAccessFile.write("\n".getBytes()); randomAccessFile.write("</httpSamples>".getBytes()); } finally { if (randomAccessFile != null) { randomAccessFile.close(); } } }
From source file:com.ibm.sbt.test.lib.MockSerializer.java
public void writeData(String data) throws IOException { File file = getFile(true);// w ww.j a va 2s. c o m RandomAccessFile raf = new RandomAccessFile(file, "rw"); // Seek to end of file System.out.println("Writing Record @" + file.length() + " in " + file.getAbsolutePath()); raf.seek((file.length() - "\n</responses>".length())); raf.write(data.getBytes("UTF-8")); raf.write("\n</responses>".getBytes("UTF-8")); }
From source file:hydrograph.ui.perspective.dialog.PreStartActivity.java
private boolean updateINIFile(String javaHome) { logger.debug("Updating JAVA_HOME in ini file ::" + javaHome); javaHome = "-vm\n" + javaHome + "\n"; RandomAccessFile file = null; boolean isUpdated = false; try {//from w w w .jav a 2 s . c o m file = new RandomAccessFile(new File(HYDROGRAPH_INI), "rw"); byte[] text = new byte[(int) file.length()]; file.readFully(text); file.seek(0); file.writeBytes(javaHome); file.write(text); isUpdated = true; } catch (IOException ioException) { logger.error("IOException occurred while updating " + HYDROGRAPH_INI + " file", ioException); } finally { try { if (file != null) { file.close(); } } catch (IOException ioException) { logger.error("IOException occurred while updating " + HYDROGRAPH_INI + " file", ioException); } } return isUpdated; }
From source file:org.apache.hadoop.dfs.TestFsck.java
public void testCorruptBlock() throws Exception { Configuration conf = new Configuration(); conf.setLong("dfs.blockreport.intervalMsec", 1000); FileSystem fs = null;//from w w w .j a v a 2s .c o m DFSClient dfsClient = null; LocatedBlocks blocks = null; int replicaCount = 0; Random random = new Random(); String outStr = null; MiniDFSCluster cluster = new MiniDFSCluster(conf, 3, true, null); cluster.waitActive(); fs = cluster.getFileSystem(); Path file1 = new Path("/testCorruptBlock"); DFSTestUtil.createFile(fs, file1, 1024, (short) 3, 0); // Wait until file replication has completed DFSTestUtil.waitReplication(fs, file1, (short) 3); String block = DFSTestUtil.getFirstBlock(fs, file1).getBlockName(); // Make sure filesystem is in healthy state outStr = runFsck(conf, 0, true, "/"); System.out.println(outStr); assertTrue(outStr.contains("HEALTHY")); // corrupt replicas File baseDir = new File(System.getProperty("test.build.data"), "dfs/data"); for (int i = 0; i < 6; i++) { File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + block); if (blockFile.exists()) { RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw"); FileChannel channel = raFile.getChannel(); String badString = "BADBAD"; int rand = random.nextInt((int) channel.size() / 2); raFile.seek(rand); raFile.write(badString.getBytes()); raFile.close(); } } // Read the file to trigger reportBadBlocks try { IOUtils.copyBytes(fs.open(file1), new IOUtils.NullOutputStream(), conf, true); } catch (IOException ie) { // Ignore exception } dfsClient = new DFSClient(new InetSocketAddress("localhost", cluster.getNameNodePort()), conf); blocks = dfsClient.namenode.getBlockLocations(file1.toString(), 0, Long.MAX_VALUE); replicaCount = blocks.get(0).getLocations().length; while (replicaCount != 3) { try { Thread.sleep(100); } catch (InterruptedException ignore) { } blocks = dfsClient.namenode.getBlockLocations(file1.toString(), 0, Long.MAX_VALUE); replicaCount = blocks.get(0).getLocations().length; } assertTrue(blocks.get(0).isCorrupt()); // Check if fsck reports the same outStr = runFsck(conf, 1, true, "/"); System.out.println(outStr); assertTrue(outStr.contains("CORRUPT")); assertTrue(outStr.contains("testCorruptBlock")); cluster.shutdown(); }
From source file:org.kchine.r.server.manager.ServerManager.java
public static RServices createRSsh(boolean keepAlive, String codeServerHostIp, int codeServerPort, Properties namingInfo, int memoryMinMegabytes, int memoryMaxMegabytes, String sshHostIp, int sshPort, String sshLogin, String sshPwd, String name, boolean showProgress, URL[] codeUrls, String logFile) throws BadSshHostException, BadSshLoginPwdException, Exception { if (showProgress) { createRSshProgressArea = new JTextArea(); createRSshProgressBar = new JProgressBar(0, 100); createRSshProgressFrame = new JFrame("Create R Server via SSH"); Runnable runnable = new Runnable() { public void run() { createRSshProgressArea.setFocusable(false); createRSshProgressBar.setIndeterminate(true); JPanel p = new JPanel(new BorderLayout()); p.add(createRSshProgressBar, BorderLayout.SOUTH); p.add(new JScrollPane(createRSshProgressArea), BorderLayout.CENTER); createRSshProgressFrame.add(p); createRSshProgressFrame.pack(); createRSshProgressFrame.setSize(300, 90); createRSshProgressFrame.setVisible(true); createRSshProgressFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); PoolUtils.locateInScreenCenter(createRSshProgressFrame); }// w w w. j av a 2 s . c o m }; if (SwingUtilities.isEventDispatchThread()) runnable.run(); else { SwingUtilities.invokeLater(runnable); } } Connection conn = null; try { conn = new Connection(sshHostIp, sshPort); try { conn.connect(); } catch (Exception e) { throw new BadSshHostException(); } boolean isAuthenticated = conn.authenticateWithPassword(sshLogin, sshPwd); if (isAuthenticated == false) throw new BadSshLoginPwdException(); InputStream is = ServerManager.class .getResourceAsStream("/org/kchine/r/server/manager/bootstrap/BootSsh.class"); byte[] buffer = new byte[is.available()]; try { for (int i = 0; i < buffer.length; ++i) { int b = is.read(); buffer[i] = (byte) b; } } catch (Exception e) { e.printStackTrace(); } String bootstrapDir = INSTALL_DIR + "classes/org/kchine/r/server/manager/bootstrap"; new File(bootstrapDir).mkdirs(); RandomAccessFile raf = new RandomAccessFile(bootstrapDir + "/BootSsh.class", "rw"); raf.setLength(0); raf.write(buffer); raf.close(); Session sess = null; try { sess = conn.openSession(); sess.execCommand("mkdir -p RWorkbench/classes/org/kchine/r/server/manager/bootstrap"); sess.waitForCondition(ChannelCondition.EXIT_STATUS, 0); } finally { try { if (sess != null) sess.close(); } catch (Exception e) { e.printStackTrace(); } } new SCPClient(conn).put(bootstrapDir + "/BootSsh.class", "RWorkbench/classes/org/kchine/r/server/manager/bootstrap"); try { sess = conn.openSession(); String command = "java -classpath RWorkbench/classes org.kchine.r.server.manager.bootstrap.BootSsh" + " " + new Boolean(keepAlive) + " " + codeServerHostIp + " " + codeServerPort + " " + BootSsh.propertiesToString(namingInfo) + " " + "NULL" + " " + memoryMinMegabytes + " " + memoryMaxMegabytes + " " + "System.out" + " " + ((name == null || name.trim().equals("")) ? BootSsh.NO_NAME : name); if (codeUrls != null && codeUrls.length > 0) { for (int i = 0; i < codeUrls.length; ++i) { command = command + " " + codeUrls[i]; } } System.out.println("createRSsh command:" + command); sess.execCommand(command); InputStream stdout = new StreamGobbler(sess.getStdout()); final BufferedReader brOut = new BufferedReader(new InputStreamReader(stdout)); InputStream stderr = new StreamGobbler(sess.getStderr()); final BufferedReader brErr = new BufferedReader(new InputStreamReader(stderr)); final StringBuffer sshOutput = new StringBuffer(); new Thread(new Runnable() { public void run() { try { while (true) { String line = brOut.readLine(); if (line == null) break; sshOutput.append(line + "\n"); System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } System.out.println("Out Log Thread Died"); } }).start(); new Thread(new Runnable() { public void run() { try { while (true) { String line = brErr.readLine(); if (line == null) break; System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } System.out.println("Err Log Thread Died"); } }).start(); sess.waitForCondition(ChannelCondition.EXIT_STATUS, 0); int eIndex = sshOutput.indexOf(BootSsh.STUB_END_MARKER); if (eIndex != -1) { int bIndex = sshOutput.indexOf(BootSsh.STUB_BEGIN_MARKER); String stub = sshOutput.substring(bIndex + BootSsh.STUB_BEGIN_MARKER.length(), eIndex); return (RServices) PoolUtils.hexToStub(stub, ServerManager.class.getClassLoader()); } else { return null; } } finally { try { if (sess != null) sess.close(); } catch (Exception e) { e.printStackTrace(); } } } finally { try { if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } if (showProgress) { createRSshProgressFrame.setVisible(false); } } }
From source file:org.ambiance.codec.YEncDecoder.java
/** * Decode a single file//from www.j av a 2s. c om */ public void decode(File input) throws DecoderException { try { YEncFile yencFile = new YEncFile(input); // Get the output file StringBuffer outputName = new StringBuffer(outputDirName); outputName.append(File.separator); outputName.append(yencFile.getHeader().getName()); RandomAccessFile output = new RandomAccessFile(outputName.toString(), "rw"); // Place the pointer to the begining of data to decode long pos = yencFile.getDataBegin(); yencFile.getInput().seek(pos); // Bufferise the file // TODO - A Amliorer ByteArrayOutputStream baos = new ByteArrayOutputStream(); while (pos < yencFile.getDataEnd()) { baos.write(yencFile.getInput().read()); pos++; } byte[] buff = decoder.decode(baos.toByteArray()); // Write and close output output.write(buff); output.close(); // Warn if CRC32 check is not OK CRC32 crc32 = new CRC32(); crc32.update(buff); if (!yencFile.getTrailer().getCrc32().equals(Long.toHexString(crc32.getValue()).toUpperCase())) throw new DecoderException("Error in CRC32 check."); } catch (YEncException ye) { throw new DecoderException("Input file is not a YEnc file or contains error : " + ye.getMessage()); } catch (FileNotFoundException fnfe) { throw new DecoderException("Enable to create output file : " + fnfe.getMessage()); } catch (IOException ioe) { throw new DecoderException("Enable to read input file : " + ioe.getMessage()); } }
From source file:org.apache.hadoop.hdfs.server.namenode.TestFsck.java
public void testCorruptBlock() throws Exception { Configuration conf = new Configuration(); conf.setLong("dfs.blockreport.intervalMsec", 1000); FileSystem fs = null;/*from ww w .j a v a 2s. com*/ DFSClient dfsClient = null; LocatedBlocks blocks = null; int replicaCount = 0; Random random = new Random(); String outStr = null; MiniDFSCluster cluster = null; try { cluster = new MiniDFSCluster(conf, 3, true, null); cluster.waitActive(); fs = cluster.getFileSystem(); Path file1 = new Path("/testCorruptBlock"); DFSTestUtil.createFile(fs, file1, 1024, (short) 3, 0); // Wait until file replication has completed DFSTestUtil.waitReplication(fs, file1, (short) 3); String block = DFSTestUtil.getFirstBlock(fs, file1).getBlockName(); // Make sure filesystem is in healthy state outStr = runFsck(conf, 0, true, "/"); System.out.println(outStr); assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS)); // corrupt replicas File baseDir = new File(System.getProperty("test.build.data", "build/test/data"), "dfs/data"); for (int i = 0; i < 6; i++) { File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + block); if (blockFile.exists()) { RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw"); FileChannel channel = raFile.getChannel(); String badString = "BADBAD"; int rand = random.nextInt((int) channel.size() / 2); raFile.seek(rand); raFile.write(badString.getBytes()); raFile.close(); } } // Read the file to trigger reportBadBlocks try { IOUtils.copyBytes(fs.open(file1), new IOUtils.NullOutputStream(), conf, true); } catch (IOException ie) { // Ignore exception } dfsClient = new DFSClient(new InetSocketAddress("localhost", cluster.getNameNodePort()), conf); blocks = dfsClient.namenode.getBlockLocations(file1.toString(), 0, Long.MAX_VALUE); replicaCount = blocks.get(0).getLocations().length; while (replicaCount != 3) { try { Thread.sleep(100); } catch (InterruptedException ignore) { } blocks = dfsClient.namenode.getBlockLocations(file1.toString(), 0, Long.MAX_VALUE); replicaCount = blocks.get(0).getLocations().length; } assertTrue(blocks.get(0).isCorrupt()); // Check if fsck reports the same outStr = runFsck(conf, 1, true, "/"); System.out.println(outStr); assertTrue(outStr.contains(NamenodeFsck.CORRUPT_STATUS)); assertTrue(outStr.contains("testCorruptBlock")); } finally { if (cluster != null) { cluster.shutdown(); } } }