aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/jogamp/opengl/util/pngj/ProgressiveOutputStream.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jogl/classes/jogamp/opengl/util/pngj/ProgressiveOutputStream.java')
-rw-r--r--src/jogl/classes/jogamp/opengl/util/pngj/ProgressiveOutputStream.java71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/jogl/classes/jogamp/opengl/util/pngj/ProgressiveOutputStream.java b/src/jogl/classes/jogamp/opengl/util/pngj/ProgressiveOutputStream.java
new file mode 100644
index 000000000..bbec247fb
--- /dev/null
+++ b/src/jogl/classes/jogamp/opengl/util/pngj/ProgressiveOutputStream.java
@@ -0,0 +1,71 @@
+package jogamp.opengl.util.pngj;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+/**
+ * stream that outputs to memory and allows to flush fragments every 'size' bytes to some other destination
+ */
+abstract class ProgressiveOutputStream extends ByteArrayOutputStream {
+ private final int size;
+
+ public ProgressiveOutputStream(int size) {
+ this.size = size;
+ }
+
+ @Override
+ public final void close() throws IOException {
+ flush();
+ super.close();
+ }
+
+ @Override
+ public final void flush() throws IOException {
+ super.flush();
+ checkFlushBuffer(true);
+ }
+
+ @Override
+ public final void write(byte[] b, int off, int len) {
+ super.write(b, off, len);
+ checkFlushBuffer(false);
+ }
+
+ @Override
+ public final void write(byte[] b) throws IOException {
+ super.write(b);
+ checkFlushBuffer(false);
+ }
+
+ @Override
+ public final void write(int arg0) {
+ super.write(arg0);
+ checkFlushBuffer(false);
+ }
+
+ @Override
+ public final synchronized void reset() {
+ super.reset();
+ }
+
+ /**
+ * if it's time to flush data (or if forced==true) calls abstract method flushBuffer() and cleans those bytes from
+ * own buffer
+ */
+ private final void checkFlushBuffer(boolean forced) {
+ while (forced || count >= size) {
+ int nb = size;
+ if (nb > count)
+ nb = count;
+ if (nb == 0)
+ return;
+ flushBuffer(buf, nb);
+ int bytesleft = count - nb;
+ count = bytesleft;
+ if (bytesleft > 0)
+ System.arraycopy(buf, nb, buf, 0, bytesleft);
+ }
+ }
+
+ public abstract void flushBuffer(byte[] b, int n);
+} \ No newline at end of file