Java SQL Blob blobToBytes(Blob blob)

Here you can find the source of blobToBytes(Blob blob)

Description

blob To Bytes

License

Apache License

Declaration

public static byte[] blobToBytes(Blob blob) 

Method Source Code

//package com.java2s;
/**/*from  w w w.j  a v a 2 s  .  c  om*/
  *  Copyright (c) 2014 http://www.lushapp.wang
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  */

import java.io.*;

import java.sql.Blob;

public class Main {

    public static byte[] blobToBytes(Blob blob) {
        BufferedInputStream is = null;
        try {
            is = new BufferedInputStream(blob.getBinaryStream());
            byte[] bytes = new byte[(int) blob.length()];
            int len = bytes.length;
            int offset = 0;
            int read = 0;

            while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {
                offset += read;
            }
            return bytes;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                if (is != null) {
                    is.close();
                    is = null;
                }
            } catch (IOException e) {
                return null;
            }
        }
    }
}

Related

  1. blob2FloatArray(Blob blob)
  2. blobToBytes(Blob blob)
  3. getAsString(Blob blob)
  4. getBlobType(byte[] image)
  5. getDataFromBlob(Blob b)
  6. getDataFromBlob(Blob b)