List of usage examples for java.io RandomAccessFile write
public void write(byte b[]) throws IOException
From source file:org.wso2.msf4j.HttpServerTest.java
protected void testStreamUpload(int size, String filename) throws IOException { //create a random file to be uploaded. File fname = new File(tmpFolder, filename); fname.createNewFile();//from w ww. java2 s. c o m RandomAccessFile randf = new RandomAccessFile(fname, "rw"); String contentStr = IntStream.range(0, size).mapToObj(value -> String.valueOf((int) (Math.random() * 1000))) .collect(Collectors.joining("")); randf.write(contentStr.getBytes(Charsets.UTF_8)); randf.close(); //test stream upload HttpURLConnection urlConn = request("/test/v1/stream/upload", HttpMethod.PUT); Files.copy(Paths.get(fname.toURI()), urlConn.getOutputStream()); assertEquals(200, urlConn.getResponseCode()); String contentFromServer = getContent(urlConn); assertEquals(contentStr, contentFromServer); urlConn.disconnect(); fname.delete(); }
From source file:com.lewa.crazychapter11.MainActivity.java
private void WriteToSdCard(String content) { try {/*ww w . j ava2 s. c o m*/ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File sdCardDir = Environment.getExternalStorageDirectory(); File targetFile = new File(sdCardDir.getCanonicalPath() + FILE_NAME_SDCARD); Log.i("crazyFile", "WriteToSdCard: sdCardDir=" + sdCardDir + " \n targetFile=" + targetFile); RandomAccessFile raf = new RandomAccessFile(targetFile, "rw"); raf.seek(targetFile.length()); raf.write(content.getBytes()); raf.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.kuali.student.git.model.SvnRevisionMapper.java
private long createRevisionEntry(RandomAccessFile dataFile, long endOfDataFileOffset, long revision, List<String> revisionLines) throws IOException { OutputStream revisionMappingStream = null; ByteArrayOutputStream bytesOut; revisionMappingStream = new BZip2CompressorOutputStream(bytesOut = new ByteArrayOutputStream()); PrintWriter pw = new PrintWriter(revisionMappingStream); IOUtils.writeLines(revisionLines, "\n", pw); pw.flush();/* www . j a v a 2 s.c o m*/ pw.close(); byte[] data = bytesOut.toByteArray(); dataFile.seek(endOfDataFileOffset); dataFile.write(data); return data.length; }
From source file:juicebox.tools.utils.original.Preprocessor.java
private void updateMasterIndex() throws IOException { RandomAccessFile raf = null; try {//from w w w .jav a 2 s .c o m raf = new RandomAccessFile(outputFile, "rw"); // Master index raf.getChannel().position(masterIndexPositionPosition); BufferedByteWriter buffer = new BufferedByteWriter(); buffer.putLong(masterIndexPosition); raf.write(buffer.getBytes()); } finally { if (raf != null) raf.close(); } }
From source file:com.mellanox.r4h.MiniDFSCluster.java
public static boolean corruptBlock(File blockFile) throws IOException { if (blockFile == null || !blockFile.exists()) { return false; }/*from w w w . j a va 2s . c o m*/ // Corrupt replica by writing random bytes into replica Random random = new Random(); 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(); LOG.warn("Corrupting the block " + blockFile); return true; }
From source file:big.BigZip.java
/** * Given a position inside our knowledge base, retrieve the data up to * the next file indicator./* w w w . j a v a 2 s . c om*/ * @param targetFile The new file that will be created * @param startPosition The position from where we start to read the data * @param endPosition * @return */ public boolean extractBytes(final File targetFile, final long startPosition, final Long endPosition) { /** * This is a tricky method. We will be extracting data from a the BIG * archive onto a new file somewhere on disk. The biggest challenge here * is to find exactly when the data for the file ends and still do the * file copy with a wonderful performance. */ try { // enable random access to the BIG file (fast as heck) RandomAccessFile dataBIG = new RandomAccessFile(fileMainBIG, "r"); // if the target file exists, try to delete it if (targetFile.exists()) { targetFile.delete(); if (targetFile.exists()) { // we failed completely System.out.println("BIG405 - Failed to delete: " + targetFile.getAbsolutePath()); return false; } } // we need to create a temporary zip file holder File fileZip = new File("temp.zip"); // delete the zip file if it already exists if (fileZip.exists()) { fileZip.delete(); if (fileZip.exists()) { // we failed completely System.out.println("BIG416 - Failed to delete: " + fileZip.getAbsolutePath()); return false; } } // create a new file RandomAccessFile dataNew = new RandomAccessFile(fileZip, "rw"); // jump directly to the position where the file is positioned dataBIG.seek(startPosition); // now we start reading bytes during the mentioned interval while (dataBIG.getFilePointer() < endPosition) { // read a byte from our BIG archive int data = dataBIG.read(); // write the same byte on the target file dataNew.write(data); } // close the file streams dataBIG.close(); dataNew.close(); // extract the file zip.extract(fileZip, new File(".")); // delete the temp zip file fileZip.delete(); } catch (FileNotFoundException ex) { Logger.getLogger(BigZip.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (IOException ex) { Logger.getLogger(BigZip.class.getName()).log(Level.SEVERE, null, ex); return false; } return true; }
From source file:au.org.ala.layers.intersect.Grid.java
/** * for DomainGenerator/*ww w. jav a 2 s .co m*/ * <p/> * writes out a list of double (same as getGrid() returns) to a file * <p/> * byteorderlsb * data type, FLOAT * * @param newfilename * @param dfiltered */ public void writeGrid(String newfilename, int[] dfiltered, double xmin, double ymin, double xmax, double ymax, double xres, double yres, int nrows, int ncols) { int size, i, length = dfiltered.length; double maxvalue = Integer.MAX_VALUE * -1; double minvalue = Integer.MAX_VALUE; //write data as whole file RandomAccessFile afile = null; try { //read of random access file can throw an exception afile = new RandomAccessFile(newfilename + ".gri", "rw"); size = 4; byte[] b = new byte[size * length]; ByteBuffer bb = ByteBuffer.wrap(b); if (byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } else { bb.order(ByteOrder.BIG_ENDIAN); } for (i = 0; i < length; i++) { bb.putInt(dfiltered[i]); } afile.write(b); } catch (Exception e) { logger.error("error writing grid file", e); } finally { if (afile != null) { try { afile.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } writeHeader(newfilename, xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols, minvalue, maxvalue, "INT4BYTES", "-9999"); }
From source file:org.apache.hadoop.hive.service.HSSessionItem.java
public boolean uploadProto(String user, String fileName, byte[] array) throws HiveServerException { boolean res = true; String fname;/* w w w . ja v a 2s . co m*/ String dname = getHome() + "/protobuf/upload/" + user; File d = new File(dname); if (!d.exists()) { if (!d.mkdirs()) { l4j.error(getSessionName() + " try to mkdir " + dname + " failed."); throw new HiveServerException("Create user proto directory failed."); } } if (!fileName.trim().toLowerCase().endsWith(".proto")) { throw new HiveServerException( "Upload proto command can only handle .proto file, Check your file suffix"); } fname = dname + "/" + fileName; RandomAccessFile raf; File f; try { f = new File(fname); if (!f.exists()) { if (!f.createNewFile()) { l4j.error("Try to create file " + fname + " failed."); throw new HiveServerException("Create user upload file " + fname + " failed."); } } } catch (java.io.IOException ex) { l4j.error(getSessionName() + " try to create file " + fname + " failed w/ " + ex); return false; } if (array.length == 0) { if (!f.delete()) { l4j.error("Try to delete file " + fname + " failed."); throw new HiveServerException("Delete user proto file " + 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(array); } 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.hadoop.hive.service.HSSessionItem.java
public boolean upload(String rtype, String user, String fileName, String data) throws HiveServerException { boolean res = true; String fname;/* w ww .j a v a 2 s .c om*/ if (rtype.equalsIgnoreCase("jar")) { fname = getHome() + "/auxlib/" + fileName; } else if (rtype.equalsIgnoreCase("proto")) { String dname = getHome() + "/protobuf/upload/" + user; File d = new File(dname); if (!d.exists()) { if (!d.mkdirs()) { l4j.error(getSessionName() + " try to mkdir " + dname + " failed."); throw new HiveServerException("Create user proto directory failed."); } } if (!fileName.trim().toLowerCase().endsWith(".proto")) { throw new HiveServerException( "Upload proto command can only handle .proto file, Check your file suffix"); } fname = dname + "/" + fileName; } else { String errorMsg = "Can't upload filetype: " + rtype; l4j.error(getSessionName() + " upload failed: " + errorMsg); throw new HiveServerException("errorMsg"); } RandomAccessFile raf; File f; try { f = new File(fname); if (!f.exists()) { if (!f.createNewFile()) { l4j.error("Try to create file " + fname + " failed."); throw new HiveServerException("Create user upload file " + fname + " failed."); } } } catch (java.io.IOException ex) { l4j.error(getSessionName() + " try to create file " + fname + " failed w/ " + ex); return false; } if (data.equalsIgnoreCase("")) { if (!f.delete()) { l4j.error("Try to delete file " + fname + " failed."); throw new HiveServerException("Delete user file " + 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(data.getBytes()); } catch (java.io.IOException ex) { l4j.error(getSessionName() + " try to truncate/write file " + fname + "failed w/ " + ex); return false; } return res; }