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