org.apache.commons.io.input.CountingInputStream Java Examples

The following examples show how to use org.apache.commons.io.input.CountingInputStream. 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: DoubleMatrixDatasetRandomAccessWriter.java    From systemsgenetics with GNU General Public License v3.0 6 votes vote down vote up
public void open(String loc) throws IOException {
	channel = new RandomAccessFile(loc + ".dat", "rw").getChannel();
	CountingInputStream countertmp = new CountingInputStream(new BufferedInputStream(Channels.newInputStream(channel), buffersize));
	DataInputStream is = new DataInputStream(countertmp);
	nrRows = is.readInt();
	nrCols = is.readInt();
	hashRows = DoubleMatrixDataset.loadIdentifiers(loc + ".rows.txt");
	hashCols = DoubleMatrixDataset.loadIdentifiers(loc + ".cols.txt");

	headerLen = 8;
	currentPos = headerLen;
	bytesPerRow = 8 * nrCols;
	buffersize = bytesPerRow * 10;
	channel.position(currentPos);
	counter = new CountingOutputStream(new BufferedOutputStream(Channels.newOutputStream(channel), buffersize));
	os = new DataOutputStream(counter);

	System.out.println("Read header. current pos: " + channel.position());
	System.out.println("Header: " + headerLen);
}
 
Example #2
Source File: DoubleMatrixDatasetRandomAccessReader.java    From systemsgenetics with GNU General Public License v3.0 6 votes vote down vote up
public DoubleMatrixDatasetRandomAccessReader(String filename) throws IOException {

		this.path = new File(filename + ".dat");
		channel = new RandomAccessFile(path, "r").getChannel();
		filesize = channel.size();
		counter = new CountingInputStream(new BufferedInputStream(Channels.newInputStream(channel), buffersize));
		is = new DataInputStream(counter);

		nrRows = is.readInt();
		nrCols = is.readInt();

		hashRows = (LinkedHashMap<R, Integer>) DoubleMatrixDataset.loadIdentifiers(filename + ".rows.txt");
		hashCols = (LinkedHashMap<C, Integer>) DoubleMatrixDataset.loadIdentifiers(filename + ".cols.txt");

		headerLen = 8;
		bytesPerRow = 8 * nrCols;
		buffersize = bytesPerRow * 10;
		currentPos = headerLen;
		System.out.println(filename + " is " + nrRows + " rows and " + nrCols + " cols; bytes per row: " + bytesPerRow);
	}
 
Example #3
Source File: InstantIncomingResourceNegotiation.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
private void receiveStream(IProgressMonitor monitor, int fileCount)
    throws SarosCancellationException, IOException {
  String message = "Receiving files from " + getPeer().getName() + "...";
  monitor.beginTask(message, fileCount);

  monitor.subTask("Waiting for Host to start...");
  monitor.waitForCompletion(expectedTransfer);
  monitor.subTask("Host is starting to send...");
  log.debug(this + ": Host is starting to send...");

  try (InputStream transmissionStream = expectedTransfer.get().acceptStream();
      CountingInputStream countStream = new CountingInputStream(transmissionStream);
      IncomingStreamProtocol isp = new IncomingStreamProtocol(countStream, session, monitor)) {
    isp.receiveStream();
    log.debug("stream bytes received: " + countStream.getByteCount());
  } catch (InterruptedException | ExecutionException e) {
    throw new LocalCancellationException(e.getMessage(), CancelOption.NOTIFY_PEER);
  }

  log.debug(this + ": stream transmission done");
  monitor.done();
}
 
