src.java.org.codehaus.stax2.io.Stax2ByteArraySource Maven / Gradle / Ivy
package org.codehaus.stax2.io;
import java.io.*;
/**
* Simple implementation of {@link Stax2BlockSource} that encapsulates
* a byte array.
*
* Note that no copy is made of the passed-in array, and that further
* there are direct access methods. Thus, although callers are not
* to modify contents of returned array, this can not be guaranteed;
* and as such if this would be a problem (security problem for example),
* caller has to make a copy of the array and pass that copy to the
* constructor.
*/
public class Stax2ByteArraySource
extends Stax2BlockSource
{
final byte[] mBuffer;
final int mStart;
final int mLength;
public Stax2ByteArraySource(byte[] buf, int start, int len)
{
mBuffer = buf;
mStart = start;
mLength = len;
}
/*
/////////////////////////////////////////
// Implementation of the Public API
/////////////////////////////////////////
*/
public Reader constructReader()
throws IOException
{
String enc = getEncoding();
InputStream in = constructInputStream();
if (enc != null && enc.length() > 0) {
return new InputStreamReader(in, enc);
}
// Sub-optimal; really shouldn't use the platform default encoding
return new InputStreamReader(in);
}
public InputStream constructInputStream()
throws IOException
{
return new ByteArrayInputStream(mBuffer, mStart, mLength);
}
/*
/////////////////////////////////////////
// Additional API for this source
/////////////////////////////////////////
*/
public byte[] getBuffer() {
return mBuffer;
}
public int getBufferStart() {
return mStart;
}
public int getBufferLength() {
return mLength;
}
}