Mercurial > jmeCapture
view src/ca/randelshofer/SeekableByteArrayOutputStream.java @ 40:56dc950feaed
added README
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 03 Nov 2011 16:39:32 -0700 |
parents | 4c5fc53778c1 |
children | 302d5e9ad120 |
line wrap: on
line source
1 /*2 * @(#)SeekableByteArrayOutputStream.java 1.0 2010-12-273 *4 * Copyright © 2010 Werner Randelshofer, Immensee, Switzerland.5 * All rights reserved.6 *7 * You may not use, copy or modify this file, except in compliance with the8 * license agreement you entered into with Werner Randelshofer.9 * For details see accompanying license terms.10 */12 package ca.randelshofer;14 import java.io.ByteArrayOutputStream;15 import java.io.IOException;16 import java.io.OutputStream;17 import java.util.Arrays;18 import static java.lang.Math.*;19 /**20 * {@code SeekableByteArrayOutputStream}.21 *22 * @author Werner Randelshofer23 * @version 1.0 2010-12-27 Created.24 */25 public class SeekableByteArrayOutputStream extends ByteArrayOutputStream {27 /**28 * The current stream position.29 */30 private int pos;32 /**33 * Creates a new byte array output stream. The buffer capacity is34 * initially 32 bytes, though its size increases if necessary.35 */36 public SeekableByteArrayOutputStream() {37 this(32);38 }40 /**41 * Creates a new byte array output stream, with a buffer capacity of42 * the specified size, in bytes.43 *44 * @param size the initial size.45 * @exception IllegalArgumentException if size is negative.46 */47 public SeekableByteArrayOutputStream(int size) {48 if (size < 0) {49 throw new IllegalArgumentException("Negative initial size: "50 + size);51 }52 buf = new byte[size];53 }55 /**56 * Writes the specified byte to this byte array output stream.57 *58 * @param b the byte to be written.59 */60 @Override61 public synchronized void write(int b) {62 int newcount = max(pos + 1, count);63 if (newcount > buf.length) {64 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));65 }66 buf[pos++] = (byte)b;67 count = newcount;68 }70 /**71 * Writes <code>len</code> bytes from the specified byte array72 * starting at offset <code>off</code> to this byte array output stream.73 *74 * @param b the data.75 * @param off the start offset in the data.76 * @param len the number of bytes to write.77 */78 @Override79 public synchronized void write(byte b[], int off, int len) {80 if ((off < 0) || (off > b.length) || (len < 0) ||81 ((off + len) > b.length) || ((off + len) < 0)) {82 throw new IndexOutOfBoundsException();83 } else if (len == 0) {84 return;85 }86 int newcount = max(pos+len,count);87 if (newcount > buf.length) {88 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));89 }90 System.arraycopy(b, off, buf, pos, len);91 pos+=len;92 count = newcount;93 }95 /**96 * Resets the <code>count</code> field of this byte array output97 * stream to zero, so that all currently accumulated output in the98 * output stream is discarded. The output stream can be used again,99 * reusing the already allocated buffer space.100 *101 * @see java.io.ByteArrayInputStream#count102 */103 @Override104 public synchronized void reset() {105 count = 0;106 pos=0;107 }109 /**110 * Sets the current stream position to the desired location. The111 * next read will occur at this location. The bit offset is set112 * to 0.113 *114 * <p> An <code>IndexOutOfBoundsException</code> will be thrown if115 * <code>pos</code> is smaller than the flushed position (as116 * returned by <code>getflushedPosition</code>).117 *118 * <p> It is legal to seek past the end of the file; an119 * <code>EOFException</code> will be thrown only if a read is120 * performed.121 *122 * @param pos a <code>long</code> containing the desired file123 * pointer position.124 *125 * @exception IndexOutOfBoundsException if <code>pos</code> is smaller126 * than the flushed position.127 * @exception IOException if any other I/O error occurs.128 */129 public void seek(long pos) throws IOException {130 this.pos = (int)pos;131 }133 /**134 * Returns the current byte position of the stream. The next write135 * will take place starting at this offset.136 *137 * @return a long containing the position of the stream.138 *139 * @exception IOException if an I/O error occurs.140 */141 public long getStreamPosition() throws IOException {142 return pos;143 }145 /** Writes the contents of the byte array into the specified output146 * stream.147 * @param out148 */149 public void toOutputStream(OutputStream out) throws IOException {150 out.write(buf, 0, count);151 }153 }