001/* 002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Bogdan Stefanescu 018 * Florent Guillaume 019 */ 020package org.nuxeo.ecm.core.api.impl.blob; 021 022import java.io.ByteArrayInputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.Serializable; 026 027/** 028 * Blob based on a byte array. 029 */ 030public class ByteArrayBlob extends AbstractBlob implements Serializable { 031 032 private static final long serialVersionUID = 1L; 033 034 protected final byte[] bytes; 035 036 public ByteArrayBlob(byte[] bytes) { 037 this(bytes, null, null); 038 } 039 040 public ByteArrayBlob(byte[] bytes, String mimeType) { 041 this(bytes, mimeType, null); 042 } 043 044 public ByteArrayBlob(byte[] bytes, String mimeType, String encoding) { 045 if (bytes == null) { 046 throw new NullPointerException("null bytes"); 047 } 048 this.bytes = bytes; 049 this.mimeType = mimeType; 050 this.encoding = encoding; 051 } 052 053 @Override 054 public long getLength() { 055 return bytes.length; 056 } 057 058 @Override 059 public InputStream getStream() { 060 return new ByteArrayInputStream(bytes); 061 } 062 063 @Override 064 public byte[] getByteArray() { 065 return bytes; 066 } 067 068 @Override 069 public String getString() throws IOException { 070 return new String(bytes, getEncoding() == null ? UTF_8 : getEncoding()); 071 } 072 073}