Java Code Examples for java.io.FileInputStream#skip()

The following examples show how to use java.io.FileInputStream#skip() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NegativeAvailable.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 *  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;
}
 
Example 2
Source File: TransferManager.java    From jplag with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Reads the next part of the related file
 * @return an array containing the next part with the correct size
 * @throws JPlagException If the download has already been completed
 *    or there was an I/O error
 */
public byte[] readNextPart() throws JPlagException {
    if(remainingBytes==0) {
        throw new JPlagException("downloadException",
            "There's nothing left to be downloaded!","");
    }
    try {
        FileInputStream in = new FileInputStream(file);
        in.skip(filesize-remainingBytes);
        
        int partsize = remainingBytes;
        if(partsize>81920) partsize = 81920;
        
        byte[] data = new byte[partsize];
        in.read(data);
        in.close();
        remainingBytes -= partsize;
        return data;
    }
    catch(IOException e) {
        e.printStackTrace();
        throw new JPlagException("downloadException", "Unable to read" +
                " submission part from server!","");
    }
}
 
Example 3
Source File: NegativeAvailable.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 *  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;
}
 
Example 4
Source File: NegativeAvailable.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 *  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;
}
 
Example 5
Source File: NegativeAvailable.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 *  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;
}
 
Example 6
Source File: FileObject.java    From BACnet4J with GNU General Public License v3.0 6 votes vote down vote up
public OctetString readData(long start, long length) throws IOException {
    FileInputStream in = new FileInputStream(file);
    try {
        while (start > 0) {
            long result = in.skip(start);
            if (result == -1)
                // EOF
                break;
            start -= result;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamUtils.transfer(in, out, length);
        return new OctetString(out.toByteArray());
    }
    finally {
        in.close();
    }
}
 
Example 7
Source File: NegativeAvailable.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 *  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;
}
 
Example 8
Source File: NegativeAvailable.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 *  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;
}
 
Example 9
Source File: OldFileChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_writeLByteBufferJ_NonZeroPosition() throws Exception {
    final int pos = 5;
    ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
    writeBuffer.position(pos);
    int result = writeOnlyFileChannel.write(writeBuffer, pos);
    assertEquals(CONTENT_AS_BYTES_LENGTH - pos, result);
    assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position());
    writeOnlyFileChannel.close();

    assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfWriteOnlyFileChannel
            .length());

    fis = new FileInputStream(fileOfWriteOnlyFileChannel);
    byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH - pos];
    fis.skip(pos);
    fis.read(inputBuffer);
    String test = CONTENT.substring(pos);
    assertTrue(Arrays.equals(test.getBytes(), inputBuffer));
}
 
Example 10
Source File: FileFtpFile.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream createInputStream(long offset) throws IOException {
    final FileInputStream input = new FileInputStream(mFile);
    if (offset == 0)
        return new BufferedInputStream(input, mBufferSize);
    if (input.skip(offset) == offset)
        return new BufferedInputStream(input, mBufferSize);
    return new BufferedInputStream(input, mBufferSize);
}
 
