001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.fusesource.hawtbuf; 018 019 import java.io.UnsupportedEncodingException; 020 021 /** 022 * @author <a href="http://hiramchirino.com">Hiram Chirino</a> 023 */ 024 final public class UTF8Buffer extends Buffer { 025 026 int hashCode; 027 String value; 028 029 public UTF8Buffer(Buffer other) { 030 super(other); 031 } 032 033 public UTF8Buffer(byte[] data, int offset, int length) { 034 super(data, offset, length); 035 } 036 037 public UTF8Buffer(byte[] data) { 038 super(data); 039 } 040 041 public UTF8Buffer(String input) { 042 super(encode(input)); 043 } 044 045 /////////////////////////////////////////////////////////////////// 046 // Overrides 047 /////////////////////////////////////////////////////////////////// 048 049 public String toString() 050 { 051 if( value==null ) { 052 value = decode(this); 053 } 054 return value; 055 } 056 057 @Override 058 public int compareTo(Buffer other) { 059 // Do a char comparison.. not a byte for byte comparison. 060 return toString().compareTo(other.toString()); 061 } 062 063 @Override 064 public boolean equals(Object obj) { 065 if( obj==this ) 066 return true; 067 068 if( obj==null || obj.getClass()!=UTF8Buffer.class ) 069 return false; 070 071 return equals((Buffer)obj); 072 } 073 074 @Override 075 public int hashCode() { 076 if( hashCode==0 ) { 077 hashCode = super.hashCode();; 078 } 079 return hashCode; 080 } 081 082 /////////////////////////////////////////////////////////////////// 083 // Statics 084 /////////////////////////////////////////////////////////////////// 085 public static UTF8Buffer utf8(String value) { 086 if( value==null ) { 087 return null; 088 } 089 return new UTF8Buffer(value); 090 } 091 092 public static UTF8Buffer utf8(Buffer buffer) { 093 if( buffer==null ) { 094 return null; 095 } 096 if( buffer.getClass() == UTF8Buffer.class ) { 097 return (UTF8Buffer) buffer; 098 } 099 return new UTF8Buffer(buffer); 100 } 101 102 static public byte[] encode(String value) 103 { 104 try { 105 return value.getBytes("UTF-8"); 106 } catch (UnsupportedEncodingException e) { 107 throw new RuntimeException("A UnsupportedEncodingException was thrown for teh UTF-8 encoding. (This should never happen)"); 108 } 109 } 110 111 static public String decode(Buffer buffer) 112 { 113 try { 114 return new String(buffer.getData(), buffer.getOffset(), buffer.getLength(), "UTF-8"); 115 } catch (UnsupportedEncodingException e) { 116 throw new RuntimeException("A UnsupportedEncodingException was thrown for teh UTF-8 encoding. (This should never happen)"); 117 } 118 } 119 120 121 }