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