public byte[] getBytes(long pos, int length) throws SQLException { if(null == bytes){ try { bytes = IO.readByteArray(stream); } catch (NestedIOException e) { throw new SQLException("Could not get bytes from stream"); } } byte[] data = new byte[length]; System.arraycopy(bytes,(int)pos-1,(Object)data, 0, length); return data; }
/** * Get contents of an <code>InputStream</code> as a <code>byte[]</code>. Use this method instead of * <code>toByteArray(InputStream)</code> when <code>InputStream</code> size is known. <b>NOTE:</b> the method checks * that the length can safely be cast to an int without truncation before using * {@link IO#readByteArray(java.io.InputStream, int)} to read into the byte array. (Arrays can have no more than * Integer.MAX_VALUE entries anyway) * * @param input the <code>InputStream</code> to read from * @param size the size of <code>InputStream</code> * @return the requested byte array * @throws NestedIOException if an I/O error occurs or <code>InputStream</code> size differ from parameter size * @throws IllegalArgumentException if size is less than zero or size is greater than Integer.MAX_VALUE * @see IO#readByteArray(java.io.InputStream, int) * @since 2.1 */ public static byte[] readByteArray(InputStream input, long size) throws NestedIOException { if (size > Integer.MAX_VALUE) { throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size); } return readByteArray(input, (int) size); }
public static byte[] readByteArrayAndClose(InputStream is) { try{ return readByteArray(is); }finally{ close(is); } }
public static byte[] readByteArrayAndClose(Reader reader) { try{ return readByteArray(reader); }finally{ close(reader); } }
public static byte[] readByteArray(File file) { FileInputStream in = null; try{ in = new FileInputStream(file); return readByteArray(in); }catch(IOException e){ throw Exceptions.wrap(e); }finally{ close(in); } }
@Override public boolean convertTo(Blob value, Class<?> targetType, Type genericType, Out<Object> out, ConvertContext context) throws Throwable { //TODO : close inputstream ? byte[] data = IO.readByteArray(value.getBinaryStream()); out.set(Converts.convert(data, targetType, genericType)); return true; }
@Override public boolean convertTo(InputStream value, Class<?> targetType, Type genericType, Out<Object> out, ConvertContext context) throws Throwable { byte[] data = IO.readByteArray(value); out.set(Converts.convert(data, targetType,genericType)); return true; }