Java Code Examples for java.io.RandomAccessFile.readFully()

The following are Jave code examples for showing how to use readFully() of the java.io.RandomAccessFile class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: openaudible   File: RealChunk.java   Source Code and License Vote up 6 votes
public static RealChunk readChunk(RandomAccessFile raf)
        throws CannotReadException, IOException {
    final String id = Utils.readString(raf, 4);
    final int size = Utils.readUint32AsInt(raf);
    if (size < 8) {
        throw new CannotReadException(
                "Corrupt file: RealAudio chunk length at position "
                        + (raf.getFilePointer() - 4)
                        + " cannot be less than 8");
    }
    if (size > (raf.length() - raf.getFilePointer() + 8)) {
        throw new CannotReadException(
                "Corrupt file: RealAudio chunk length of " + size
                        + " at position " + (raf.getFilePointer() - 4)
                        + " extends beyond the end of the file");
    }
    final byte[] bytes = new byte[size - 8];
    raf.readFully(bytes);
    return new RealChunk(id, size, bytes);
}
 
Example 2
Project: airgram   File: AudioInfo.java   Source Code and License Vote up 6 votes
public static AudioInfo getAudioInfo(File file) {
    try {
        byte header[] = new byte[12];
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        randomAccessFile.readFully(header, 0, 8);
        randomAccessFile.close();
        InputStream input = new BufferedInputStream(new FileInputStream(file));
        if (header[4] == 'f' && header[5] == 't' && header[6] == 'y' && header[7] == 'p') {
            return new M4AInfo(input);
        } else {
            return new MP3Info(input, file.length());
        }
    } catch (Exception e) {
        return null;
    }
}
 
Example 3
Project: MetadataEditor   File: RealChunk.java   Source Code and License Vote up 6 votes
public static RealChunk readChunk(RandomAccessFile raf)
		throws CannotReadException, IOException {
	final String id = Utils.readString(raf, 4);
	final int size = (int)Utils.readUint32(raf);
	if (size < 8) {
		throw new CannotReadException(
				"Corrupt file: RealAudio chunk length at position "
						+ (raf.getFilePointer() - 4)
						+ " cannot be less than 8");
	}
	if (size > (raf.length() - raf.getFilePointer() + 8)) {
		throw new CannotReadException(
				"Corrupt file: RealAudio chunk length of " + size
						+ " at position " + (raf.getFilePointer() - 4)
						+ " extends beyond the end of the file");
	}
	final byte[] bytes = new byte[size - 8];
	raf.readFully(bytes);
	return new RealChunk(id, size, bytes);
}
 
