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