Skip to content

Commit

Permalink
DataBuffer, which is DataOutput to byte[].
Browse files Browse the repository at this point in the history
I tired of typing ByteArrayOutputStream.
  • Loading branch information
NathanSweet committed Aug 2, 2014
1 parent b2415b8 commit f19a2bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
28 changes: 28 additions & 0 deletions gdx/src/com/badlogic/gdx/utils/DataBuffer.java
@@ -0,0 +1,28 @@

package com.badlogic.gdx.utils;

import com.badlogic.gdx.utils.StreamUtils.OptimizedByteArrayOutputStream;

/** Extends {@link DataOutput} that writes bytes to a byte array.
* @author Nathan Sweet */
public class DataBuffer extends DataOutput {
private final OptimizedByteArrayOutputStream outStream;

public DataBuffer () {
this(32);
}

public DataBuffer (int initialSize) {
super(new OptimizedByteArrayOutputStream(initialSize));
outStream = (OptimizedByteArrayOutputStream)out;
}

/** Returns the backing array, which has 0 to {@link #size()} items. */
public byte[] getBuffer () {
return outStream.getBuffer();
}

public byte[] toArray () {
return outStream.toByteArray();
}
}
1 change: 1 addition & 0 deletions gdx/src/com/badlogic/gdx/utils/DataOutput.java
Expand Up @@ -16,6 +16,7 @@

package com.badlogic.gdx.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand Down
8 changes: 6 additions & 2 deletions gdx/src/com/badlogic/gdx/utils/StreamUtils.java
Expand Up @@ -93,8 +93,8 @@ public static void closeQuietly (Closeable c) {
}

/** A ByteArrayOutputStream which avoids copying of the byte array if not necessary. */
private static class OptimizedByteArrayOutputStream extends ByteArrayOutputStream {
OptimizedByteArrayOutputStream (int initialSize) {
static public class OptimizedByteArrayOutputStream extends ByteArrayOutputStream {
public OptimizedByteArrayOutputStream (int initialSize) {
super(initialSize);
}

Expand All @@ -103,5 +103,9 @@ public synchronized byte[] toByteArray () {
if (count == buf.length) return buf;
return super.toByteArray();
}

public byte[] getBuffer () {
return buf;
}
}
}

0 comments on commit f19a2bf

Please sign in to comment.