Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions

:
#)DataChunkOutputStream.java 1.1 2011-01-17 Copyright (c) 2008-2011 Werner Randelshofer, Goldau, Switzerland. All rights reserved. You may not use, copy or modify this file, except in compliance with the license agreement you entered into with Werner Randelshofer. For details see accompanying license terms.
 
 package org.monte.media.avi;
 
 import java.io.*;

This output stream filter supports common data types used inside of an AVI RIFF Data Chunk.

Author(s):
Werner Randelshofer
Version:
1.1 2011-01-17 Adds functionality for preventing "flush" and "close" of the underlying stream.
1.0.1 2010-04-05 Removed unused constants.
1.0 2008-08-11 Created.
 
 public class DataChunkOutputStream extends FilterOutputStream {

    
The number of bytes written to the data output stream so far. If this counter overflows, it will be wrapped to Integer.MAX_VALUE.
 
     protected long written;
    
Whether flush and close request shall be forwarded to underlying stream
 
     private boolean forwardFlushAndClose;
 
     public DataChunkOutputStream(OutputStream out) {
         this(outtrue);
     }
 
     public DataChunkOutputStream(OutputStream outboolean forwardFlushAndClose) {
         super(out);
         this. = forwardFlushAndClose;
     }

    
Writes an chunk type identifier (4 bytes).

Parameters:
s A string with a length of 4 characters.
 
     public void writeType(String sthrows IOException {
         if (s.length() != 4) {
             throw new IllegalArgumentException("type string must have 4 characters");
         }
 
         try {
             .write(s.getBytes("ASCII"), 0, 4);
             incCount(4);
         } catch (UnsupportedEncodingException e) {
             throw new InternalError(e.toString());
         }
     }

    
Writes out a byte to the underlying output stream as a 1-byte value. If no exception is thrown, the counter written is incremented by 1.

Parameters:
v a byte value to be written.
Throws:
java.io.IOException if an I/O error occurs.
See also:
java.io.FilterOutputStream.out
 
     public final void writeByte(int vthrows IOException {
         .write(v);
         incCount(1);
     }

    
Writes len bytes from the specified byte array starting at offset off to the underlying output stream. If no exception is thrown, the counter written is incremented by len.

Parameters:
b the data.
off the start offset in the data.
len the number of bytes to write.
Throws:
java.io.IOException if an I/O error occurs.
See also:
java.io.FilterOutputStream.out
 
     @Override
     public synchronized void write(byte b[], int offint len)
             throws IOException {
         .write(bofflen);
         incCount(len);
     }

    
Writes the specified byte (the low eight bits of the argument b) to the underlying output stream. If no exception is thrown, the counter written is incremented by 1.

Implements the write method of OutputStream.

Parameters:
b the byte to be written.
Throws:
java.io.IOException if an I/O error occurs.
See also:
java.io.FilterOutputStream.out
    @Override
    public synchronized void write(int bthrows IOException {
        .write(b);
        incCount(1);
    }

    
Writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4.

Parameters:
v an int to be written.
Throws:
java.io.IOException if an I/O error occurs.
See also:
java.io.FilterOutputStream.out
    public void writeInt(int vthrows IOException {
        .write((v >>> 0) & 0xff);
        .write((v >>> 8) & 0xff);
        .write((v >>> 16) & 0xff);
        .write((v >>> 24) & 0xff);
        incCount(4);
    }

    
Writes an unsigned 32 bit integer value.

Parameters:
v The value
Throws:
java.io.IOException
    public void writeUInt(long vthrows IOException {
        .write((int) ((v >>> 0) & 0xff));
        .write((int) ((v >>> 8) & 0xff));
        .write((int) ((v >>> 16) & 0xff));
        .write((int) ((v >>> 24) & 0xff));
        incCount(4);
    }

    
Writes a signed 16 bit integer value.

Parameters:
v The value
Throws:
java.io.IOException
    public void writeShort(int vthrows IOException {
        .write((int) ((v >>> 0) & 0xff));
        .write((int) ((v >> 8) & 0xff));
        incCount(2);
    }

    
Writes a signed 16 bit integer value.

Parameters:
v The value
Throws:
java.io.IOException
    public void writeShorts(short[] vint offint lenthrows IOException {
        for (int i = offi < off + leni++) {
            .write((int) ((v[i] >>> 0) & 0xff));
            .write((int) ((v[i] >> 8) & 0xff));
        }
        incCount(len * 2);
    }

    
Writes unsigned 24 bit integer values.

Parameters:
v The value
Throws:
java.io.IOException
    public void writeInts24(int[] vint offint lenthrows IOException {
        for (int i = offi < off + leni++) {
            .write((int) ((v[i] >>> 0) & 0xff));
            .write((int) ((v[i] >> 8) & 0xff));
            .write((int) ((v[i] >> 16) & 0xff));
        }
        incCount(len * 3);
    }
    public void writeLong(long vthrows IOException {
        .write((int) (v >>> 0) & 0xff);
        .write((int) (v >>> 8) & 0xff);
        .write((int) (v >>> 16) & 0xff);
        .write((int) (v >>> 24) & 0xff);
        .write((int) (v >>> 32) & 0xff);
        .write((int) (v >>> 40) & 0xff);
        .write((int) (v >>> 48) & 0xff);
        .write((int) (v >>> 56) & 0xff);
        incCount(8);
    }
    public void writeUShort(int vthrows IOException {
        .write((int) ((v >>> 0) & 0xff));
        .write((int) ((v >> 8) & 0xff));
        incCount(2);
    }

    
Increases the written counter by the specified value until it reaches Long.MAX_VALUE.
    protected void incCount(int value) {
        long temp =  + value;
        if (temp < 0) {
            temp = .;
        }
         = temp;
    }

    
Returns the current value of the counter written, the number of bytes written to this data output stream so far. If the counter overflows, it will be wrapped to Integer.MAX_VALUE.

Returns:
the value of the written field.
See also:
java.io.DataOutputStream.written
    public final long size() {
        return ;
    }

    
Sets the value of the counter written to 0.
    public void clearCount() {
         = 0;
    }
    @Override
    public void close() throws IOException {
        if () {
            super.close();
        }
    }
    @Override
    public void flush() throws IOException {
        if () {
            super.flush();
        }
    }
New to GrepCode? Check out our FAQ X