List of usage examples for java.io RandomAccessFile read
public int read(byte b[]) throws IOException
From source file:com.liferay.lms.servlet.SCORMFileServerServlet.java
/** * Copy the given byte range of the given input to the given output. * @param input The input to copy the given range to the given output for. * @param output The output to copy the given range from the given input for. * @param start Start of the byte range. * @param length Length of the byte range. * @throws IOException If something fails at I/O level. *//*from ww w. j a v a 2 s.co m*/ private static void copy(RandomAccessFile input, OutputStream output, long start, long length) throws IOException { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int read; if (input.length() == length) { // Write full range. while ((read = input.read(buffer)) > 0) { output.write(buffer, 0, read); } } else { // Write partial range. input.seek(start); long toRead = length; while ((read = input.read(buffer)) > 0) { if ((toRead -= read) > 0) { output.write(buffer, 0, read); } else { output.write(buffer, 0, (int) toRead + read); break; } } } }
From source file:au.org.ala.layers.grid.GridCacheBuilder.java
static public float getNextValue(RandomAccessFile raf, Grid g) { float f = Float.NaN; try {//from ww w . j a v a 2 s .c o m int size; byte[] b; if (g.datatype.charAt(0) == 'B') {//"BYTE")) { f = raf.readByte(); } else if (g.datatype.charAt(0) == 'U') {//equalsIgnoreCase("UBYTE")) { f = raf.readByte(); if (f < 0) { f += 256; } } else if (g.datatype.charAt(0) == 'S') {//equalsIgnoreCase("SHORT")) { size = 2; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = (short) (((0xFF & b[1]) << 8) | (b[0] & 0xFF)); } else { f = (short) (((0xFF & b[0]) << 8) | (b[1] & 0xFF)); } } else if (g.datatype.charAt(0) == 'I') {//equalsIgnoreCase("INT")) { size = 4; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = ((0xFF & b[3]) << 24) | ((0xFF & b[2]) << 16) + ((0xFF & b[1]) << 8) + (b[0] & 0xFF); } else { f = ((0xFF & b[0]) << 24) | ((0xFF & b[1]) << 16) + ((0xFF & b[2]) << 8) + ((0xFF & b[3]) & 0xFF); } } else if (g.datatype.charAt(0) == 'L') {//equalsIgnoreCase("LONG")) { size = 8; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = ((long) (0xFF & b[7]) << 56) + ((long) (0xFF & b[6]) << 48) + ((long) (0xFF & b[5]) << 40) + ((long) (0xFF & b[4]) << 32) + ((long) (0xFF & b[3]) << 24) + ((long) (0xFF & b[2]) << 16) + ((long) (0xFF & b[1]) << 8) + (0xFF & b[0]); } else { f = ((long) (0xFF & b[0]) << 56) + ((long) (0xFF & b[1]) << 48) + ((long) (0xFF & b[2]) << 40) + ((long) (0xFF & b[3]) << 32) + ((long) (0xFF & b[4]) << 24) + ((long) (0xFF & b[5]) << 16) + ((long) (0xFF & b[6]) << 8) + (0xFF & b[7]); } } else if (g.datatype.charAt(0) == 'F') {//.equalsIgnoreCase("FLOAT")) { size = 4; b = new byte[size]; raf.read(b); ByteBuffer bb = ByteBuffer.wrap(b); if (g.byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } f = bb.getFloat(); } else if (g.datatype.charAt(0) == 'D') {//.equalsIgnoreCase("DOUBLE")) { size = 8; b = new byte[8]; raf.read(b); ByteBuffer bb = ByteBuffer.wrap(b); if (g.byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } f = (float) bb.getDouble(); } //replace not a number if ((float) f == (float) g.nodatavalue) { f = Float.NaN; } } catch (Exception e) { } return f; }
From source file:phex.util.FileUtils.java
/** * Splits the source file at the splitpoint into the destination file. * The result will be the source file containing data to the split point and * the destination file containing the data from the split point to the end * of the file.//from w ww. j a v a 2s . c o m * * @param source the source file to split. * @param destination the destination file to split into. * @param splitPoint the split point byte position inside the file. * When the file size is 10 and the splitPoint is 3 the source file will contain * the first 3 bytes while the destination file will contain the last 7 bytes. * @throws IOException in case of a IOException during split operation. */ public static void splitFile(File source, File destination, long splitPoint) throws IOException { // open files RandomAccessFile sourceFile = new RandomAccessFile(source, "rws"); try { FileOutputStream outStream = new FileOutputStream(destination); try { sourceFile.seek(splitPoint); byte[] buffer = new byte[(int) Math.min(BUFFER_LENGTH, source.length() + 1)]; int length; while (-1 != (length = sourceFile.read(buffer))) { outStream.write(buffer, 0, length); } sourceFile.setLength(splitPoint); } finally { IOUtil.closeQuietly(outStream); } } finally { IOUtil.closeQuietly(sourceFile); } }
From source file:org.red5.io.flv.FLVReader.java
public static long getDuration(File flvFile) { RandomAccessFile flv = null; try {//w w w . j a va2 s. c o m flv = new RandomAccessFile(flvFile, "r"); long flvLength = flv.length(); if (flvLength < 13) { return 0; } flv.seek(flvLength - 4); byte[] buf = new byte[4]; flv.read(buf); long lastTagSize = 0; for (int i = 0; i < 4; i++) { lastTagSize += (buf[i] & 0x0ff) << ((3 - i) * 8); } if (lastTagSize == 0) { return 0; } flv.seek(flvLength - lastTagSize); flv.read(buf); long duration = 0; for (int i = 0; i < 3; i++) { duration += (buf[i] & 0x0ff) << ((2 - i) * 8); } duration += (buf[3] & 0x0ff) << 24; // extension byte return duration; } catch (IOException e) { return 0; } finally { try { if (flv != null) { flv.close(); } } catch (IOException e) { } flv = null; } }
From source file:org.jevis.commons.JEVisFileImp.java
@Override public void loadFromFile(File file) throws IOException { RandomAccessFile f = new RandomAccessFile(file, "r"); byte[] bytes = new byte[(int) f.length()]; f.read(bytes); this.bytes = bytes; f.close();/*from w ww. java 2s .c o m*/ }
From source file:eu.trentorise.smartcampus.feedback.test.TestFeedbackManagers.java
private byte[] readTestFile() throws IOException { RandomAccessFile f = new RandomAccessFile("src/test/resources/android.jpg", "r"); byte[] b = new byte[(int) f.length()]; f.read(b); f.close();/* w w w. ja va 2 s . co m*/ return b; }
From source file:com.tcl.lzhang1.mymusic.MusicUtil.java
/** * get the music info//from w ww .ja v a 2 s . c o m * * @param musicFile * @return */ public static SongModel getMusicInfo(File musicFile) { SongModel model = new SongModel(); // retrun null if music file is null or is or directory if (musicFile == null || !musicFile.isFile()) { return null; } byte[] buf = new byte[128]; try { Log.d(LOG_TAG, "process music file{" + musicFile.getAbsolutePath() + "}"); // tag_v1 RandomAccessFile music = new RandomAccessFile(musicFile, "r"); music.seek(music.length() - 128); music.read(buf);// read tag to buffer // tag_v2 byte[] header = new byte[10]; music.seek(0); music.read(header, 0, 10); // if ("ID3".equalsIgnoreCase(new String(header, 0, 3))) { // int ID3V2_frame_size = (int) (header[6] & 0x7F) * 0x200000 // | (int) (header[7] & 0x7F) * 0x400 // | (int) (header[8] & 0x7F) * 0x80 // | (int) (header[9] & 0x7F); // byte[] FrameHeader = new byte[4]; // music.seek(ID3V2_frame_size + 10); // music.read(FrameHeader, 0, 4); // model = getTimeInfo(FrameHeader, ID3V2_frame_size, musicFile); // } else { // byte[] FrameHeader = new byte[4]; // music.read(FrameHeader, 0, 4); // model = getTimeInfo(FrameHeader, 0, musicFile); // } music.close();// close file // check length // if (buf.length != 128) { // throw new // ErrorMusicLength(String.format("error music info length, length is:%i", // buf.length)); // } // // if (!"TAG".equalsIgnoreCase(new String(buf, 0, 3))) { // throw new UnknownTagException("unknown tag exception"); // } String songName = null; // try { // songName = new String(buf, 3, 30, "gbk").trim(); // } catch (UnsupportedEncodingException e) { // // TODO: handle exception // e.printStackTrace(); // songName = new String(buf, 3, 30).trim(); // } String singerName = ""; // try { // singerName = new String(buf, 33, 30, "gbk").trim(); // } catch (UnsupportedEncodingException e) { // // TODO: handle exception // singerName = new String(buf, 33, 30).trim(); // } String ablum = ""; // try { // ablum = new String(buf, 63, 30, "gbk").trim(); // } catch (UnsupportedEncodingException e) { // // TODO: handle exception // ablum = new String(buf, 63, 30).trim(); // } String year = ""; // try { // year = new String(buf, 93, 4, "gbk").trim(); // } catch (UnsupportedEncodingException e) { // year = new String(buf, 93, 4).trim(); // // TODO: handle exception // } String reamrk = ""; ContentResolver contentResolver = sContext.getContentResolver(); Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, "_data=?", new String[] { musicFile.getAbsolutePath() }, null); cursor.moveToFirst(); if (cursor != null && cursor.getCount() != 0) { try { if (TextUtils.isEmpty(songName)) { songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE)); singerName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST)); ablum = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM)); year = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.YEAR)); } long secs = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)); model.setTime(secs); secs /= 1000; model.setHours((int) secs / 3600); model.setMinutes(((int) secs % 3600) / 60); model.setSeconds(((int) secs % 3600) % 60); cursor.close(); } catch (CursorIndexOutOfBoundsException e) { // TODO: handle exception if (null != cursor) { cursor.close(); cursor = null; } Log.d(LOG_TAG, "CursorIndexOutOfBoundsException:" + e.getMessage()); try { songName = new String(buf, 3, 30, "gbk").trim(); } catch (UnsupportedEncodingException e0) { // TODO: handle exception e.printStackTrace(); songName = new String(buf, 3, 30).trim(); } try { singerName = new String(buf, 33, 30, "gbk").trim(); } catch (UnsupportedEncodingException e1) { // TODO: handle exception singerName = new String(buf, 33, 30).trim(); } try { ablum = new String(buf, 63, 30, "gbk").trim(); } catch (UnsupportedEncodingException e2) { // TODO: handle exception ablum = new String(buf, 63, 30).trim(); } try { year = new String(buf, 93, 4, "gbk").trim(); } catch (UnsupportedEncodingException e3) { year = new String(buf, 93, 4).trim(); // TODO: handle exception } try { reamrk = new String(buf, 97, 28, "gbk").trim(); } catch (UnsupportedEncodingException e4) { // TODO: handle exception reamrk = new String(buf, 97, 28).trim(); } } } // // get the time len model.setSongName(songName); model.setSingerName(singerName); model.setAblumName(ablum); model.setFile(musicFile.getAbsolutePath()); model.setRemark(""); Log.d(LOG_TAG, String.format("scaned music file[%s],album name[%s],song name[%s],singer name[%s]", model.getFile(), model.getAblumName(), model.getSingerName(), model.getSingerName())); mSongs.add(model); return model; } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } return model; }
From source file:org.apache.jackrabbit.core.value.BLOBInTempFile.java
public int read(byte[] b, long position) throws IOException, RepositoryException { RandomAccessFile raf = new RandomAccessFile(file, "r"); try {/* w w w . j a va2s .c o m*/ raf.seek(position); return raf.read(b); } finally { raf.close(); } }
From source file:se.kth.ssvl.tslab.wsn.service.bpf.ActionReceiver.java
@Override public void bundleReceived(Bundle bundle) { Log.i(TAG, "Received bundle! Reading:"); switch (bundle.payload().location()) { case DISK:/*w w w .j av a 2 s. com*/ RandomAccessFile f = null; try { f = new RandomAccessFile(bundle.payload().file(), "r"); byte[] buffer = new byte[(int) f.length()]; f.read(buffer); Log.i(TAG, new String(buffer)); } catch (FileNotFoundException e) { Log.e(TAG, "Payload should be in file: " + bundle.payload().file().getAbsolutePath() + ". But did not exist!"); } catch (Exception e) { Log.e(TAG, e.getMessage()); } finally { try { f.close(); } catch (IOException e) { Log.e(TAG, e.getMessage()); } } break; case MEMORY: try { Log.i(TAG, new String(bundle.payload().memory_buf())); } catch (BundleLockNotHeldByCurrentThread e) { e.printStackTrace(); } break; default: Log.w(TAG, "The bundle was neither stored in disk nor memory"); } }
From source file:org.apache.sling.commons.log.logback.internal.Tailer.java
/** * Read new lines. Code below is taken from org.apache.commons.io.input.Tailer * * @throws java.io.IOException if an I/O error occurs. * @param startPos position in file from where to start reading *///www. j a v a 2 s .c o m private void readLines(RandomAccessFile file, long startPos) throws IOException { StringBuilder sb = new StringBuilder(); file.seek(startPos); int num; boolean seenCR = false; while (((num = file.read(buffer)) != -1)) { for (int i = 0; i < num; i++) { byte ch = buffer[i]; switch (ch) { case '\n': seenCR = false; // swallow CR before LF listener.handle(sb.toString()); sb.setLength(0); break; case '\r': if (seenCR) { sb.append('\r'); } seenCR = true; break; default: if (seenCR) { seenCR = false; // swallow final CR listener.handle(sb.toString()); sb.setLength(0); } sb.append((char) ch); // add character, not its ascii value } } } //Drain the left over part if (sb.length() != 0) { listener.handle(sb.toString()); } }