001package com.hfg.util.io; 002 003import java.io.FilterReader; 004import java.io.IOException; 005import java.io.Reader; 006 007//------------------------------------------------------------------------------ 008/** 009 FilterReader that only allows letters through. 010 <div> 011 @author J. Alex Taylor, hairyfatguy.com 012 </div> 013 */ 014//------------------------------------------------------------------------------ 015// com.hfg Library 016// 017// This library is free software; you can redistribute it and/or 018// modify it under the terms of the GNU Lesser General Public 019// License as published by the Free Software Foundation; either 020// version 2.1 of the License, or (at your option) any later version. 021// 022// This library is distributed in the hope that it will be useful, 023// but WITHOUT ANY WARRANTY; without even the implied warranty of 024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 025// Lesser General Public License for more details. 026// 027// You should have received a copy of the GNU Lesser General Public 028// License along with this library; if not, write to the Free Software 029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 030// 031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 032// jataylor@hairyfatguy.com 033//------------------------------------------------------------------------------ 034 035public class LettersOnlyReader extends FilterReader 036{ 037 private char[] mBuffer = new char[8196]; 038 private int mBufferLimit; 039 private int mBufferIndex; 040 private boolean mEndOfStreamReached; 041 042 //--------------------------------------------------------------------------- 043 public LettersOnlyReader(Reader inReader) 044 { 045 super(inReader); 046 } 047 048 //--------------------------------------------------------------------------- 049 public int read() 050 throws IOException 051 { 052 int returnChar; 053 054 do 055 { 056 returnChar = innerRead(); 057 } 058 while (returnChar >= 0 059 && ! Character.isLetter(returnChar)); 060 061 return returnChar; 062 } 063 064 //--------------------------------------------------------------------------- 065 public int read(char[] inBuffer, int inOffset, int inMaxReadLength) 066 throws IOException 067 { 068 int theChar; 069 int numCharsRead = 0; 070 do 071 { 072 theChar = read(); 073 if (theChar > 0) 074 { 075 inBuffer[inOffset++] = (char) theChar; 076 numCharsRead++; 077 } 078 } 079 while (theChar >= 0 080 && numCharsRead < inMaxReadLength); 081 082 return (theChar < 0 && 0 == numCharsRead ? -1 : numCharsRead); 083 } 084 085 //--------------------------------------------------------------------------- 086 protected int innerRead() 087 throws IOException 088 { 089 if (mBufferIndex >= mBufferLimit) 090 { 091 fillBuffer(); 092 } 093 094 return (mEndOfStreamReached ? -1 : mBuffer[mBufferIndex++]); 095 } 096 097 //--------------------------------------------------------------------------- 098 private void fillBuffer() 099 throws IOException 100 { 101 mBufferLimit = super.in.read(mBuffer, 0, mBuffer.length); 102 103 if (-1 == mBufferLimit) 104 { 105 mEndOfStreamReached = true; 106 } 107 108 // Reset the index 109 mBufferIndex = 0; 110 } 111}