package org.jboss.ejb.plugins.cmp.jdbc;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.sql.Blob;
public final class ByteArrayBlob implements Blob
{
private final byte[] mBytes;
public ByteArrayBlob(byte[] bytes)
{
if (bytes == null)
{
bytes = new byte[0];
}
mBytes = bytes;
}
public InputStream getBinaryStream() throws SQLException
{
return new ByteArrayInputStream(mBytes);
}
public byte[] getBytes(long pos, int length) throws SQLException
{
if (length < 0 || length > mBytes.length || pos > mBytes.length)
{
return new byte[0];
}
if (pos <= 0)
{
pos = 1; }
byte[] buffer = new byte[length];
System.arraycopy(mBytes, (int)pos - 1, buffer, 0, length);
return buffer;
}
public long length() throws SQLException
{
return mBytes.length;
}
public long position(Blob pattern , long start) throws SQLException
{
return position(pattern.getBytes(0, (int)pattern.length()), start);
}
public long position(byte pattern[], long start) throws SQLException
{
int max = mBytes.length - pattern.length;
if (start < 0)
{
start = 0; } else if (start >= mBytes.length)
{
return -1; }
if (pattern.length == 0)
{
return -1; }
byte first = pattern[0];
int i = (int)start;
while (true)
{
while (i <= max && mBytes[i] != first)
{
i++;
}
if (i > max)
{
return -1; }
int j = i + 1;
int end = j + pattern.length - 1;
int k = 1;
boolean cont = true;
while (cont && j < end)
{
if (mBytes[j++] != pattern[k++])
{
i++;
cont = false;
}
}
if (cont)
{
return i;
}
}
}
public OutputStream setBinaryStream(long pos)
throws SQLException
{
throw new UnsupportedOperationException("ByteArrayBlob is immutable");
}
public int setBytes(long pos, byte[] bytes)
throws SQLException
{
throw new UnsupportedOperationException("ByteArrayBlob is immutable");
}
public int setBytes(long pos, byte[] bytes, int offset, int length)
throws SQLException
{
throw new UnsupportedOperationException("ByteArrayBlob is immutable");
}
public void truncate(long length)
throws SQLException
{
throw new UnsupportedOperationException("ByteArrayBlob is immutable");
}
}