List of usage examples for java.io RandomAccessFile read
public int read(byte b[]) throws IOException
From source file:ome.services.blitz.impl.ExporterI.java
/** * Read size bytes, and transition to "waiting" If any exception is thrown, * the offset for the current file will not be updated. */// w w w . j a v a 2 s .c om private byte[] read(long pos, int size) throws ServerError { if (size > MAX_SIZE) { throw new ApiUsageException("Max read size is: " + MAX_SIZE); } byte[] buf = new byte[size]; RandomAccessFile ra = null; try { ra = new RandomAccessFile(file, "r"); long l = ra.length(); if (pos + size > l) { size = (int) (l - pos); } ra.seek(pos); int read = ra.read(buf); // Handle end of file if (read < 0) { buf = new byte[0]; } else if (read < size) { byte[] newBuf = new byte[read]; System.arraycopy(buf, 0, newBuf, 0, read); buf = newBuf; } } catch (IOException io) { throw new RuntimeException(io); } finally { if (ra != null) { try { ra.close(); } catch (IOException e) { log.warn("IOException on file close"); } } } return buf; }
From source file:org.caboclo.clients.GoogleDriveClient.java
@Override public void sendNextPart(MultiPartUpload mpu) { byte[] chunk = new byte[Constants.CHUNK_UPLOAD_SIZE]; try {//from w ww .ja v a2s .c o m RandomAccessFile raf = new RandomAccessFile(mpu.getFile(), "r"); raf.seek(mpu.getOffset()); int chunkLen = raf.read(chunk); if (chunkLen < 0) { mpu.setFinished(); raf.close(); return; } if (chunkLen < Constants.CHUNK_UPLOAD_SIZE) { chunk = Arrays.copyOfRange(chunk, 0, chunkLen); } String sessionURL = (String) mpu.getObject("location"); String mimeType = (String) mpu.getObject("mimeType"); long start = mpu.getOffset(); long end = start + chunkLen - 1; long total = mpu.getFile().length(); String contentRange = "bytes " + start + "-" + end + "/" + total; Client client = Client.create(); WebResource webResource = client.resource(sessionURL); ClientResponse response = webResource.header("Host", "www.googleapis.com") .header("Authorization", "Bearer " + token).header("Content-Length", "" + chunkLen) .header("Content-Type", mimeType).header("Content-Range", contentRange) .put(ClientResponse.class, chunk); mpu.incrOffset(chunkLen); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:app.utils.ACache.java
public byte[] getAsBinary(String key) { RandomAccessFile RAFile = null; boolean removeFile = false; try {//from w w w. j a v a 2 s .c o m File file = mCache.get(key); if (!file.exists()) return null; RAFile = new RandomAccessFile(file, "r"); byte[] byteArray = new byte[(int) RAFile.length()]; RAFile.read(byteArray); if (!Utils.isDue(byteArray)) { return Utils.clearDateInfo(byteArray); } else { removeFile = true; return null; } } catch (Exception e) { e.printStackTrace(); return null; } finally { if (RAFile != null) { try { RAFile.close(); } catch (IOException e) { e.printStackTrace(); } } if (removeFile) remove(key); } }
From source file:com.aol.advertising.qiao.injector.file.AbstractFileTailer.java
protected long checksum(RandomAccessFile raFile) throws IOException, InsufficientFileLengthException { long pos = raFile.getFilePointer(); try {//ww w.ja v a 2 s . c om byte[] buffer = new byte[checksumByteLength]; raFile.seek(0); int n = raFile.read(buffer); if (n < checksumByteLength) { String s; logger.warn(s = ("not enough data for checksum: current file size=" + n)); throw new InsufficientFileLengthException(s); } synchronized (_crc) { _crc.reset(); _crc.update(buffer); return _crc.getValue(); } } finally { raFile.seek(pos); } }
From source file:com.android.strictmodetest.StrictModeActivity.java
private void fileReadLoop() { RandomAccessFile raf = null; File filename = getFileStreamPath("test.dat"); try {//from w ww.j a v a 2 s . c o m long sumNanos = 0; byte[] buf = new byte[512]; //raf = new RandomAccessFile(filename, "rw"); //raf.write(buf); //raf.close(); //raf = null; // The data's almost certainly cached -- it's not clear what we're testing here raf = new RandomAccessFile(filename, "r"); raf.seek(0); raf.read(buf); } catch (IOException e) { Log.e(TAG, "File read failed", e); } finally { try { if (raf != null) raf.close(); } catch (IOException e) { } } }
From source file:com.devilyang.musicstation.cache.ACache.java
/** * ? byte ?/*from w ww.j a v a 2 s .c o m*/ * * @param key * @return byte ? */ public byte[] getAsBinary(String key) { RandomAccessFile RAFile = null; boolean removeFile = false; try { File file = mCacheManager.get(key); if (!file.exists()) return null; RAFile = new RandomAccessFile(file, "r"); byte[] byteArray = new byte[(int) RAFile.length()]; RAFile.read(byteArray); if (!Utils.isDue(byteArray)) { return Utils.clearDateInfo(byteArray); } else { removeFile = true; return null; } } catch (Exception e) { e.printStackTrace(); return null; } finally { if (RAFile != null) { try { RAFile.close(); } catch (IOException e) { e.printStackTrace(); } } if (removeFile) remove(key); } }
From source file:au.org.ala.layers.web.UserDataService.java
private void setImageBlank(HttpServletResponse response) { if (blankImageBytes == null && blankImageObject != null) { synchronized (blankImageObject) { if (blankImageBytes == null) { try { RandomAccessFile raf = new RandomAccessFile( UserDataService.class.getResource("/blank.png").getFile(), "r"); blankImageBytes = new byte[(int) raf.length()]; raf.read(blankImageBytes); raf.close();/* w w w.j a v a2s . c o m*/ } catch (IOException e) { logger.error("error reading default blank tile", e); } } } } if (blankImageObject != null) { response.setContentType("image/png"); try { ServletOutputStream outStream = response.getOutputStream(); outStream.write(blankImageBytes); outStream.flush(); outStream.close(); } catch (IOException e) { logger.error("error outputting blank tile", e); } } }
From source file:GridFDock.DataDistribute.java
public Status uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException { long step = localFile.length() / 100; long process = 0; long localreadbytes = 0L; boolean tmp2 = true; Status result = null;//w w w . ja v a2 s. c o m ftpClient.setFileType(FTP.BINARY_FILE_TYPE); while (tmp2) { RandomAccessFile raf = new RandomAccessFile(localFile, "r"); OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes(CODING_1), CODING_2)); if (remoteSize > 0) { ftpClient.setRestartOffset(remoteSize); process = remoteSize / step; raf.seek(remoteSize); localreadbytes = remoteSize; } byte[] bytes = new byte[1024]; int c; while ((c = raf.read(bytes)) != -1) { out.write(bytes, 0, c); localreadbytes += c; if (localreadbytes / step != process) { process = localreadbytes / step; // System.out.println("Upload Progress" + process); } } out.flush(); raf.close(); out.close(); boolean judge = ftpClient.completePendingCommand(); if (judge) { result = Status.Upload_From_Break_Success; tmp2 = false; } else { result = Status.Upload_New_File_Failed; } } return result; }
From source file:org.commoncrawl.service.crawler.CrawlLog.java
/** * seek out next instance of sync bytes in the file input stream * /*w w w . ja v a 2s. c o m*/ * @param file * @throws IOException */ private static boolean seekToNextSyncBytesPos(byte[] syncBytesBuffer, RandomAccessFile file, long maxFileSize) throws IOException { while (file.getFilePointer() < maxFileSize) { try { // read in a sync.length buffer amount file.read(syncBytesBuffer); int syncLen = SYNC_BYTES_SIZE; // start scan for next sync position ... for (int i = 0; file.getFilePointer() < maxFileSize; i++) { int j = 0; for (; j < syncLen; j++) { if (_sync[j] != syncBytesBuffer[(i + j) % syncLen]) break; } if (j == syncLen) { // found matching sync bytes - reset file pos to before sync bytes file.seek(file.getFilePointer() - SYNC_BYTES_SIZE); // position // before // sync return true; } syncBytesBuffer[i % syncLen] = file.readByte(); } } catch (IOException e) { LOG.warn("IOException at:" + file.getFilePointer() + " Exception:" + CCStringUtils.stringifyException(e)); LOG.warn("Skipping to:" + file.getFilePointer() + 4096); file.seek(file.getFilePointer() + 4096); } } return false; }
From source file:caesar.feng.framework.utils.ACache.java
/** * ? byte ?/* w w w.j a v a 2 s . c om*/ * * @param key * @return byte ? */ public byte[] getAsBinary(String key) { RandomAccessFile RAFile = null; boolean removeFile = false; try { File file = mCache.get(key); if (!file.exists()) return null; RAFile = new RandomAccessFile(file, "r"); byte[] byteArray = new byte[(int) RAFile.length()]; RAFile.read(byteArray); if (!Utils.isDue(byteArray)) { return Utils.clearDateInfo(byteArray); } else { removeFile = true; return null; } } catch (Exception e) { e.printStackTrace(); return null; } finally { if (RAFile != null) { try { RAFile.close(); } catch (IOException e) { e.printStackTrace(); } } if (removeFile) remove(key); } }