001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.oozie; 020 021import java.io.ByteArrayInputStream; 022import java.io.DataInputStream; 023import java.io.IOException; 024import org.apache.oozie.compression.CodecFactory; 025import org.apache.oozie.compression.CompressionCodec; 026 027/** 028 * BinaryBlob to maintain compress and uncompressed data 029 */ 030public class BinaryBlob { 031 032 private byte[] rawBlob; 033 private byte[] bytes; 034 035 /** 036 * Construct a binaryblob 037 * 038 * @param byteArray the source byte array 039 * @param isUncompressed - true if data is uncompressed 040 */ 041 public BinaryBlob(byte[] byteArray, boolean isUncompressed) { 042 if (isUncompressed) { 043 this.bytes = byteArray; 044 this.rawBlob = null; 045 } 046 else { 047 this.rawBlob = byteArray; 048 } 049 } 050 051 /** 052 * Set bytes 053 * 054 * @param byteArray the byte array 055 */ 056 public void setBytes(byte[] byteArray) { 057 this.bytes = byteArray; 058 this.rawBlob = null; 059 } 060 061 /** 062 * Returns a decompressed byte array 063 * 064 * @return byte array 065 */ 066 public byte[] getBytes() { 067 if (bytes != null) { 068 return bytes; 069 } 070 if (rawBlob == null) { 071 return null; 072 } 073 try { 074 DataInputStream dais = new DataInputStream(new ByteArrayInputStream(rawBlob)); 075 CompressionCodec codec = CodecFactory.getDeCompressionCodec(dais); 076 if (codec != null) { 077 bytes = codec.decompressToBytes(dais); 078 } 079 else { 080 bytes = rawBlob; 081 } 082 dais.close(); 083 } 084 catch (IOException ex) { 085 throw new RuntimeException(ex); 086 } 087 rawBlob = null; 088 return bytes; 089 090 } 091 092 /** 093 * Returns a raw blob 094 * 095 * @return raw blob 096 */ 097 public byte[] getRawBlob() { 098 if (rawBlob != null) { 099 return rawBlob; 100 } 101 if (bytes == null) { 102 return null; 103 } 104 if (CodecFactory.isCompressionEnabled()) { 105 byte[] headerBytes = CodecFactory.getHeaderBytes(); 106 try { 107 rawBlob = CodecFactory.getCompressionCodec().compressBytes(headerBytes, bytes); 108 } 109 catch (IOException ex) { 110 throw new RuntimeException(ex); 111 } 112 } 113 else { 114 rawBlob = bytes; 115 } 116 return rawBlob; 117 } 118 119}