DBLargeObject.java
/*
* Copyright 2013 Gregory Graham.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nz.co.gregs.dbvolution.datatypes;
import java.io.InputStream;
import java.text.DecimalFormat;
import nz.co.gregs.dbvolution.results.LargeObjectResult;
/**
* Encapsulates database values that are large, vague objects.
*
* <p>
* Use a DBLargeObject subtype when the column is a {@code BLOB}, {@code CLOB},
* {@code TEXT}, {@code JavaObject}, or similar datatype.
*
* <p>
* Mostly you should use {@link DBLargeBinary} or {@link DBLargeText} though
* there should be other more specific classes eventually. There is also
* {@link DBJavaObject} for storing Java objects directly in the database.
*
* <p style="color: #F90;">Support DBvolution at
* <a href="http://patreon.com/dbvolution" target=new>Patreon</a></p>
*
* @author Gregory Graham
* @param <T> the Java type of the value to be represented by this BLOB QDT
*/
public abstract class DBLargeObject<T> extends QueryableDatatype<T> implements LargeObjectResult<T> {
private static final long serialVersionUID = 1L;
/**
* The default constructor for DBLargeObject.
*
* <p>
* Creates an unset undefined DBLargeObject object.
*
*/
public DBLargeObject() {
super();
}
/**
* The column expression constructor for DBLargeObject.
*
* <p>
* Creates DBLargeObject that will be generated by the database at query time.
*
* @param blobExpression the expression to be evaluated during the query
*/
public DBLargeObject(LargeObjectResult<T> blobExpression) {
super(blobExpression);
}
/**
* The column expression constructor for DBLargeObject.
*
* <p>
* Creates DBLargeObject that will be generated by the database at query time.
*
* @param blobExpression the expression to be evaluated during the query
*/
public DBLargeObject(T blobExpression) {
super(blobExpression);
}
/**
* Returns the internal InputStream.
*
* <p style="color: #F90;">Support DBvolution at
* <a href="http://patreon.com/dbvolution" target=new>Patreon</a></p>
*
* @return an InputStream to read the bytes.
*/
public abstract InputStream getInputStream();
/**
*
* <p style="color: #F90;">Support DBvolution at
* <a href="http://patreon.com/dbvolution" target=new>Patreon</a></p>
*
* @return the size of the Large Object as an int
*/
public abstract int getSize();
public String getSizeAsReadableString() {
int size = getSize();
if (size <= 0) {
return "0";
}
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups))
+ " " + units[digitGroups];
}
@Override
public String toString() {
return "/*BINARY DATA*/";
}
// @Override
// public DBLargeObject<T> copy() {
// return (DBLargeObject<T>) super.copy();
// }
@Override
public boolean isLargeObject() {
return true;
}
}