1 package org.apache.archiva.metadata.repository.cassandra.model;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import com.netflix.astyanax.serializers.AbstractSerializer;
24 import com.netflix.astyanax.serializers.ComparatorType;
25 import org.apache.cassandra.db.marshal.UTF8Type;
26 import org.apache.commons.codec.binary.StringUtils;
27 import org.apache.commons.io.IOUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.IOException;
34 import java.nio.ByteBuffer;
35 import java.nio.charset.Charset;
36 import java.util.zip.Deflater;
37 import java.util.zip.DeflaterInputStream;
38 import java.util.zip.DeflaterOutputStream;
39 import java.util.zip.InflaterInputStream;
43 * For Huge String we use a compression
44 * @author Olivier Lamy
46 public class HugeStringSerializer
47 extends AbstractSerializer<String>
50 private Logger logger = LoggerFactory.getLogger( getClass() );
52 private static final String UTF_8 = "UTF-8";
54 private static final HugeStringSerializer instance = new HugeStringSerializer();
56 private static final Charset charset = Charset.forName( UTF_8 );
58 public static HugeStringSerializer get()
64 public ByteBuffer toByteBuffer( String obj )
73 byte[] bytes = compressWithDeflate( StringUtils.getBytesUtf8( obj ) );
74 return ByteBuffer.wrap( bytes );
76 catch ( IOException e )
78 throw new RuntimeException( "Fail to compress column data", e );
83 public String fromByteBuffer( ByteBuffer byteBuffer )
85 if ( byteBuffer == null )
90 ByteBuffer dup = byteBuffer.duplicate();
93 String str = getFromDeflateBytes( dup.array() );
96 catch ( IOException e )
98 throw new RuntimeException( "Fail to decompress column data", e );
103 public String getFromDeflateBytes( byte[] bytes )
106 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( bytes );
107 InflaterInputStream inflaterInputStream = new InflaterInputStream( byteArrayInputStream );
108 return IOUtils.toString( inflaterInputStream );
111 public byte[] compressWithDeflate( byte[] unCompress )
116 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
117 DeflaterOutputStream out = new DeflaterOutputStream( buffer, new Deflater( Deflater.BEST_COMPRESSION ) );
118 out.write( unCompress );
120 ByteArrayInputStream bais = new ByteArrayInputStream( buffer.toByteArray() );
121 byte[] res = IOUtils.toByteArray( bais );
124 catch ( IOException e )
126 logger.debug( "IOException in compressStringWithDeflate", e );
133 public ComparatorType getComparatorType()
135 return ComparatorType.BYTESTYPE;
139 public ByteBuffer fromString( String str )
141 return instance.fromString( str );
145 public String getString( ByteBuffer byteBuffer )
147 return instance.getString( byteBuffer );
151 private static final String UTF_8 = "UTF-8";
152 private static final HugeStringSerializer instance = new HugeStringSerializer();
153 private static final Charset charset = Charset.forName(UTF_8);
155 public static HugeStringSerializer get() {
160 public ByteBuffer toByteBuffer(String obj) {
164 return ByteBuffer.wrap(obj.getBytes(charset));
168 public String fromByteBuffer(ByteBuffer byteBuffer) {
169 if (byteBuffer == null) {
172 final ByteBuffer dup = byteBuffer.duplicate();
173 return charset.decode(dup).toString();
177 public ComparatorType getComparatorType() {
178 return ComparatorType.UTF8TYPE;
182 public ByteBuffer fromString(String str) {
183 return UTF8Type.instance.fromString(str);
187 public String getString(ByteBuffer byteBuffer) {
188 return UTF8Type.instance.getString(byteBuffer);