Example 4
Project: oscm   File: HeadersTest.java   Source Code and License Vote up 6 votes
public static void checkFile(String filePath, String header) {
    try {
        byte[] fileContent;
        RandomAccessFile randomFile = new RandomAccessFile(filePath, "rw");
        fileContent = new byte[(int) randomFile.length()];
        randomFile.readFully(fileContent);
        randomFile.close();
        String text = new String(fileContent);
        if (text.indexOf("Copyright IBM Corp") != -1) {
            return;
        }
        if (text.indexOf(header) == -1) {
            failed = true;
            failedFilesCollection.add(filePath);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 5
Project: EasyController   File: FileLFUCache.java   Source Code and License Vote up 6 votes
protected static byte[] readBytes(File file, int fixedLength) throws IOException {
	if (!file.exists()) {
		throw new FileNotFoundException("Not found: " + file);
	}
	if (!file.isFile()) {
		throw new IOException("Not a file: " + file);
	}
	long len = file.length();
	if (len >= Integer.MAX_VALUE) {
		throw new IOException("File is larger then max array size");
	}

	if (fixedLength > -1 && fixedLength < len) {
		len = fixedLength;
	}

	byte[] bytes = new byte[(int) len];
	RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
	randomAccessFile.readFully(bytes);
	randomAccessFile.close();

	return bytes;
}
 
Example 6
Project: hadoop   File: FSImageUtil.java   Source Code and License Vote up 5 votes
public static boolean checkFileFormat(RandomAccessFile file)
    throws IOException {
  if (file.length() < Loader.MINIMUM_FILE_LENGTH)
    return false;

  byte[] magic = new byte[MAGIC_HEADER.length];
  file.readFully(magic);
  if (!Arrays.equals(MAGIC_HEADER, magic))
    return false;

  return true;
}
 
Example 7
Project: incubator-netbeans   File: ClassBytesLoader.java   Source Code and License Vote up 5 votes
private static byte[] readFile(final URL classUrl)
                        throws IOException, URISyntaxException {
    URI uri = new URI(classUrl.toString());
    File file = new File(uri);
    RandomAccessFile f = new RandomAccessFile(file, "r");   // NOI18N
    byte[] buf = new byte[(int) f.length()];

    f.readFully(buf);
    //System.err.println("Size "+buf.length);
    f.close();

    return buf;
}
 
Example 8
Project: NBANDROID-V2   File: ApkUtils.java   Source Code and License Vote up 5 votes
public static int fastSignInfo(File file) {
    int v1 = SIGNED_NOT;
    int v2 = SIGNED_NOT;
    try {
        ZipFile zip = new ZipFile(file);
        if (zip.getEntry("META-INF/CERT.RSA") != null) {
            v1 = SIGNED_V1;
        }
        Field archiveField = ZipFile.class.getDeclaredField("archive");
        archiveField.setAccessible(true);
        final RandomAccessFile archive = (RandomAccessFile) archiveField.get(zip);
        Method positionAtCentralDirectory = ZipFile.class.getDeclaredMethod("positionAtCentralDirectory");
        positionAtCentralDirectory.setAccessible(true);
        positionAtCentralDirectory.invoke(zip);
        long centralDirectoryOffset = archive.getFilePointer();
        archive.seek(centralDirectoryOffset - 24);
        byte[] buffer = new byte[24];
        archive.readFully(buffer);
        zip.close();
        ByteBuffer footer = ByteBuffer.wrap(buffer);
        footer.order(ByteOrder.LITTLE_ENDIAN);
        if ((footer.getLong(8) == APK_SIG_BLOCK_MAGIC_LO)
                && (footer.getLong(16) == APK_SIG_BLOCK_MAGIC_HI)) {
            v2 = SIGNED_V2;
        }

    } catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
    }
    return v1 | v2;
}
 
Example 9
Project: FlickLauncher   File: FirstRun.java   Source Code and License Vote up 5 votes
/**
 * Read state from saved file
 * @param installation File
 * @return String
 * @throws IOException
 */
private static String readInstallationFile(File installation) throws IOException {
    RandomAccessFile f = new RandomAccessFile(installation, "r");// read only mode
    byte[] bytes = new byte[(int) f.length()];
    f.readFully(bytes);
    f.close();

    return new String(bytes);
}
 
Example 10
Project: boohee_v5.6   File: TinkerZipFile.java   Source Code and License Vote up 5 votes
private static EocdRecord parseEocdRecord(RandomAccessFile raf, long offset, boolean isZip64)
        throws IOException {
    long numEntries;
    long centralDirOffset;
    raf.seek(offset);
    byte[] eocd = new byte[18];
    raf.readFully(eocd);
    BufferIterator it = HeapBufferIterator.iterator(eocd, 0, eocd.length, ByteOrder
            .LITTLE_ENDIAN);
    if (isZip64) {
        numEntries = -1;
        centralDirOffset = -1;
        it.skip(16);
    } else {
        int diskNumber = it.readShort() & 65535;
        int diskWithCentralDir = it.readShort() & 65535;
        numEntries = (long) (it.readShort() & 65535);
        int totalNumEntries = it.readShort() & 65535;
        it.skip(4);
        centralDirOffset = ((long) it.readInt()) & 4294967295L;
        if (!(numEntries == ((long) totalNumEntries) && diskNumber == 0 && diskWithCentralDir
                == 0)) {
            throw new ZipException("Spanned archives not supported");
        }
    }
    return new EocdRecord(numEntries, centralDirOffset, it.readShort() & 65535);
}
 
Example 11
Project: text_converter   File: Installation.java   Source Code and License Vote up 5 votes
private static String readInstallationFile(File installation) throws IOException {
    RandomAccessFile f = new RandomAccessFile(installation, "r");
    byte[] bytes = new byte[(int) f.length()];
    f.readFully(bytes);
    f.close();
    return new String(bytes);
}
 
Example 12
Project: hadoop   File: VersionInfoMojo.java   Source Code and License Vote up 5 votes
/**
 * Reads and returns the full contents of the specified file.
 * 
 * @param file File to read
 * @return byte[] containing full contents of file
 * @throws IOException if there is an I/O error while reading the file
 */
private byte[] readFile(File file) throws IOException {
  RandomAccessFile raf = new RandomAccessFile(file, "r");
  byte[] buffer = new byte[(int) raf.length()];
  raf.readFully(buffer);
  raf.close();
  return buffer;
}
 
Example 13
Project: letv   File: ApkExternalInfoTool.java   Source Code and License Vote up 5 votes
private static byte[] a(RandomAccessFile randomAccessFile) throws IOException {
    int i = 1;
    long length = randomAccessFile.length() - 22;
    randomAccessFile.seek(length);
    byte[] bytes = a.getBytes();
    byte read = randomAccessFile.read();
    while (read != (byte) -1) {
        if (read == bytes[0] && randomAccessFile.read() == bytes[1] && randomAccessFile.read() == bytes[2] && randomAccessFile.read() == bytes[3]) {
            break;
        }
        length--;
        randomAccessFile.seek(length);
        read = randomAccessFile.read();
    }
    i = 0;
    if (i == 0) {
        throw new ZipException("archive is not a ZIP archive");
    }
    randomAccessFile.seek((16 + length) + 4);
    byte[] bArr = new byte[2];
    randomAccessFile.readFully(bArr);
    i = new ZipShort(bArr).getValue();
    if (i == 0) {
        return null;
    }
    bArr = new byte[i];
    randomAccessFile.read(bArr);
    return bArr;
}
 
Example 14
Project: javaide   File: Installation.java   Source Code and License Vote up 5 votes
private static String readInstallationFile(File installation) throws IOException {
    RandomAccessFile f = new RandomAccessFile(installation, "r");
    byte[] bytes = new byte[(int) f.length()];
    f.readFully(bytes);
    f.close();
    return new String(bytes);
}
 
Example 15
Project: vlc-example-streamplayer   File: VLCUtil.java   Source Code and License Vote up 4 votes
private static boolean readArmAttributes(RandomAccessFile in, ElfData elf) throws IOException {
    byte[] bytes = new byte[elf.sh_size];
    in.seek(elf.sh_offset);
    in.readFully(bytes);

    // wrap bytes in a ByteBuffer to force endianess
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(elf.order);

    //http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044e/IHI0044E_aaelf.pdf
    //http://infocenter.arm.com/help/topic/com.arm.doc.ihi0045d/IHI0045D_ABI_addenda.pdf
    if (buffer.get() != 'A') // format-version
        return false;

    // sub-sections loop
    while (buffer.remaining() > 0) {
        int start_section = buffer.position();
        int length = buffer.getInt();
        String vendor = getString(buffer);
        if (vendor.equals("aeabi")) {
            // tags loop
            while (buffer.position() < start_section + length) {
                int start = buffer.position();
                int tag = buffer.get();
                int size = buffer.getInt();
                // skip if not Tag_File, we don't care about others
                if (tag != 1) {
                    buffer.position(start + size);
                    continue;
                }

                // attributes loop
                while (buffer.position() < start + size) {
                    tag = getUleb128(buffer);
                    if (tag == 6) { // CPU_arch
                        int arch = getUleb128(buffer);
                        elf.att_arch = CPU_archs[arch];
                    } else if (tag == 27) { // ABI_HardFP_use
                        getUleb128(buffer);
                        elf.att_fpu = true;
                    } else {
                        // string for 4=CPU_raw_name / 5=CPU_name / 32=compatibility
                        // string for >32 && odd tags
                        // uleb128 for other
                        tag %= 128;
                        if (tag == 4 || tag == 5 || tag == 32 || (tag > 32 && (tag & 1) != 0))
                            getString(buffer);
                        else
                            getUleb128(buffer);
                    }
                }
            }
            break;
        }
    }
    return true;
}
 
Example 16
Project: PlusGram   File: SendMessagesHelper.java   Source Code and License Vote up 4 votes
public void sendSticker(TLRPC.Document document, long peer, MessageObject replyingMessageObject, boolean asAdmin) {
    if (document == null) {
        return;
    }
    if ((int) peer == 0) {
        int high_id = (int) (peer >> 32);
        TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance().getEncryptedChat(high_id);
        if (encryptedChat == null) {
            return;
        }
        if (document.thumb instanceof TLRPC.TL_photoSize) {
            File file = FileLoader.getPathToAttach(document.thumb, true);
            if (file.exists()) {
                try {
                    int len = (int) file.length();
                    byte[] arr = new byte[(int) file.length()];
                    RandomAccessFile reader = new RandomAccessFile(file, "r");
                    reader.readFully(arr);
                    TLRPC.TL_document newDocument = new TLRPC.TL_document();
                    newDocument.thumb = new TLRPC.TL_photoCachedSize();
                    newDocument.thumb.location = document.thumb.location;
                    newDocument.thumb.size = document.thumb.size;
                    newDocument.thumb.w = document.thumb.w;
                    newDocument.thumb.h = document.thumb.h;
                    newDocument.thumb.type = document.thumb.type;
                    newDocument.thumb.bytes = arr;

                    newDocument.id = document.id;
                    newDocument.access_hash = document.access_hash;
                    newDocument.date = document.date;
                    newDocument.mime_type = document.mime_type;
                    newDocument.size = document.size;
                    newDocument.dc_id = document.dc_id;
                    newDocument.attributes = document.attributes;
                    if (newDocument.mime_type == null) {
                        newDocument.mime_type = "";
                    }
                    document = newDocument;
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
            }
        }
    }
    SendMessagesHelper.getInstance().sendMessage((TLRPC.TL_document) document, null, null, peer, replyingMessageObject, asAdmin, null, null);
}
 
Example 17
Project: airgram   File: SendMessagesHelper.java   Source Code and License Vote up 4 votes
public void sendSticker(TLRPC.Document document, long peer, MessageObject replyingMessageObject) {
    if (document == null) {
        return;
    }
    if ((int) peer == 0) {
        int high_id = (int) (peer >> 32);
        TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance().getEncryptedChat(high_id);
        if (encryptedChat == null) {
            return;
        }
        if (document.thumb instanceof TLRPC.TL_photoSize) {
            File file = FileLoader.getPathToAttach(document.thumb, true);
            if (file.exists()) {
                try {
                    int len = (int) file.length();
                    byte[] arr = new byte[(int) file.length()];
                    RandomAccessFile reader = new RandomAccessFile(file, "r");
                    reader.readFully(arr);
                    TLRPC.TL_document newDocument = new TLRPC.TL_document();
                    newDocument.thumb = new TLRPC.TL_photoCachedSize();
                    newDocument.thumb.location = document.thumb.location;
                    newDocument.thumb.size = document.thumb.size;
                    newDocument.thumb.w = document.thumb.w;
                    newDocument.thumb.h = document.thumb.h;
                    newDocument.thumb.type = document.thumb.type;
                    newDocument.thumb.bytes = arr;

                    newDocument.id = document.id;
                    newDocument.access_hash = document.access_hash;
                    newDocument.date = document.date;
                    newDocument.mime_type = document.mime_type;
                    newDocument.size = document.size;
                    newDocument.dc_id = document.dc_id;
                    newDocument.attributes = document.attributes;
                    if (newDocument.mime_type == null) {
                        newDocument.mime_type = "";
                    }
                    document = newDocument;
                } catch (Exception e) {
                    FileLog.e("tmessages", e);
                }
            }
        }
    }
    SendMessagesHelper.getInstance().sendMessage((TLRPC.TL_document) document, null, null, peer, replyingMessageObject, null, null);
}
 
Example 18
Project: AppServiceRestFul   File: Util.java   Source Code and License Vote up 4 votes
public static byte[] readFromFile(String fileName, int offset, int len) {
	if (fileName == null) {
		return null;
	}

	File file = new File(fileName);
	if (!file.exists()) {
		System.out.println("readFromFile: file not found");
		return null;
	}

	if (len == -1) {
		len = (int) file.length();
	}

	System.out.println("readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));

	if(offset <0){
		System.out.println("readFromFile invalid offset:" + offset);
		return null;
	}
	if(len <=0 ){
		System.out.println("readFromFile invalid len:" + len);
		return null;
	}
	if(offset + len > (int) file.length()){
		System.out.println("readFromFile invalid file len:" + file.length());
		return null;
	}

	byte[] b = null;
	try {
		RandomAccessFile in = new RandomAccessFile(fileName, "r");
		b = new byte[len]; // 创建合适文件大小的数组
		in.seek(offset);
		in.readFully(b);
		in.close();

	} catch (Exception e) {
		System.out.println("readFromFile : errMsg = " + e.getMessage());
		e.printStackTrace();
	}
	return b;
}
 
Example 19
Project: AppServiceRestFul   File: Util.java   Source Code and License Vote up 4 votes
public static byte[] readFromFile(String fileName, int offset, int len) {
	if (fileName == null) {
		return null;
	}

	File file = new File(fileName);
	if (!file.exists()) {
		System.out.println("readFromFile: file not found");
		return null;
	}

	if (len == -1) {
		len = (int) file.length();
	}

	System.out.println("readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));

	if(offset <0){
		System.out.println("readFromFile invalid offset:" + offset);
		return null;
	}
	if(len <=0 ){
		System.out.println("readFromFile invalid len:" + len);
		return null;
	}
	if(offset + len > (int) file.length()){
		System.out.println("readFromFile invalid file len:" + file.length());
		return null;
	}

	byte[] b = null;
	try {
		RandomAccessFile in = new RandomAccessFile(fileName, "r");
		b = new byte[len]; // 创建合适文件大小的数组
		in.seek(offset);
		in.readFully(b);
		in.close();

	} catch (Exception e) {
		System.out.println("readFromFile : errMsg = " + e.getMessage());
		e.printStackTrace();
	}
	return b;
}
 
Example 20
Project: AnimationsDemo   File: MCPTool.java   Source Code and License Vote up 2 votes
/**
 * RandomAccessFile seek and readFully
 * @param raf
 * @param index
 * @param buffer
 * @throws IOException
 */
private static void readFully(RandomAccessFile raf, long index, byte[] buffer) throws IOException {
	raf.seek(index);
	raf.readFully(buffer);
}