Author: olegk Date: Wed Sep 24 12:21:07 2008 New Revision: 698697 URL: http://svn.apache.org/viewvc?rev=698697&view=rev Log: Decoupled CodecUtil and LineBreakingOutputStream; Factored Base64Encoder into a separate class
Added: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/Base64Encoder.java (with props) Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/CodecUtil.java james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/LineBreakingOutputStream.java Added: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/Base64Encoder.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/Base64Encoder.java?rev=698697&view=auto ============================================================================== --- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/Base64Encoder.java (added) +++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/Base64Encoder.java Wed Sep 24 12:21:07 2008 @@ -0,0 +1,123 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mime4j.decoder; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +final class Base64Encoder { + private static final int MASK = 0x3F; + private static final int FIRST_MASK = MASK << 18; + private static final int SECOND_MASK = MASK << 12; + private static final int THIRD_MASK = MASK << 6; + private static final int FORTH_MASK = MASK; + + private static final byte[] ENCODING = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', + 'O', 'P' ,'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', + 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/'}; + + private final byte[] in; + private final byte[] out; + + public Base64Encoder(final int inputBufferSize) { + in = new byte[inputBufferSize]; + int outputBufferSize = ((int) Math.floor((4*inputBufferSize) / 3f) + 3); + outputBufferSize = outputBufferSize + 2 * (int) Math.floor(outputBufferSize / 76f); + out = new byte[outputBufferSize]; + } + + public void encode(final InputStream inStream, final OutputStream outStream) throws IOException { + int inputLength; + while ((inputLength = inStream.read(in)) > -1) { + int outputLength = encodeInputBuffer(in, 0, inputLength); + if (outputLength > 0) { + outStream.write(out, 0, outputLength); + } + } + } + + private int encodeInputBuffer(byte[] in, final int pos, final int inputLength) { + if (inputLength == 0) { + return 0; + } + int inputEnd = pos + inputLength; + int inputIndex = pos; + int outputIndex = 0; + while (inputEnd - inputIndex > 2) { + int one = (toInt(in[inputIndex++]) << 16); + int two = (toInt(in[inputIndex++]) << 8); + int three = toInt(in[inputIndex++]); + int quantum = one | two | three; + int index = (quantum & FIRST_MASK) >> 18; + outputIndex = setResult(out, outputIndex, ENCODING[index]); + index = (quantum & SECOND_MASK) >> 12; + outputIndex = setResult(out, outputIndex, ENCODING[index]); + index = (quantum & THIRD_MASK) >> 6; + outputIndex = setResult(out, outputIndex, ENCODING[index]); + index = (quantum & FORTH_MASK); + outputIndex = setResult(out, outputIndex, ENCODING[index]); + } + + switch (inputEnd - inputIndex) { + case 1: + int quantum = in[inputIndex++] << 16; + int index = (quantum & FIRST_MASK) >> 18; + outputIndex = setResult(out, outputIndex, ENCODING[index]); + index = (quantum & SECOND_MASK) >> 12; + outputIndex = setResult(out, outputIndex, ENCODING[index]); + outputIndex = setResult(out, outputIndex, (byte) '='); + outputIndex = setResult(out, outputIndex, (byte) '='); + break; + + case 2: + quantum = (in[inputIndex++] << 16) + (in[inputIndex++] << 8); + index = (quantum & FIRST_MASK) >> 18; + outputIndex = setResult(out, outputIndex, ENCODING[index]); + index = (quantum & SECOND_MASK) >> 12; + outputIndex = setResult(out, outputIndex, ENCODING[index]); + index = (quantum & THIRD_MASK) >> 6; + outputIndex = setResult(out, outputIndex, ENCODING[index]); + outputIndex = setResult(out, outputIndex, (byte) '='); + break; + } + + return outputIndex; + } + + private int toInt(byte b) { + return 255 & b; + } + + private int setResult(byte[] results, int outputIndex, byte value) { + results[outputIndex++] = value; + outputIndex = checkLineLength(results, outputIndex); + return outputIndex; + } + + private int checkLineLength(byte[] results, int outputIndex) { + if (outputIndex == 76 || outputIndex > 76 && (outputIndex - 2*Math.floor(outputIndex/76f - 1)) % 76 == 0) { + results[outputIndex++] = '\r'; + results[outputIndex++] = '\n'; + } + return outputIndex; + } +} \ No newline at end of file Propchange: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/Base64Encoder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/Base64Encoder.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/Base64Encoder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/CodecUtil.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/CodecUtil.java?rev=698697&r1=698696&r2=698697&view=diff ============================================================================== --- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/CodecUtil.java (original) +++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/CodecUtil.java Wed Sep 24 12:21:07 2008 @@ -29,11 +29,6 @@ */ public class CodecUtil { - - public static final byte[] CRLF = {'\r', '\n'}; - - public static final byte[] CRLF_CRLF = {'\r', '\n', '\r', '\n'}; - static final int DEFAULT_ENCODING_BUFFER_SIZE = 1024; /** @@ -102,104 +97,5 @@ public static OutputStream wrapBase64(final OutputStream out) throws IOException { return new Base64OutputStream(out); } - - private static final class Base64Encoder { - private static final int MASK = 0x3F; - private static final int FIRST_MASK = MASK << 18; - private static final int SECOND_MASK = MASK << 12; - private static final int THIRD_MASK = MASK << 6; - private static final int FORTH_MASK = MASK; - - private static final byte[] ENCODING = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', - 'O', 'P' ,'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', - 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', - '4', '5', '6', '7', '8', '9', '+', '/'}; - - private final byte[] in; - private final byte[] out; - - public Base64Encoder(final int inputBufferSize) { - in = new byte[inputBufferSize]; - int outputBufferSize = ((int) Math.floor((4*inputBufferSize) / 3f) + 3); - outputBufferSize = outputBufferSize + 2 * (int) Math.floor(outputBufferSize / 76f); - out = new byte[outputBufferSize]; - } - - public void encode(final InputStream inStream, final OutputStream outStream) throws IOException { - int inputLength; - while ((inputLength = inStream.read(in)) > -1) { - int outputLength = encodeInputBuffer(in, 0, inputLength); - if (outputLength > 0) { - outStream.write(out, 0, outputLength); - } - } - } - - private int encodeInputBuffer(byte[] in, final int pos, final int inputLength) { - if (inputLength == 0) { - return 0; - } - int inputEnd = pos + inputLength; - int inputIndex = pos; - int outputIndex = 0; - while (inputEnd - inputIndex > 2) { - int one = (toInt(in[inputIndex++]) << 16); - int two = (toInt(in[inputIndex++]) << 8); - int three = toInt(in[inputIndex++]); - int quantum = one | two | three; - int index = (quantum & FIRST_MASK) >> 18; - outputIndex = setResult(out, outputIndex, ENCODING[index]); - index = (quantum & SECOND_MASK) >> 12; - outputIndex = setResult(out, outputIndex, ENCODING[index]); - index = (quantum & THIRD_MASK) >> 6; - outputIndex = setResult(out, outputIndex, ENCODING[index]); - index = (quantum & FORTH_MASK); - outputIndex = setResult(out, outputIndex, ENCODING[index]); - } - - switch (inputEnd - inputIndex) { - case 1: - int quantum = in[inputIndex++] << 16; - int index = (quantum & FIRST_MASK) >> 18; - outputIndex = setResult(out, outputIndex, ENCODING[index]); - index = (quantum & SECOND_MASK) >> 12; - outputIndex = setResult(out, outputIndex, ENCODING[index]); - outputIndex = setResult(out, outputIndex, (byte) '='); - outputIndex = setResult(out, outputIndex, (byte) '='); - break; - - case 2: - quantum = (in[inputIndex++] << 16) + (in[inputIndex++] << 8); - index = (quantum & FIRST_MASK) >> 18; - outputIndex = setResult(out, outputIndex, ENCODING[index]); - index = (quantum & SECOND_MASK) >> 12; - outputIndex = setResult(out, outputIndex, ENCODING[index]); - index = (quantum & THIRD_MASK) >> 6; - outputIndex = setResult(out, outputIndex, ENCODING[index]); - outputIndex = setResult(out, outputIndex, (byte) '='); - break; - } - - return outputIndex; - } - - private int toInt(byte b) { - return 255 & b; - } - - private int setResult(byte[] results, int outputIndex, byte value) { - results[outputIndex++] = value; - outputIndex = checkLineLength(results, outputIndex); - return outputIndex; - } - - private int checkLineLength(byte[] results, int outputIndex) { - if (outputIndex == 76 || outputIndex > 76 && (outputIndex - 2*Math.floor(outputIndex/76f - 1)) % 76 == 0) { - results[outputIndex++] = '\r'; - results[outputIndex++] = '\n'; - } - return outputIndex; - } - } } Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/LineBreakingOutputStream.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/LineBreakingOutputStream.java?rev=698697&r1=698696&r2=698697&view=diff ============================================================================== --- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/LineBreakingOutputStream.java (original) +++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/decoder/LineBreakingOutputStream.java Wed Sep 24 12:21:07 2008 @@ -32,6 +32,8 @@ */ public class LineBreakingOutputStream extends FilterOutputStream { + private static final byte[] CRLF = {'\r', '\n'}; + private int linepos = 0; private int lineLength = 76; @@ -49,7 +51,7 @@ off += count; len -= count; } - out.write(CodecUtil.CRLF); + out.write(CRLF); linepos = 0; } else { out.write(b, off, len); @@ -61,7 +63,7 @@ public final void write(final int b) throws IOException { if (linepos >= lineLength) { - out.write(CodecUtil.CRLF); + out.write(CRLF); linepos = 0; } out.write(b); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]