本文整理汇总了Java中java.io.FileInputStream.skip方法的典型用法代码示例。如果您正苦于以下问题:Java FileInputStream.skip方法的具体用法?Java FileInputStream.skip怎么用?Java FileInputStream.skip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.FileInputStream
的用法示例。
在下文中一共展示了FileInputStream.skip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: WriteFile
import java.io.FileInputStream; //导入方法依赖的package包/类
public void WriteFile(File outputFile, int startFrame, int numFrames)
throws java.io.IOException {
outputFile.createNewFile();
FileInputStream in = new FileInputStream(mInputFile);
FileOutputStream out = new FileOutputStream(outputFile);
int maxFrameLen = 0;
for (int i = 0; i < numFrames; i++) {
if (mFrameLens[startFrame + i] > maxFrameLen)
maxFrameLen = mFrameLens[startFrame + i];
}
byte[] buffer = new byte[maxFrameLen];
int pos = 0;
for (int i = 0; i < numFrames; i++) {
int skip = mFrameOffsets[startFrame + i] - pos;
int len = mFrameLens[startFrame + i];
if (skip > 0) {
in.skip(skip);
pos += skip;
}
in.read(buffer, 0, len);
out.write(buffer, 0, len);
pos += len;
}
in.close();
out.close();
}
示例2: skipBytes
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Skip toSkip number of bytes and return the remaining bytes of the file.
*/
private static long skipBytes(FileInputStream fis, int toSkip, long space)
throws IOException {
long skip = fis.skip(toSkip);
if (skip != toSkip) {
throw new RuntimeException("skip() returns " + skip
+ " but expected " + toSkip);
}
long newSpace = space - toSkip;
long remaining = newSpace > 0 ? newSpace : 0;
int avail = fis.available();
if (avail != remaining) {
throw new RuntimeException("available() returns " + avail
+ " but expected " + remaining);
}
System.out.println("Skipped " + skip + " bytes "
+ " available() returns " + avail);
return newSpace;
}
示例3: read
import java.io.FileInputStream; //导入方法依赖的package包/类
private static byte[] read(String inputFileName, int start, int end)
throws FileNotFoundException, IOException {
File theFile = new File(inputFileName);
FileInputStream input = new FileInputStream(theFile);
int skipped = 0;
while (skipped < start) {
skipped += input.skip(start - skipped);
}
int length = (int) (Math.min(end, theFile.length()) - start);
byte[] bytes = new byte[length];
int bytesRead = 0;
while (bytesRead < bytes.length) {
bytesRead = input.read(bytes, bytesRead, bytes.length - bytesRead);
if (bytesRead == -1) {
break;
}
}
return bytes;
}
示例4: InputStreamAdapter
import java.io.FileInputStream; //导入方法依赖的package包/类
InputStreamAdapter(final File file, final long pos,
final long length)
throws FileNotFoundException, IOException {
if (file == null) {
throw new NullPointerException("file");
}
if (pos < 0) {
throw new IllegalArgumentException("pos: " + pos);
}
if (length < 0) {
throw new IllegalArgumentException("length: " + length);
}
final FileInputStream fis = new FileInputStream(file);
if (pos > 0) {
final long actualPos = fis.skip(pos);
}
final BufferedInputStream bis = new BufferedInputStream(fis);
final CountdownInputStream cis = new CountdownInputStream(bis);
cis.setCount(length);
m_countdownInputStream = cis;
}
示例5: open
import java.io.FileInputStream; //导入方法依赖的package包/类
@Override
public long open(DataSpec dataSpec) throws ContentDataSourceException {
try {
uriString = dataSpec.uri.toString();
AssetFileDescriptor assetFd = resolver.openAssetFileDescriptor(dataSpec.uri, "r");
inputStream = new FileInputStream(assetFd.getFileDescriptor());
long skipped = inputStream.skip(dataSpec.position);
if (skipped < dataSpec.position) {
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
// skip beyond the end of the data.
throw new EOFException();
}
if (dataSpec.length != C.LENGTH_UNBOUNDED) {
bytesRemaining = dataSpec.length;
} else {
bytesRemaining = inputStream.available();
if (bytesRemaining == 0) {
// FileInputStream.available() returns 0 if the remaining length cannot be determined, or
// if it's greater than Integer.MAX_VALUE. We don't know the true length in either case,
// so treat as unbounded.
bytesRemaining = C.LENGTH_UNBOUNDED;
}
}
} catch (IOException e) {
throw new ContentDataSourceException(e);
}
opened = true;
if (listener != null) {
listener.onTransferStart();
}
return bytesRemaining;
}
示例6: WriteFile
import java.io.FileInputStream; //导入方法依赖的package包/类
public void WriteFile(File outputFile, int startFrame, int numFrames)
throws java.io.IOException {
outputFile.createNewFile();
FileInputStream in = new FileInputStream(mInputFile);
FileOutputStream out = new FileOutputStream(outputFile);
byte[] header = new byte[6];
header[0] = '#';
header[1] = '!';
header[2] = 'A';
header[3] = 'M';
header[4] = 'R';
header[5] = '\n';
out.write(header, 0, 6);
int maxFrameLen = 0;
for (int i = 0; i < numFrames; i++) {
if (mFrameLens[startFrame + i] > maxFrameLen)
maxFrameLen = mFrameLens[startFrame + i];
}
byte[] buffer = new byte[maxFrameLen];
int pos = 0;
for (int i = 0; i < numFrames; i++) {
int skip = mFrameOffsets[startFrame + i] - pos;
int len = mFrameLens[startFrame + i];
if (skip < 0) {
continue;
}
if (skip > 0) {
in.skip(skip);
pos += skip;
}
in.read(buffer, 0, len);
out.write(buffer, 0, len);
pos += len;
}
in.close();
out.close();
}
示例7: computeMd5OfFirst10Frames
import java.io.FileInputStream; //导入方法依赖的package包/类
public String computeMd5OfFirst10Frames()
throws java.io.FileNotFoundException,
java.io.IOException,
java.security.NoSuchAlgorithmException {
int[] frameOffsets = getFrameOffsets();
int[] frameLens = getFrameLens();
int numFrames = frameLens.length;
if (numFrames > 10) {
numFrames = 10;
}
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
FileInputStream in = new FileInputStream(mInputFile);
int pos = 0;
for (int i = 0; i < numFrames; i++) {
int skip = frameOffsets[i] - pos;
int len = frameLens[i];
if (skip > 0) {
in.skip(skip);
pos += skip;
}
byte[] buffer = new byte[len];
in.read(buffer, 0, len);
digest.update(buffer);
pos += len;
}
in.close();
byte[] hash = digest.digest();
return bytesToHex(hash);
}
示例8: verifyMac
import java.io.FileInputStream; //导入方法依赖的package包/类
private static void verifyMac(MasterSecret masterSecret, File file) throws IOException {
Mac mac = initializeMac(masterSecret.getMacKey());
FileInputStream macStream = new FileInputStream(file);
InputStream dataStream = new LimitedInputStream(new FileInputStream(file), file.length() - MAC_LENGTH);
byte[] theirMac = new byte[MAC_LENGTH];
if (macStream.skip(file.length() - MAC_LENGTH) != file.length() - MAC_LENGTH) {
throw new IOException("Unable to seek");
}
readFully(macStream, theirMac);
byte[] buffer = new byte[4096];
int read;
while ((read = dataStream.read(buffer)) != -1) {
mac.update(buffer, 0, read);
}
byte[] ourMac = mac.doFinal();
if (!MessageDigest.isEqual(ourMac, theirMac)) {
throw new IOException("Bad MAC");
}
macStream.close();
dataStream.close();
}
示例9: open
import java.io.FileInputStream; //导入方法依赖的package包/类
@Override
public long open(DataSpec dataSpec) throws ContentDataSourceException {
try {
uri = dataSpec.uri;
assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
long skipped = inputStream.skip(dataSpec.position);
if (skipped < dataSpec.position) {
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
// skip beyond the end of the data.
throw new EOFException();
}
if (dataSpec.length != C.LENGTH_UNSET) {
bytesRemaining = dataSpec.length;
} else {
bytesRemaining = inputStream.available();
if (bytesRemaining == 0) {
// FileInputStream.available() returns 0 if the remaining length cannot be determined, or
// if it's greater than Integer.MAX_VALUE. We don't know the true length in either case,
// so treat as unbounded.
bytesRemaining = C.LENGTH_UNSET;
}
}
} catch (IOException e) {
throw new ContentDataSourceException(e);
}
opened = true;
if (listener != null) {
listener.onTransferStart(this, dataSpec);
}
return bytesRemaining;
}
示例10: serveFile
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Serves file from homeDir and its' subdirectories (only). Uses only URI,
* ignores all headers and HTTP parameters.
*/
private Response serveFile(Map<String, String> header, File file, String mime) {
Response res;
try {
// Calculate etag
String etag = Integer
.toHexString((file.getAbsolutePath() + file.lastModified() + String.valueOf(file.length()))
.hashCode());
// Support (simple) skipping:
long startFrom = 0;
long endAt = -1;
String range = header.get("range");
if (range != null && range.startsWith("bytes=")) {
range = range.substring("bytes=".length());
int minus = range.indexOf('-');
try {
if (minus > 0) {
startFrom = Long.parseLong(range.substring(0, minus));
endAt = Long.parseLong(range.substring(minus + 1));
}
} catch (NumberFormatException ignored) {
}
}
// Change return code and add Content-Range header when skipping is
// requested
long fileLen = file.length();
if (range != null && startFrom >= 0) {
if (startFrom >= fileLen) {
res = createResponse(Response.Status.RANGE_NOT_SATISFIABLE,
NanoHTTPD.MIME_PLAINTEXT, "");
res.addHeader("Content-Range", "bytes 0-0/" + fileLen);
res.addHeader("ETag", etag);
} else {
if (endAt < 0) {
endAt = fileLen - 1;
}
long newLen = endAt - startFrom + 1;
if (newLen < 0) {
newLen = 0;
}
final long dataLen = newLen;
FileInputStream fis = new FileInputStream(file) {
@Override
public int available() throws IOException {
return (int) dataLen;
}
};
long skipped = fis.skip(startFrom);
if (skipped != startFrom) {
throw new IOException("unable to skip the required " + startFrom + " bytes.");
}
res = createResponse(Response.Status.PARTIAL_CONTENT, mime, fis);
res.addHeader("Content-Length", String.valueOf(dataLen));
res.addHeader("Content-Range", "bytes " + startFrom + "-" + endAt + "/"
+ fileLen);
res.addHeader("ETag", etag);
}
} else {
if (etag.equals(header.get("if-none-match"))) {
res = createResponse(Response.Status.NOT_MODIFIED, mime, "");
} else {
res = createResponse(Response.Status.OK, mime, new FileInputStream(file));
res.addHeader("Content-Length", String.valueOf(fileLen));
res.addHeader("ETag", etag);
}
}
} catch (IOException ioe) {
res = createResponse(Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT,
"FORBIDDEN: Reading file failed.");
}
return res;
}
示例11: serveFile
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Serves file from homeDir and its' subdirectories (only). Uses only URI,
* ignores all headers and HTTP parameters.
*/
Response serveFile(String uri, Map<String, String> header, File file, String mime) {
Response res;
try {
// Calculate etag
String etag = Integer
.toHexString((file.getAbsolutePath() + file.lastModified() + String.valueOf(file.length()))
.hashCode());
// Support (simple) skipping:
long startFrom = 0;
long endAt = -1;
String range = header.get("range");
if (range != null && range.startsWith("bytes=")) {
range = range.substring("bytes=".length());
int minus = range.indexOf('-');
try {
if (minus > 0) {
startFrom = Long.parseLong(range.substring(0, minus));
endAt = Long.parseLong(range.substring(minus + 1));
}
} catch (NumberFormatException ignored) {
}
}
// Change return code and add Content-Range header when skipping is
// requested
long fileLen = file.length();
if (range != null && startFrom >= 0) {
if (startFrom >= fileLen) {
res = createResponse(NanoHTTPD.Response.Status.RANGE_NOT_SATISFIABLE,
NanoHTTPD.MIME_PLAINTEXT, "");
res.addHeader("Content-Range", "bytes 0-0/" + fileLen);
res.addHeader("ETag", etag);
} else {
if (endAt < 0) {
endAt = fileLen - 1;
}
long newLen = endAt - startFrom + 1;
if (newLen < 0) {
newLen = 0;
}
final long dataLen = newLen;
FileInputStream fis = new FileInputStream(file) {
@Override
public int available() throws IOException {
return (int) dataLen;
}
};
long skipped = fis.skip(startFrom);
if (skipped != startFrom) {
throw new IOException("unable to skip the required " + startFrom + " bytes.");
}
res = createResponse(NanoHTTPD.Response.Status.PARTIAL_CONTENT, mime, fis);
res.addHeader("Content-Length", String.valueOf(dataLen));
res.addHeader("Content-Range", "bytes " + startFrom + "-" + endAt + "/"
+ fileLen);
res.addHeader("ETag", etag);
}
} else {
if (etag.equals(header.get("if-none-match"))) {
res = createResponse(NanoHTTPD.Response.Status.NOT_MODIFIED, mime, "");
} else {
res = createResponse(NanoHTTPD.Response.Status.OK, mime, new FileInputStream(file));
res.addHeader("Content-Length", String.valueOf(fileLen));
res.addHeader("ETag", etag);
}
}
} catch (IOException ioe) {
res = createResponse(NanoHTTPD.Response.Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT,
"FORBIDDEN: Reading file failed.");
}
return res;
}
示例12: ReadFile
import java.io.FileInputStream; //导入方法依赖的package包/类
public void ReadFile(File inputFile)
throws java.io.FileNotFoundException,
java.io.IOException {
super.ReadFile(inputFile);
mNumFrames = 0;
mMaxFrames = 64; // This will grow as needed
mFrameOffsets = new int[mMaxFrames];
mFrameLens = new int[mMaxFrames];
mFrameGains = new int[mMaxFrames];
mMinGain = 1000000000;
mMaxGain = 0;
mBitRate = 10;
mOffset = 0;
// No need to handle filesizes larger than can fit in a 32-bit int
mFileSize = (int)mInputFile.length();
if (mFileSize < 128) {
throw new java.io.IOException("File too small to parse");
}
FileInputStream stream = new FileInputStream(mInputFile);
byte[] header = new byte[12];
stream.read(header, 0, 6);
mOffset += 6;
if (header[0] == '#' &&
header[1] == '!' &&
header[2] == 'A' &&
header[3] == 'M' &&
header[4] == 'R' &&
header[5] == '\n') {
parseAMR(stream, mFileSize - 6);
}
stream.read(header, 6, 6);
mOffset += 6;
if (header[4] == 'f' &&
header[5] == 't' &&
header[6] == 'y' &&
header[7] == 'p' &&
header[8] == '3' &&
header[9] == 'g' &&
header[10] == 'p' &&
header[11] == '4') {
int boxLen =
((0xff & header[0]) << 24) |
((0xff & header[1]) << 16) |
((0xff & header[2]) << 8) |
((0xff & header[3]));
if (boxLen >= 4 && boxLen <= mFileSize - 8) {
stream.skip(boxLen - 12);
mOffset += boxLen - 12;
}
parse3gpp(stream, mFileSize - boxLen);
}
}
示例13: testPrototypeInflaterGzip
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Test using the raw Inflater codec for reading gzip files.
*/
@Test
public void testPrototypeInflaterGzip() throws IOException {
CompressionCodec gzip = new GzipCodec(); // used only for file extension
localFs.delete(workDir, true); // localFs = FileSystem instance
System.out.println(COLOR_BR_BLUE + "testPrototypeInflaterGzip() using " +
"non-native/Java Inflater and manual gzip header/trailer parsing" +
COLOR_NORMAL);
// copy prebuilt (correct!) version of concat.gz to HDFS
final String fn = "concat" + gzip.getDefaultExtension();
Path fnLocal = new Path(System.getProperty("test.concat.data", "/tmp"), fn);
Path fnHDFS = new Path(workDir, fn);
localFs.copyFromLocalFile(fnLocal, fnHDFS);
final FileInputStream in = new FileInputStream(fnLocal.toString());
assertEquals("concat bytes available", 148, in.available());
// should wrap all of this header-reading stuff in a running-CRC wrapper
// (did so in BuiltInGzipDecompressor; see below)
byte[] compressedBuf = new byte[256];
int numBytesRead = in.read(compressedBuf, 0, 10);
assertEquals("header bytes read", 10, numBytesRead);
assertEquals("1st byte", 0x1f, compressedBuf[0] & 0xff);
assertEquals("2nd byte", 0x8b, compressedBuf[1] & 0xff);
assertEquals("3rd byte (compression method)", 8, compressedBuf[2] & 0xff);
byte flags = (byte)(compressedBuf[3] & 0xff);
if ((flags & 0x04) != 0) { // FEXTRA
numBytesRead = in.read(compressedBuf, 0, 2);
assertEquals("XLEN bytes read", 2, numBytesRead);
int xlen = ((compressedBuf[1] << 8) | compressedBuf[0]) & 0xffff;
in.skip(xlen);
}
if ((flags & 0x08) != 0) { // FNAME
while ((numBytesRead = in.read()) != 0) {
assertFalse("unexpected end-of-file while reading filename",
numBytesRead == -1);
}
}
if ((flags & 0x10) != 0) { // FCOMMENT
while ((numBytesRead = in.read()) != 0) {
assertFalse("unexpected end-of-file while reading comment",
numBytesRead == -1);
}
}
if ((flags & 0xe0) != 0) { // reserved
assertTrue("reserved bits are set??", (flags & 0xe0) == 0);
}
if ((flags & 0x02) != 0) { // FHCRC
numBytesRead = in.read(compressedBuf, 0, 2);
assertEquals("CRC16 bytes read", 2, numBytesRead);
int crc16 = ((compressedBuf[1] << 8) | compressedBuf[0]) & 0xffff;
}
// ready to go! next bytes should be start of deflated stream, suitable
// for Inflater
numBytesRead = in.read(compressedBuf);
// Inflater docs refer to a "dummy byte": no clue what that's about;
// appears to work fine without one
byte[] uncompressedBuf = new byte[256];
Inflater inflater = new Inflater(true);
inflater.setInput(compressedBuf, 0, numBytesRead);
try {
int numBytesUncompressed = inflater.inflate(uncompressedBuf);
String outString =
new String(uncompressedBuf, 0, numBytesUncompressed, "UTF-8");
System.out.println("uncompressed data of first gzip member = [" +
outString + "]");
} catch (java.util.zip.DataFormatException ex) {
throw new IOException(ex.getMessage());
}
in.close();
}
示例14: main
import java.io.FileInputStream; //导入方法依赖的package包/类
public static void
main(
String[] args )
{
try{
final FileInputStream fis = new FileInputStream( "C:\\temp\\mp.part6.rar" );
RARTOCDecoder decoder =
new RARTOCDecoder(
new DataProvider()
{
@Override
public int
read(
byte[] buffer )
throws IOException
{
return( fis.read( buffer ));
}
@Override
public void
skip(
long bytes )
throws IOException
{
fis.skip( bytes );
}
});
decoder.analyse(
new TOCResultHandler()
{
@Override
public void
entryRead(
String name,
long size,
boolean password )
{
System.out.println( name + ": " + size + (password?" protected":""));
}
@Override
public void
complete()
{
System.out.println( "complete" );
}
@Override
public void
failed(
IOException error )
{
System.out.println( "failed: " + Debug.getNestedExceptionMessage( error ));
}
});
}catch( Throwable e ){
e.printStackTrace();
}
}
示例15: jumpChunk
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* jumps the current chunk
* @param e FileInputStream
* @return false - always
* @throws IOException error in FileInputStream
*/
public boolean jumpChunk(FileInputStream e) throws IOException {
e.skip(length-6);
return false;
}