Frames | No Frames |
1: /* ByteCharset.java -- Abstract class for generic 1-byte encodings. 2: Copyright (C) 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: package gnu.java.nio.charset; 39: 40: import java.nio.ByteBuffer; 41: import java.nio.CharBuffer; 42: import java.nio.charset.CoderResult; 43: 44: /** 45: * Helper class to deal with encoding loops that write a byte at a time 46: * 47: * @author Ian Rogers 48: */ 49: public abstract class ByteEncodeLoopHelper 50: { 51: /** 52: * @return can the given character be encoded 53: */ 54: protected abstract boolean isMappable(char c); 55: 56: /** 57: * Map the given character to a byte, the given character is guaranteed to be 58: * mappable 59: */ 60: protected abstract byte mapToByte(char c); 61: 62: /** 63: * Encodes one or more characters into one or more bytes, mapping each 64: * character to only one byte 65: * 66: * @param in character buffer to read from 67: * @param out byte buffer to write to 68: * @return the result state of the encoder 69: */ 70: CoderResult encodeLoop(CharBuffer in, ByteBuffer out) 71: { 72: if (in.hasArray() && out.hasArray()) 73: { 74: return arrayEncodeLoop(in, out); 75: } else 76: { 77: return normalEncodeLoop(in, out); 78: } 79: } 80: 81: /** 82: * Encode loop using get and put operations 83: */ 84: private CoderResult normalEncodeLoop(CharBuffer in, ByteBuffer out) 85: { 86: int outRemaining = out.remaining(); 87: int inRemaining = in.remaining(); 88: while (inRemaining > 0 && outRemaining > 0) 89: { 90: char c = in.get(); 91: inRemaining--; 92: 93: if (!isMappable(c)) 94: { 95: in.position(in.position() - 1); 96: return CoderResult.unmappableForLength(1); 97: } 98: byte b = mapToByte(c); 99: out.put(b); 100: outRemaining--; 101: } 102: if (inRemaining > 0) 103: { 104: return CoderResult.OVERFLOW; 105: } else 106: { 107: return CoderResult.UNDERFLOW; 108: } 109: } 110: 111: /** 112: * Encode loop using array read and write operations 113: */ 114: private CoderResult arrayEncodeLoop(CharBuffer in, ByteBuffer out) 115: { 116: char[] inArray = in.array(); 117: byte[] outArray = out.array(); 118: int inPos = in.arrayOffset() + in.position(); 119: int outPos = out.arrayOffset() + out.position(); 120: int inRemaining = in.remaining(); 121: int outRemaining = out.remaining(); 122: CoderResult result; 123: 124: bailOut: 125: if (inRemaining <= outRemaining) 126: { 127: for (int i = 0; i < inRemaining; i++) 128: { 129: char inChar = inArray[inPos]; 130: inPos++; 131: if (!isMappable(inChar)) 132: { 133: inPos--; 134: result = CoderResult.unmappableForLength(1); 135: break bailOut; 136: } 137: byte b = mapToByte(inChar); 138: outArray[outPos] = b; 139: outPos++; 140: } 141: result = CoderResult.UNDERFLOW; 142: } 143: else 144: { 145: for (int i = 0; i < outRemaining; i++) 146: { 147: char inChar = inArray[inPos]; 148: inPos++; 149: if (!isMappable(inChar)) 150: { 151: inPos--; 152: result = CoderResult.unmappableForLength(1); 153: break bailOut; 154: } 155: byte b = mapToByte(inChar); 156: outArray[outPos] = b; 157: outPos++; 158: } 159: result = CoderResult.OVERFLOW; 160: } 161: in.position(inPos - in.arrayOffset()); 162: out.position(outPos - out.arrayOffset()); 163: return result; 164: } 165: }