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