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 /** 020 * @author <a href="http://hiramchirino.com">Hiram Chirino</a> 021 */ 022 final public class AsciiBuffer extends Buffer { 023 024 private int hashCode; 025 private String value; 026 027 public AsciiBuffer(Buffer other) { 028 super(other); 029 } 030 031 public AsciiBuffer(byte[] data, int offset, int length) { 032 super(data, offset, length); 033 } 034 035 public AsciiBuffer(byte[] data) { 036 super(data); 037 } 038 039 public AsciiBuffer(String value) { 040 super(encode(value)); 041 this.value = value; 042 } 043 044 /////////////////////////////////////////////////////////////////// 045 // Overrides 046 /////////////////////////////////////////////////////////////////// 047 048 public String toString() 049 { 050 if( value == null ) { 051 value = decode(this); 052 } 053 return value; 054 } 055 056 @Override 057 public boolean equals(Object obj) { 058 if( obj==this ) 059 return true; 060 061 if( obj==null || obj.getClass()!=AsciiBuffer.class ) 062 return false; 063 064 return equals((Buffer)obj); 065 } 066 067 @Override 068 public int hashCode() { 069 if( hashCode==0 ) { 070 hashCode = super.hashCode();; 071 } 072 return hashCode; 073 } 074 075 /////////////////////////////////////////////////////////////////// 076 // Statics 077 /////////////////////////////////////////////////////////////////// 078 079 public static AsciiBuffer ascii(String value) { 080 if( value==null ) { 081 return null; 082 } 083 return new AsciiBuffer(value); 084 } 085 086 public static AsciiBuffer ascii(Buffer buffer) { 087 if( buffer==null ) { 088 return null; 089 } 090 if( buffer.getClass() == AsciiBuffer.class ) { 091 return (AsciiBuffer) buffer; 092 } 093 return new AsciiBuffer(buffer); 094 } 095 096 static public byte[] encode(String value) 097 { 098 int size = value.length(); 099 byte rc[] = new byte[size]; 100 for( int i=0; i < size; i++ ) { 101 rc[i] = (byte)(value.charAt(i)&0xFF); 102 } 103 return rc; 104 } 105 106 static public String decode(Buffer value) 107 { 108 int size = value.getLength(); 109 char rc[] = new char[size]; 110 for( int i=0; i < size; i++ ) { 111 rc[i] = (char)(value.get(i) & 0xFF ); 112 } 113 return new String(rc); 114 } 115 116 }