Skip to content

Commit

Permalink
use Stream instead of byte[] for file content
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgoitia committed Oct 17, 2024
1 parent eb951cf commit 4cd1df6
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 104 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {

description = "PE file info extractor"
group = "es.goitia.pe"
version = "1.0.0"
version = "2.0.0"
var mainClassName = "es.goitia.pe.PEInfo"

repositories {
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/es/goitia/pe/CountingInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package es.goitia.pe;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* InputStream with offset counter
* Without custom support for {@link InputStream#mark(int)}
*/
public class CountingInputStream extends FilterInputStream {
long offset;

/**
* Wraps other inputStream for counting bytes read/skipped
*
* @param in the underlying input stream, or <code>null</code> if
* this instance is to be created without an underlying stream.
*/
protected CountingInputStream(InputStream in) {
super(in);
}

public long getOffset() {
return offset;
}

public void setOffset(long offset) {
this.offset = offset;
}

public void resetOffset() {
setOffset(0);
}

@Override
public int read() throws IOException {
int read = in.read();
if (read != -1) {
offset++;
}
return read;
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
int read = in.read(b, off, len);
if (read != -1) {
offset += read;
}
return read;
}

@Override
public long skip(long n) throws IOException {
long skipped = in.skip(n);
offset += skipped;
return skipped;
}

public boolean skipAll(long n) throws IOException {
long total = 0;
while (total < n) {
long s = skip(n - total);
if (s == 0) {
return false;
}
total += s;
}
return true;
}
}
Loading

0 comments on commit 4cd1df6

Please sign in to comment.