Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / gnu / java / nio / charset / ByteEncodeLoopHelper.java
blob5f703b195f2e78140652ce94c2762b98fb069bbf
1 /* ByteCharset.java -- Abstract class for generic 1-byte encodings.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package gnu.java.nio.charset;
40 import java.nio.ByteBuffer;
41 import java.nio.CharBuffer;
42 import java.nio.charset.CoderResult;
44 /**
45 * Helper class to deal with encoding loops that write a byte at a time
47 * @author Ian Rogers
49 public abstract class ByteEncodeLoopHelper
51 /**
52 * @return can the given character be encoded
54 protected abstract boolean isMappable(char c);
56 /**
57 * Map the given character to a byte, the given character is guaranteed to be
58 * mappable
60 protected abstract byte mapToByte(char c);
62 /**
63 * Encodes one or more characters into one or more bytes, mapping each
64 * character to only one byte
66 * @param in character buffer to read from
67 * @param out byte buffer to write to
68 * @return the result state of the encoder
70 CoderResult encodeLoop(CharBuffer in, ByteBuffer out)
72 if (in.hasArray() && out.hasArray())
74 return arrayEncodeLoop(in, out);
75 } else
77 return normalEncodeLoop(in, out);
81 /**
82 * Encode loop using get and put operations
84 private CoderResult normalEncodeLoop(CharBuffer in, ByteBuffer out)
86 int outRemaining = out.remaining();
87 int inRemaining = in.remaining();
88 while (inRemaining > 0 && outRemaining > 0)
90 char c = in.get();
91 inRemaining--;
93 if (!isMappable(c))
95 in.position(in.position() - 1);
96 return CoderResult.unmappableForLength(1);
98 byte b = mapToByte(c);
99 out.put(b);
100 outRemaining--;
102 if (inRemaining > 0)
104 return CoderResult.OVERFLOW;
105 } else
107 return CoderResult.UNDERFLOW;
112 * Encode loop using array read and write operations
114 private CoderResult arrayEncodeLoop(CharBuffer in, ByteBuffer out)
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 if (inRemaining <= outRemaining)
125 for (int i = 0; i < inRemaining; i++)
127 char inChar = inArray[inPos];
128 inPos++;
129 if (!isMappable(inChar))
131 inPos--;
132 result = CoderResult.unmappableForLength(1);
133 break;
135 byte b = mapToByte(inChar);
136 outArray[outPos] = b;
137 outPos++;
139 result = CoderResult.UNDERFLOW;
141 else
143 for (int i = 0; i < outRemaining; i++)
145 char inChar = inArray[inPos];
146 inPos++;
147 if (!isMappable(inChar))
149 inPos--;
150 result = CoderResult.unmappableForLength(1);
151 break;
153 byte b = mapToByte(inChar);
154 outArray[outPos] = b;
155 outPos++;
157 result = CoderResult.OVERFLOW;
159 in.position(inPos - in.arrayOffset());
160 out.position(outPos - out.arrayOffset());
161 return result;