|
| 1 | +/***** BEGIN LICENSE BLOCK ***** |
| 2 | + * Version: EPL 1.0/GPL 2.0/LGPL 2.1 |
| 3 | + * |
| 4 | + * The contents of this file are subject to the Eclipse Public |
| 5 | + * License Version 1.0 (the "License"); you may not use this file |
| 6 | + * except in compliance with the License. You may obtain a copy of |
| 7 | + * the License at http://www.eclipse.org/legal/epl-v10.html |
| 8 | + * |
| 9 | + * Software distributed under the License is distributed on an "AS |
| 10 | + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| 11 | + * implied. See the License for the specific language governing |
| 12 | + * rights and limitations under the License. |
| 13 | + * |
| 14 | + * Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de> |
| 15 | + * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se> |
| 16 | + * Copyright (C) 2003-2004 Thomas E Enebo <enebo@acm.org> |
| 17 | + * Copyright (C) 2004 Charles O Nutter <headius@headius.com> |
| 18 | + * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de> |
| 19 | + * Copyright (C) 2005 Derek Berner <derek.berner@state.nm.us> |
| 20 | + * Copyright (C) 2006 Evan Buswell <ebuswell@gmail.com> |
| 21 | + * Copyright (C) 2007 Nick Sieger <nicksieger@gmail.com> |
| 22 | + * Copyright (C) 2009 Joseph LaFata <joe@quibb.org> |
| 23 | + * |
| 24 | + * Alternatively, the contents of this file may be used under the terms of |
| 25 | + * either of the GNU General Public License Version 2 or later (the "GPL"), |
| 26 | + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 27 | + * in which case the provisions of the GPL or the LGPL are applicable instead |
| 28 | + * of those above. If you wish to allow use of your version of this file only |
| 29 | + * under the terms of either the GPL or the LGPL, and not to allow others to |
| 30 | + * use your version of this file under the terms of the EPL, indicate your |
| 31 | + * decision by deleting the provisions above and replace them with the notice |
| 32 | + * and other provisions required by the GPL or the LGPL. If you do not delete |
| 33 | + * the provisions above, a recipient may use your version of this file under |
| 34 | + * the terms of any one of the EPL, the GPL or the LGPL. |
| 35 | + ***** END LICENSE BLOCK *****/ |
| 36 | +package org.jruby.util; |
| 37 | + |
| 38 | +public class PackUtils { |
| 39 | + |
| 40 | + private static final byte[] hex_table; |
| 41 | + |
| 42 | + static { |
| 43 | + hex_table = ByteList.plain("0123456789ABCDEF"); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * encodes a String with the Quoted printable, MIME encoding (see RFC2045). |
| 48 | + * appends the result of the encoding in a StringBuffer |
| 49 | + * @param io2Append The StringBuffer which should receive the result |
| 50 | + * @param i2Encode The String to encode |
| 51 | + * @param iLength The max number of characters to encode |
| 52 | + * @return the io2Append buffer |
| 53 | + **/ |
| 54 | + public static ByteList qpencode(ByteList io2Append, ByteList i2Encode, int iLength) { |
| 55 | + io2Append.ensure(1024); |
| 56 | + int lCurLineLength = 0; |
| 57 | + int lPrevChar = -1; |
| 58 | + byte[] l2Encode = i2Encode.getUnsafeBytes(); |
| 59 | + try { |
| 60 | + int end = i2Encode.getBegin() + i2Encode.getRealSize(); |
| 61 | + for (int i = i2Encode.getBegin(); i < end; i++) { |
| 62 | + int lCurChar = l2Encode[i] & 0xff; |
| 63 | + if (lCurChar > 126 || (lCurChar < 32 && lCurChar != '\n' && lCurChar != '\t') || lCurChar == '=') { |
| 64 | + io2Append.append('='); |
| 65 | + io2Append.append(hex_table[lCurChar >>> 4]); |
| 66 | + io2Append.append(hex_table[lCurChar & 0x0f]); |
| 67 | + lCurLineLength += 3; |
| 68 | + lPrevChar = -1; |
| 69 | + } else if (lCurChar == '\n') { |
| 70 | + if (lPrevChar == ' ' || lPrevChar == '\t') { |
| 71 | + io2Append.append('='); |
| 72 | + io2Append.append(lCurChar); |
| 73 | + } |
| 74 | + io2Append.append(lCurChar); |
| 75 | + lCurLineLength = 0; |
| 76 | + lPrevChar = lCurChar; |
| 77 | + } else { |
| 78 | + io2Append.append(lCurChar); |
| 79 | + lCurLineLength++; |
| 80 | + lPrevChar = lCurChar; |
| 81 | + } |
| 82 | + if (lCurLineLength > iLength) { |
| 83 | + io2Append.append('='); |
| 84 | + io2Append.append('\n'); |
| 85 | + lCurLineLength = 0; |
| 86 | + lPrevChar = '\n'; |
| 87 | + } |
| 88 | + } |
| 89 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 90 | + //normal exit, this should be faster than a test at each iterations for string with more than |
| 91 | + //about 40 char |
| 92 | + } |
| 93 | + |
| 94 | + if (lCurLineLength > 0) { |
| 95 | + io2Append.append('='); |
| 96 | + io2Append.append('\n'); |
| 97 | + } |
| 98 | + return io2Append; |
| 99 | + } |
| 100 | +} |
0 commit comments