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; 024 025import org.apache.oozie.compression.CodecFactory; 026import org.apache.oozie.compression.CompressionCodec; 027import org.apache.oozie.util.ByteArrayUtils; 028 029/** 030 * BinaryBlob to maintain compress and uncompressed data 031 */ 032public class BinaryBlob { 033 034 private byte[] rawBlob; 035 private byte[] bytes; 036 037 /** 038 * Construct a binaryblob 039 * 040 * @param byteArray the source byte array 041 * @param isUncompressed - true if data is uncompressed 042 */ 043 public BinaryBlob(byte[] byteArray, boolean isUncompressed) { 044 if (isUncompressed) { 045 this.bytes = ByteArrayUtils.weakIntern(byteArray); 046 this.rawBlob = null; 047 } 048 else { 049 this.rawBlob = ByteArrayUtils.weakIntern(byteArray); 050 } 051 } 052 053 /** 054 * Set bytes 055 * 056 * @param byteArray the byte array 057 */ 058 public void setBytes(byte[] byteArray) { 059 this.bytes = ByteArrayUtils.weakIntern(byteArray); 060 this.rawBlob = null; 061 } 062 063 /** 064 * Returns a decompressed byte array 065 * 066 * @return byte array 067 */ 068 public byte[] getBytes() { 069 if (bytes != null) { 070 return bytes; 071 } 072 if (rawBlob == null) { 073 return null; 074 } 075 try { 076 DataInputStream dais = new DataInputStream(new ByteArrayInputStream(rawBlob)); 077 CompressionCodec codec = CodecFactory.getDeCompressionCodec(dais); 078 if (codec != null) { 079 bytes = ByteArrayUtils.weakIntern(codec.decompressToBytes(dais)); 080 } 081 else { 082 bytes = ByteArrayUtils.weakIntern(rawBlob); 083 } 084 dais.close(); 085 } 086 catch (IOException ex) { 087 throw new RuntimeException(ex); 088 } 089 rawBlob = null; 090 return bytes; 091 092 } 093 094 /** 095 * Returns a raw blob 096 * 097 * @return raw blob 098 */ 099 public byte[] getRawBlob() { 100 if (rawBlob != null) { 101 return rawBlob; 102 } 103 if (bytes == null) { 104 return null; 105 } 106 if (CodecFactory.isCompressionEnabled()) { 107 byte[] headerBytes = CodecFactory.getHeaderBytes(); 108 try { 109 rawBlob = ByteArrayUtils.weakIntern(CodecFactory.getCompressionCodec().compressBytes(headerBytes, bytes)); 110 } 111 catch (IOException ex) { 112 throw new RuntimeException(ex); 113 } 114 } 115 else { 116 rawBlob = ByteArrayUtils.weakIntern(bytes); 117 } 118 return rawBlob; 119 } 120 121}