Example #4
Source File: StoregateReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testReadCloseReleaseEntity() throws Exception {
    final TransferStatus status = new TransferStatus();
    final byte[] content = RandomUtils.nextBytes(32769);
    final TransferStatus writeStatus = new TransferStatus();
    writeStatus.setLength(content.length);
    final StoregateIdProvider nodeid = new StoregateIdProvider(session).withCache(cache);
    final Path room = new StoregateDirectoryFeature(session, nodeid).mkdir(
        new Path(String.format("/My files/%s", new AlphanumericRandomStringService().random()),
            EnumSet.of(Path.Type.directory, Path.Type.volume)), null, new TransferStatus());
    final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final StoregateWriteFeature writer = new StoregateWriteFeature(session, nodeid);
    final HttpResponseOutputStream<VersionId> out = writer.write(test, writeStatus, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(writeStatus, writeStatus).transfer(new ByteArrayInputStream(content), out);
    final CountingInputStream in = new CountingInputStream(new StoregateReadFeature(session, nodeid).read(test, status, new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new StoregateDeleteFeature(session, nodeid).delete(Collections.singletonList(room), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example #5
Source File: ArscData.java    From Baffle with MIT License 6 votes vote down vote up
private static ArscData readTable(File file , CountingInputStream countIn, ExtDataInput in )
        throws IOException {
    ArscData arscData = new ArscData();
    arscData.mFile = file;
    arscData.mHeader = Header.read(in);
    int packageCount = in.readInt();
    if (packageCount != 1) {
        throw new UnsupportedOperationException("not support more then 1 package");
    }
    arscData.mTableStrings = StringBlock.read(in);
    arscData.mPkgHeaderIndex = (int) countIn.getByteCount();
    arscData.mPkgHeader = PackageHeader.read(in);
    arscData.mTypeStrStart = (int) countIn.getByteCount();
    arscData.mTypeNames = StringBlock.read(in);
    arscData.mTypeStrEnd =  (int) countIn.getByteCount();
    arscData.mSpecNames = StringBlock.read(in);
    arscData.mResIndex =  (int) countIn.getByteCount();
    return arscData;
}
 
Example #6
Source File: SDSReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testReadCloseReleaseEntity() throws Exception {
    final TransferStatus status = new TransferStatus();
    final byte[] content = RandomUtils.nextBytes(32769);
    final TransferStatus writeStatus = new TransferStatus();
    writeStatus.setLength(content.length);
    final SDSNodeIdProvider nodeid = new SDSNodeIdProvider(session).withCache(cache);
    final Path room = new SDSDirectoryFeature(session, nodeid).mkdir(
        new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory, Path.Type.volume, Path.Type.triplecrypt)), null, new TransferStatus());
    final Path test = new Path(room, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final SDSWriteFeature writer = new SDSWriteFeature(session, nodeid);
    final HttpResponseOutputStream<VersionId> out = writer.write(test, writeStatus, new DisabledConnectionCallback());
    assertNotNull(out);
    new StreamCopier(writeStatus, writeStatus).transfer(new ByteArrayInputStream(content), out);
    final CountingInputStream in = new CountingInputStream(new SDSReadFeature(session, nodeid).read(test, status, new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new SDSDeleteFeature(session, nodeid).delete(Collections.singletonList(room), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example #7
Source File: AutoCRLFObjectLoader.java    From git-code-format-maven-plugin with MIT License 6 votes vote down vote up
@Override
public long getSize() {
  // https://github.com/Cosium/git-code-format-maven-plugin/issues/42: It is very important to
  // return the exact transformed content size
  if (cachedSize != null) {
    return cachedSize;
  }
  try (CountingInputStream countingInputStream = new CountingInputStream(openStream())) {
    while (countingInputStream.read() != -1) {
      // Do nothing in the while, we are just moving bytes through CountingInputstream to retrieve
      // the total stream size
    }
    cachedSize = countingInputStream.getByteCount();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  return cachedSize;
}
 
Example #8
Source File: GoogleStorageReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testReadWhitespace() throws Exception {
    final int length = 47;
    final byte[] content = RandomUtils.nextBytes(length);
    final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path file = new Path(container, String.format("t %s", new AlphanumericRandomStringService().random()), EnumSet.of(Path.Type.file));
    final TransferStatus status = new TransferStatus().length(content.length);
    status.setChecksum(new SHA256ChecksumCompute().compute(new ByteArrayInputStream(content), status));
    final OutputStream out = new GoogleStorageWriteFeature(session).write(file, status, new DisabledConnectionCallback());
    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
    out.close();
    assertEquals(length, new GoogleStorageAttributesFinderFeature(session).find(file).getSize());
    final CountingInputStream in = new CountingInputStream(new GoogleStorageReadFeature(session).read(file, new TransferStatus(), new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new GoogleStorageDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example #9
Source File: PEReader.java    From phoenicis with GNU Lesser General Public License v3.0 6 votes vote down vote up
private RsrcSection readResourceSection(CountingInputStream executableInputStream, SectionHeader[] sectionHeaders)
        throws IOException {
    SectionHeader rsrcSectionHeader = null;
    for (SectionHeader sectionHeader : sectionHeaders) {
        if (".rsrc\u0000\u0000\u0000".equals(new String(sectionHeader.name))) {
            rsrcSectionHeader = sectionHeader;
        }
    }

    if (rsrcSectionHeader == null) {
        return null;
    }

    long numberToSkip = rsrcSectionHeader.pointerToRawData.getUnsignedValue() - executableInputStream.getCount();
    executableInputStream.skip(numberToSkip);
    byte[] rsrcSection = new byte[(int) rsrcSectionHeader.sizeOfRawData.getUnsignedValue()];
    executableInputStream.read(rsrcSection);

    return new RsrcSection(rsrcSection);
}
 
Example #10
Source File: GoogleStorageReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testReadCloseReleaseEntity() throws Exception {
    final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path file = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
    final int length = 2048;
    final byte[] content = RandomUtils.nextBytes(length);
    final TransferStatus status = new TransferStatus().length(content.length);
    status.setChecksum(new SHA256ChecksumCompute().compute(new ByteArrayInputStream(content), status));
    final OutputStream out = new GoogleStorageWriteFeature(session).write(file, status, new DisabledConnectionCallback());
    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
    out.close();
    final CountingInputStream in = new CountingInputStream(new GoogleStorageReadFeature(session).read(file, status, new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new GoogleStorageDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example #11
Source File: S3ReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testReadCloseReleaseEntity() throws Exception {
    final Path container = new Path("test-us-east-1-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path file = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final int length = 2048;
    final byte[] content = RandomUtils.nextBytes(length);
    final TransferStatus status = new TransferStatus().length(content.length);
    status.setChecksum(new SHA256ChecksumCompute().compute(new ByteArrayInputStream(content), status));
    final OutputStream out = new S3WriteFeature(session).write(file, status, new DisabledConnectionCallback());
    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
    out.close();
    final CountingInputStream in = new CountingInputStream(new S3ReadFeature(session).read(file, status, new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new S3DefaultDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example #12
Source File: ChunkListDecrypter.java    From InflatableDonkey with MIT License 5 votes vote down vote up
/**
 *
 * @param container
 * @param inputStream closed on exit
 * @param store
 * @throws IOException
 * @throws ArithmeticException on input streams over 2 Gb.
 * @throws IllegalArgumentException on non 0x01 chunk keys
 */
public void apply(StorageHostChunkList container, InputStream inputStream, ChunkStore store) throws IOException {
    logger.trace("<< apply() - input: {}", inputStream);
    // Ensure our chunk offsets are sequentially ordered.
    List<ChunkInfo> list = container.getChunkInfoList()
            .stream()
            .sorted(CHUNK_OFFSET_COMPARATOR)
            .collect(toList());

    try (CountingInputStream countingInputStream = new CountingInputStream(inputStream)) {
        streamChunks(list, countingInputStream, store);
    } catch (UncheckedIOException ex) {
        throw ex.getCause();
    }

    if (logger.isDebugEnabled()) {
        // Sanity check. Has a minor IO cost with a disk based chunk store.
        String missingChunks = list.stream()
                .map(ci -> ci.getChunkChecksum().toByteArray())
                .filter(c -> !store.contains(c))
                .map(c -> "0x" + Hex.toHexString(c))
                .collect(joining(" "));
        if (missingChunks.isEmpty()) {
            logger.debug("-- apply() - all chunks have been stored");
        } else {
            logger.warn("-- apply() - missing chunks: {}", missingChunks);
        }
    }
    logger.trace(">> apply()");
}
 
Example #13
Source File: RemoteNodeProcessor.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private boolean handleResponseBody(InputStream inputStream, TickBuilder tickBuilder) {
  CountingInputStream countingStream = new CountingInputStream(inputStream);
  DataInputStream input = new DataInputStream(countingStream);
  RemoteMessage message;

  try {
    while ((message = mapper.decode(input)) != null) {
      if (message instanceof TrackStartResponseMessage) {
        handleTrackStartResponse((TrackStartResponseMessage) message);
      } else if (message instanceof TrackFrameDataMessage) {
        handleTrackFrameData((TrackFrameDataMessage) message);
      } else if (message instanceof TrackExceptionMessage) {
        handleTrackException((TrackExceptionMessage) message);
      } else if (message instanceof NodeStatisticsMessage) {
        handleNodeStatistics((NodeStatisticsMessage) message);
      }
    }
  } catch (InterruptedException interruption) {
    log.error("Node {} processing thread was interrupted.", nodeAddress);
    Thread.currentThread().interrupt();
    return false;
  } catch (Throwable e) {
    log.error("Error when processing response from node {}.", nodeAddress, e);
    ExceptionTools.rethrowErrors(e);
  } finally {
    tickBuilder.responseSize = countingStream.getCount();
  }

  return true;
}
 
Example #14
Source File: LogOp.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
private static RDFPatch readPatch(DeltaAction action) throws IOException {
    HttpServletRequest request = action.request;
    long byteLength = request.getContentLengthLong();
    try ( CountingInputStream in = new CountingInputStream(request.getInputStream()); ) {
        RDFPatch patch = RDFPatchOps.read(in);
        if ( byteLength != -1L ) {
            if ( in.getByteCount() != byteLength )
                FmtLog.warn(LOG, "[%d] Length mismatch: Read: %d : Content-Length: %d", action.id, in.getByteCount(),  byteLength);
        }
        return patch;
    }
}
 
Example #15
Source File: RawARSCDecoder.java    From AndResGuard with Apache License 2.0 5 votes vote down vote up
public static Header read(ExtDataInput in, CountingInputStream countIn) throws IOException {
  short type;
  int start = countIn.getCount();
  try {
    type = in.readShort();
  } catch (EOFException ex) {
    return new Header(TYPE_NONE, 0, 0, countIn.getCount());
  }
  return new Header(type, in.readShort(), in.readInt(), start);
}
 
Example #16
Source File: PEReader.java    From phoenicis with GNU Lesser General Public License v3.0 5 votes vote down vote up
public PEFile parseExecutable(InputStream inputStream) throws IOException {
    try (CountingInputStream executableInputStream = new CountingInputStream(inputStream)) {
        final ImageDOSHeader imageDOSHeader = readDosHeader(executableInputStream);
        final byte[] realModeStubProgram = readRealModeStubProgram(executableInputStream, imageDOSHeader);
        final ImageNTHeaders imageNTHeaders = readImageNTHeaders(executableInputStream);
        final SectionHeader[] sectionHeaders = readSectionHeaders(executableInputStream, imageNTHeaders);
        final RsrcSection resourceSection = readResourceSection(executableInputStream, sectionHeaders);
        return new PEFile(imageDOSHeader, realModeStubProgram, imageNTHeaders, sectionHeaders, resourceSection);
    }
}
 
Example #17
Source File: PEReader.java    From phoenicis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private SectionHeader[] readSectionHeaders(CountingInputStream executableInputStream, ImageNTHeaders imageNTHeaders)
        throws IOException {
    final int numberOfSectionHeaders = imageNTHeaders.fileHeader.numberOfSections.getUnsignedValue();
    final SectionHeader[] sectionHeaders = new SectionHeader[numberOfSectionHeaders];
    for (int i = 0; i < numberOfSectionHeaders; i++) {
        byte[] sectionHeaderBytes = new byte[SectionHeader.SECTION_HEADER_SIZE];
        executableInputStream.read(sectionHeaderBytes);
        sectionHeaders[i] = new SectionHeader(sectionHeaderBytes);
    }
    return sectionHeaders;
}
 
Example #18
Source File: BoundSessionInputBuffer.java    From BUbiNG with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link SessionInputBuffer} bounded to a given maximum length.
 *
 * @param buffer the buffer to wrap
 * @param length the maximum number of bytes to read (from the buffered stream).
 */
public BoundSessionInputBuffer(final SessionInputBuffer buffer, final long length) {
	super(new HttpTransportMetricsImpl(), BUFFER_SIZE, 0, null, null);
	this.bounded = new ContentLengthInputStream(buffer, length);
	this.input = new CountingInputStream(this.bounded);
	super.bind(this.input);
	this.length = length;
}
 
Example #19
Source File: ChunkListDecrypter.java    From InflatableDonkey with MIT License 5 votes vote down vote up
void streamChunks(List<ChunkInfo> chunkInfos, CountingInputStream inputStream, ChunkStore store) {
    logger.debug("-- streamChunks() - chunk count: {}", chunkInfos.size());
    chunkInfos.stream()
            .peek(ci -> logger.debug("-- streamChunks() - chunk info: {}", ci))
            .filter(u -> isChunkMissing(u, store))
            .forEach(u -> streamChunk(inputStream, inputStream.getCount(), u, store));
}
 
Example #20
Source File: HTTPURLConnSLR.java    From webarchive-commons with Apache License 2.0 5 votes vote down vote up
@Override
protected InputStream doSeekLoad(long offset, int maxLength)
		throws IOException {
	
	URL theUrl = new URL(url);		
	
       URLConnection connection = theUrl.openConnection();
       
       httpUrlConn = (HttpURLConnection)connection;
       
       connection.setConnectTimeout(connTimeout);
       connection.setReadTimeout(readTimeout);
       
       String rangeHeader = makeRangeHeader(offset, maxLength);
       
       if (rangeHeader != null) {
       	httpUrlConn.addRequestProperty("Range", rangeHeader);
       }
       
	if (this.isNoKeepAlive()) {
		httpUrlConn.addRequestProperty("Connection", "close");
	}
	
	if (this.getCookie() != null) {
		httpUrlConn.addRequestProperty("Cookie", cookie);
	}
	
	httpUrlConn.connect();
	
	int code = httpUrlConn.getResponseCode();
	connectedUrl =  httpUrlConn.getURL().toString();
	
	if ((code != 206) && (code != 200)) {
		throw new BadHttpStatusException(code, connectedUrl + " " + rangeHeader);
	}
	
	InputStream is = httpUrlConn.getInputStream();
	cin = new CountingInputStream(is);
	return cin;
}
 
Example #21
Source File: ImportAdapterCSV.java    From arx with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of this object with given configuration.
 *
 * @param config {@link #config}
 * @throws IOException In case file doesn't contain actual data
 */
protected ImportAdapterCSV(ImportConfigurationCSV config) throws IOException {

    super(config);
    this.config = config;
    this.bytesTotal = new File(config.getFileLocation()).length();

    /* Used to keep track of progress */
    cin = new CountingInputStream(new FileInputStream(new File(config.getFileLocation())));

    /* Determine length*/
    if (config.isOptimizedLoading()) {
        records = getLength(new FileInputStream(new File(config.getFileLocation())), config.getCharset(), config.getDelimiter(), config.getQuote(), config.getEscape(), config.getLinebreak(), config.getMaxColumns());
    }
    
    /* Get CSV iterator */
    in = new CSVDataInput(cin, config.getCharset(), config.getDelimiter(), config.getQuote(), config.getEscape(), config.getLinebreak(), new CSVOptions(config.getMaxColumns()));
    it = in.iterator(config.getContainsHeader());

    /* Check whether there is actual data within the CSV file */
    if (it.hasNext()) {
        row = it.next();
        if (config.getContainsHeader()) {
            if (!it.hasNext()) {
                throw new IOException("CSV contains nothing but header");
            }
        }
    } else {
        throw new IOException("CSV file contains no data");
    }

    // Create header
    header = createHeader();
}
 
Example #22
Source File: DriveReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReadPath() throws Exception {
    final DriveFileidProvider fileid = new DriveFileidProvider(session).withCache(cache);
    final Path directory = new DriveDirectoryFeature(session, fileid).mkdir(new Path(DriveHomeFinderService.MYDRIVE_FOLDER, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), null, new TransferStatus());
    final Path file = new DriveTouchFeature(session, fileid).touch(new Path(directory, String.format("t %s", new AlphanumericRandomStringService().random()), EnumSet.of(Path.Type.file)), new TransferStatus());
    assertEquals(0, new DriveAttributesFinderFeature(session, fileid).find(file).getSize());
    final CountingInputStream in = new CountingInputStream(new DriveReadFeature(session, fileid).read(file, new TransferStatus(), new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new DriveDeleteFeature(session, fileid).delete(Arrays.asList(file, directory), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example #23
Source File: ARSCDecoder.java    From ratel with Apache License 2.0 5 votes vote down vote up
private ARSCDecoder(InputStream arscStream, ResTable resTable, boolean storeFlagsOffsets, boolean keepBroken) {
    arscStream = mCountIn = new CountingInputStream(arscStream);
    if (storeFlagsOffsets) {
        mFlagsOffsets = new ArrayList<FlagsOffset>();
    } else {
        mFlagsOffsets = null;
    }
    // We need to explicitly cast to DataInput as otherwise the constructor is ambiguous.
    // We choose DataInput instead of InputStream as ExtDataInput wraps an InputStream in
    // a DataInputStream which is big-endian and ignores the little-endian behavior.
    mIn = new ExtDataInput((DataInput) new LittleEndianDataInputStream(arscStream));
    mResTable = resTable;
    mKeepBroken = keepBroken;
}
 
Example #24
Source File: ARSCDecoder.java    From ratel with Apache License 2.0 5 votes vote down vote up
public static Header read(ExtDataInput in, CountingInputStream countIn) throws IOException {
    short type;
    int start = countIn.getCount();
    try {
        type = in.readShort();
    } catch (EOFException ex) {
        return new Header(TYPE_NONE, 0, 0, countIn.getCount());
    }
    return new Header(type, in.readShort(), in.readInt(), start);
}
 
Example #25
Source File: SwiftReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReadCloseReleaseEntity() throws Exception {
    final TransferStatus status = new TransferStatus();
    final Path container = new Path(".ACCESS_LOGS", EnumSet.of(Path.Type.directory, Path.Type.volume));
    container.attributes().setRegion("DFW");
    final SwiftRegionService regionService = new SwiftRegionService(session);
    final CountingInputStream in = new CountingInputStream(new SwiftReadFeature(session, regionService).read(new Path(container,
        "/cdn.cyberduck.ch/2015/03/01/10/3b1d6998c430d58dace0c16e58aaf925.log.gz",
        EnumSet.of(Path.Type.file)), status, new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
}
 
Example #26
Source File: DAVReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReadCloseReleaseEntity() throws Exception {
    final Host host = new Host(new DAVSSLProtocol(), "svn.cyberduck.ch", new Credentials(
        PreferencesFactory.get().getProperty("connection.login.anon.name"), null
    ));
    final DAVSession session = new DAVSession(host, new DisabledX509TrustManager(), new DefaultX509KeyManager());
    session.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback());
    session.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
    final TransferStatus status = new TransferStatus();
    final Path test = new Path("/trunk/LICENSE.txt", EnumSet.of(Path.Type.file));
    final CountingInputStream in = new CountingInputStream(new DAVReadFeature(session).read(test, status, new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    session.close();
}
 
Example #27
Source File: DriveReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReadWhitespace() throws Exception {
    final DriveFileidProvider fileid = new DriveFileidProvider(session).withCache(cache);
    final Path file = new DriveTouchFeature(session, fileid).touch(new Path(DriveHomeFinderService.MYDRIVE_FOLDER, String.format("t %s", new AlphanumericRandomStringService().random()), EnumSet.of(Path.Type.file)), new TransferStatus());
    assertEquals(0, new DriveAttributesFinderFeature(session, fileid).find(file).getSize());
    final CountingInputStream in = new CountingInputStream(new DriveReadFeature(session, fileid).read(file, new TransferStatus(), new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new DriveDeleteFeature(session, fileid).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example #28
Source File: DriveReadFeatureTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReadEmpty() throws Exception {
    final DriveFileidProvider fileid = new DriveFileidProvider(session).withCache(cache);
    final Path directory = new DriveDirectoryFeature(session, fileid).mkdir(new Path(DriveHomeFinderService.MYDRIVE_FOLDER, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), null, new TransferStatus());
    final Path file = new DriveTouchFeature(session, fileid).touch(new Path(directory, String.format("t %s", new AlphanumericRandomStringService().random()), EnumSet.of(Path.Type.file)), new TransferStatus());
    assertEquals(0, new DriveAttributesFinderFeature(session, fileid).find(file).getSize());
    final CountingInputStream in = new CountingInputStream(new DriveReadFeature(session, fileid).read(file, new TransferStatus(), new DisabledConnectionCallback()));
    in.close();
    assertEquals(0L, in.getByteCount(), 0L);
    new DriveDeleteFeature(session, fileid).delete(Arrays.asList(file, directory), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
 
Example #29
Source File: FixedLengthInputStream.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 5 votes vote down vote up
public FixedLengthInputStream(InputStream stream, long maxLen) {
  super(new CountingInputStream(new CloseShieldInputStream(stream)));

  // Save a correctly-typed reference to the underlying stream.
  this.countingIn = (CountingInputStream) this.in;
  this.maxBytes = maxLen;
}
 
Example #30
Source File: TusServletRequest.java    From tus-java-server with MIT License 5 votes vote down vote up
public InputStream getContentInputStream() throws IOException {
    if (contentInputStream == null) {
        contentInputStream = super.getInputStream();

        //If we're dealing with chunked transfer encoding,
        //abstract it so that the rest of our code doesn't need to care
        boolean isChunked = hasChunkedTransferEncoding();
        if (isChunked && isChunkedTransferDecodingEnabled) {
            contentInputStream = new HttpChunkedEncodingInputStream(contentInputStream, trailerHeaders);
        }

        countingInputStream = new CountingInputStream(contentInputStream);
        contentInputStream = countingInputStream;

        ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.forUploadChecksumHeader(
                getHeader(HttpHeader.UPLOAD_CHECKSUM));

        List<ChecksumAlgorithm> algorithms;

        if (isChunked) {
            //Since the Checksum header can still come at the end, keep track of all checksums
            algorithms = Arrays.asList(ChecksumAlgorithm.values());
        } else if (checksumAlgorithm != null) {
            algorithms = Collections.singletonList(checksumAlgorithm);
        } else {
            algorithms = Collections.emptyList();
        }

        for (ChecksumAlgorithm algorithm : algorithms) {
            DigestInputStream is = new DigestInputStream(contentInputStream, algorithm.getMessageDigest());
            digestInputStreamMap.put(algorithm, is);

            contentInputStream = is;
        }
    }

    return contentInputStream;
}