Example 11
Source File: RawSamples.java    From Android-Audio-Recorder with Apache License 2.0 5 votes vote down vote up
public void open(long offset, int bufReadSize) {
    try {
        readBuffer = new byte[(int) getBufferLen(bufReadSize)];
        is = new FileInputStream(in);
        is.skip(offset * (AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: ContentDataSource.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long open(DataSpec dataSpec) throws ContentDataSourceException {
  try {
    uri = dataSpec.uri;
    transferInitializing(dataSpec);
    assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
    if (assetFileDescriptor == null) {
      throw new FileNotFoundException("Could not open file descriptor for: " + uri);
    }
    inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
    long assetStartOffset = assetFileDescriptor.getStartOffset();
    long skipped = inputStream.skip(assetStartOffset + dataSpec.position) - assetStartOffset;
    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 {
      long assetFileDescriptorLength = assetFileDescriptor.getLength();
      if (assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH) {
        // The asset must extend to the end of the file. If FileInputStream.getChannel().size()
        // returns 0 then the remaining length cannot be determined.
        FileChannel channel = inputStream.getChannel();
        long channelSize = channel.size();
        bytesRemaining = channelSize == 0 ? C.LENGTH_UNSET : channelSize - channel.position();
      } else {
        bytesRemaining = assetFileDescriptorLength - skipped;
      }
    }
  } catch (IOException e) {
    throw new ContentDataSourceException(e);
  }

  opened = true;
  transferStarted(dataSpec);

  return bytesRemaining;
}
 
Example 13
Source File: TaskLog.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * Read a log file from start to end positions. The offsets may be negative,
 * in which case they are relative to the end of the file. For example,
 * Reader(taskid, kind, 0, -1) is the entire file and 
 * Reader(taskid, kind, -4197, -1) is the last 4196 bytes. 
 * @param taskid the id of the task to read the log file for
 * @param kind the kind of log to read
 * @param start the offset to read from (negative is relative to tail)
 * @param end the offset to read upto (negative is relative to tail)
 * @param isCleanup whether the attempt is cleanup attempt or not
 * @throws IOException
 */
public Reader(TaskAttemptID taskid, LogName kind, 
              long start, long end, boolean isCleanup) throws IOException {
  // find the right log file
  LogFileDetail fileDetail = getLogFileDetail(taskid, kind, isCleanup);
  // calculate the start and stop
  long size = fileDetail.length;
  if (start < 0) {
    start += size + 1;
  }
  if (end < 0) {
    end += size + 1;
  }
  start = Math.max(0, Math.min(start, size));
  end = Math.max(0, Math.min(end, size));
  start += fileDetail.start;
  end += fileDetail.start;
  bytesRemaining = end - start;
  file = new FileInputStream(new File(getBaseDir(fileDetail.location), 
      kind.toString()));
  // skip upto start
  long pos = 0;
  while (pos < start) {
    long result = file.skip(start - pos);
    if (result < 0) {
      bytesRemaining = 0;
      break;
    }
    pos += result;
  }
}
 
Example 14
Source File: ArscFileGenerator.java    From Baffle with MIT License 5 votes vote down vote up
public  CRC32 createObfuscateFile( ArscData data, StringBlock tableBlock,
        StringBlock keyBlock, File file ) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    CRC32 cksum = new CRC32();
    CheckedOutputStream checkedOutputStream = new CheckedOutputStream(fileOutputStream, cksum);
    LEDataOutputStream out = new LEDataOutputStream(checkedOutputStream);
    
    int tableStrChange = data.getmTableStrings().getSize() - tableBlock.getSize();
    int keyStrChange = data.getmSpecNames().getSize() - keyBlock.getSize();
    data.getmHeader().chunkSize -=(tableStrChange + keyStrChange);
    data.getmHeader().write(out);
    out.writeInt(1);
    tableBlock.write(out);
    data.getmPkgHeader().header.chunkSize -=keyStrChange;
    data.getmPkgHeader().write(out);
    data.getTypeNames().write(out);
    keyBlock.write(out);
    
    byte[] buff = new byte[1024];
    FileInputStream in = new FileInputStream(data.getFile());
    in.skip(data.getmResIndex());
    int len ;
    while(((len = in.read(buff)) != -1)){
        out.write(buff , 0 , len);
    }
    
    in.close();
    out.close();
    checkedOutputStream.close();
    fileOutputStream.close();
    return cksum;
}
 
Example 15
Source File: ProcessCorpus.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void processLabeledData(String input,String output) throws Exception{
	FileInputStream is = new FileInputStream(input);
	is.skip(3); //skip BOM
	BufferedReader r = new BufferedReader(
			new InputStreamReader(is, "utf8"));
	OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(output), "utf8");
	while(true) {
		String sent = r.readLine();
		if(null == sent) break;
		String s = Tags.genSegSequence(sent, delimer, 4);
		w.write(s);
	}
	r.close();
	w.close();
}
 
Example 16
Source File: ContentDataSource.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public long open(DataSpec dataSpec) throws ContentDataSourceException {
  try {
    uri = dataSpec.uri;
    transferInitializing(dataSpec);
    assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r");
    if (assetFileDescriptor == null) {
      throw new FileNotFoundException("Could not open file descriptor for: " + uri);
    }
    inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
    long assetStartOffset = assetFileDescriptor.getStartOffset();
    long skipped = inputStream.skip(assetStartOffset + dataSpec.position) - assetStartOffset;
    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 {
      long assetFileDescriptorLength = assetFileDescriptor.getLength();
      if (assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH) {
        // The asset must extend to the end of the file. If FileInputStream.getChannel().size()
        // returns 0 then the remaining length cannot be determined.
        FileChannel channel = inputStream.getChannel();
        long channelSize = channel.size();
        bytesRemaining = channelSize == 0 ? C.LENGTH_UNSET : channelSize - channel.position();
      } else {
        bytesRemaining = assetFileDescriptorLength - skipped;
      }
    }
  } catch (IOException e) {
    throw new ContentDataSourceException(e);
  }

  opened = true;
  transferStarted(dataSpec);

  return bytesRemaining;
}
 
Example 17
Source File: TaskLog.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Read a log file from start to end positions. The offsets may be negative,
 * in which case they are relative to the end of the file. For example,
 * Reader(taskid, kind, 0, -1) is the entire file and 
 * Reader(taskid, kind, -4197, -1) is the last 4196 bytes. 
 * @param taskid the id of the task to read the log file for
 * @param kind the kind of log to read
 * @param start the offset to read from (negative is relative to tail)
 * @param end the offset to read upto (negative is relative to tail)
 * @param isCleanup whether the attempt is cleanup attempt or not
 * @throws IOException
 */
public Reader(TaskAttemptID taskid, LogName kind, 
              long start, long end, boolean isCleanup) throws IOException {
  // find the right log file
  Map<LogName, LogFileDetail> allFilesDetails =
      getAllLogsFileDetails(taskid, isCleanup);
  LogFileDetail fileDetail = allFilesDetails.get(kind);
  // calculate the start and stop
  long size = fileDetail.length;
  if (start < 0) {
    start += size + 1;
  }
  if (end < 0) {
    end += size + 1;
  }
  start = Math.max(0, Math.min(start, size));
  end = Math.max(0, Math.min(end, size));
  start += fileDetail.start;
  end += fileDetail.start;
  bytesRemaining = end - start;
  file = new FileInputStream(new File(getBaseDir(fileDetail.location), 
      kind.toString()));
  // skip upto start
  long pos = 0;
  while (pos < start) {
    long result = file.skip(start - pos);
    if (result < 0) {
      bytesRemaining = 0;
      break;
    }
    pos += result;
  }
}
 
Example 18
Source File: ByteBufferInputStreamTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void testStream() throws FileNotFoundException, IOException {
	File f = File.createTempFile( ByteBufferInputStreamTest.class.getName(), "tmp" );
	f.deleteOnExit();
	final int l = 100000;
	long seed = System.currentTimeMillis();
	if ( DEBUG ) System.err.println( "Seed: " + seed );
	Random random = new Random( seed );

	for( int n = 1; n < 4; n++ ) {
		final FileOutputStream fos = new FileOutputStream( f );
		for( int i = 0; i < l * n; i++ ) fos.write( random.nextInt() & 0xFF );
		fos.close();

		ByteBufferInputStream bis = ByteBufferInputStream.map( new FileInputStream( f ).getChannel(), MapMode.READ_ONLY );

		FileInputStream test = new FileInputStream( f );
		FileChannel fc = test.getChannel();
		int a1, a2, off, len, pos;
		byte b1[] = new byte[ 32768 ];
		byte b2[] = new byte[ 32768 ];

		for( int k = 0; k < l / 10; k++ ) {

			switch ( random.nextInt( 6 ) ) {

			case 0:
				if ( DEBUG ) System.err.println( "read()" );
				a1 = bis.read();
				a2 = test.read();
				assertEquals( a2, a1 );
				break;

			case 1:
				off = random.nextInt( b1.length );
				len = random.nextInt( b1.length - off + 1 );
				a1 = bis.read( b1, off, len );
				a2 = test.read( b2, off, len );
				if ( DEBUG ) System.err.println( "read(b, " + off + ", " + len + ")" );

				assertEquals( a2, a1 );

				for ( int i = off; i < off + len; i++ )
					assertEquals( b2[ i ], b1[ i ] );
				break;

			case 2:
				if ( DEBUG ) System.err.println( "available()" );
				assertEquals( test.available(), bis.available() );
				break;

			case 3:
				pos = (int)bis.position();
				if ( DEBUG ) System.err.println( "position()=" + pos );
				assertEquals( (int)fc.position(), pos );
				break;

			case 4:
				pos = random.nextInt( l * n );
				bis.position( pos );
				if ( DEBUG ) System.err.println( "position(" + pos + ")" );
				( test = new FileInputStream( f ) ).skip( pos );
				fc = test.getChannel();
				break;

			case 5:
				pos = random.nextInt( (int)( l * n - bis.position() + 1 ) );
				if ( DEBUG ) System.err.println( "skip(" + pos + ")" );
				a1 = (int)bis.skip( pos );
				a2 = (int)test.skip( pos );
				assertEquals( a2, a1 );
				break;
			}
		}
		
		test.close();
	}
}
 
Example 19
Source File: TailFileMulti.java    From BigDataScript with Apache License 2.0 4 votes vote down vote up
/**
 * Check if there is output available on any file
 * @returns Number of bytes read. Negative number of there were problems
 */
@Override
protected int tail() {
	if (!exists) {
		exists = inputFile.exists();
		if (!exists) return 0;
	}

	// File size
	long size = inputFile.length();
	if (size <= inputPos) return 0;

	int avail = (int) (size - inputPos);
	int count = 0;
	try {
		// Open input
		FileInputStream fis = new FileInputStream(inputFileName);
		fis.skip(inputPos);
		BufferedInputStream input = new BufferedInputStream(fis);

		BufferedOutputStream output = null;
		avail = input.available();

		while (avail > 0) {
			// Read all available bytes
			avail = Math.min(avail, MAX_BUFFER_SIZE); // Limit buffer size (some systems return MAX_INT when the file is growing)
			byte[] bytes = new byte[avail];
			count = input.read(bytes);
			inputPos += count;

			// Show bytes
			if (showStderr) System.err.write(bytes, 0, count);
			else System.out.write(bytes, 0, count);

			// Write to output
			if (output != null) output.write(bytes, 0, count);

			avail = input.available(); // Something more to read?
		}

		// Close input and output
		input.close();
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return count;
}
 
Example 20
Source File: SimpleWebServer.java    From Lanmitm with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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() + "" + file.length()).hashCode());

		// Support (simple) skipping:
		long startFrom = 0;
		long endAt = -1;
		String range = header.get("range");
		if (range != null) {
			if (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;
					}
				};
				fis.skip(startFrom);

				res = createResponse(Response.Status.PARTIAL_CONTENT, mime,
						fis);
				res.addHeader("Content-Length", "" + 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", "" + fileLen);
				res.addHeader("ETag", etag);
			}
		}
	} catch (IOException ioe) {
		res = getForbiddenResponse("Reading file failed.");
	}

	return res;
}