You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2008/07/15 13:20:40 UTC
svn commit: r676874 - in
/james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j:
BufferedLineReaderInputStream.java LineReaderInputStream.java
LineReaderInputStreamAdaptor.java MimeBoundaryInputStream.java
MimeEntity.java
Author: bago
Date: Tue Jul 15 04:20:38 2008
New Revision: 676874
URL: http://svn.apache.org/viewvc?rev=676874&view=rev
Log:
Remove .reset() hack from the parsing stream classes tree (MIME4J-56)
Patch provided by Oleg Kalnichevski.
Modified:
james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/BufferedLineReaderInputStream.java
james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStream.java
james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStreamAdaptor.java
james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeBoundaryInputStream.java
james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeEntity.java
Modified: james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/BufferedLineReaderInputStream.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/BufferedLineReaderInputStream.java?rev=676874&r1=676873&r2=676874&view=diff
==============================================================================
--- james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/BufferedLineReaderInputStream.java (original)
+++ james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/BufferedLineReaderInputStream.java Tue Jul 15 04:20:38 2008
@@ -1,286 +1,283 @@
-/****************************************************************
- * 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;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * Input buffer that can be used to search for patterns using Quick Search
- * algorithm in data read from an {@link InputStream}.
- */
-public class BufferedLineReaderInputStream extends LineReaderInputStream {
-
- private final byte[] buffer;
-
- private int bufpos;
- private int buflen;
-
- public BufferedLineReaderInputStream(final InputStream instream, int buffersize) {
- super(instream);
- if (instream == null) {
- throw new IllegalArgumentException("Input stream may not be null");
- }
- if (buffersize <= 0) {
- throw new IllegalArgumentException("Buffer size may not be negative or zero");
- }
- this.buffer = new byte[buffersize];
- this.bufpos = 0;
- this.buflen = 0;
- }
-
- public int fillBuffer() throws IOException {
- // compact the buffer if necessary
- if (this.bufpos > 0) {
- int len = this.buflen - this.bufpos;
- if (len > 0) {
- System.arraycopy(this.buffer, this.bufpos, this.buffer, 0, len);
- }
- this.bufpos = 0;
- this.buflen = len;
- }
- int l;
- int off = this.buflen;
- int len = this.buffer.length - off;
- l = in.read(this.buffer, off, len);
- if (l == -1) {
- return -1;
- } else {
- this.buflen = off + l;
- return l;
- }
- }
-
- public boolean hasBufferedData() {
- return this.bufpos < this.buflen;
- }
-
- public int read() throws IOException {
- int noRead = 0;
- while (!hasBufferedData()) {
- noRead = fillBuffer();
- if (noRead == -1) {
- return -1;
- }
- }
- return this.buffer[this.bufpos++] & 0xff;
- }
-
- public int read(final byte[] b, int off, int len) throws IOException {
- if (b == null) {
- return 0;
- }
- int noRead = 0;
- while (!hasBufferedData()) {
- noRead = fillBuffer();
- if (noRead == -1) {
- return -1;
- }
- }
- int chunk = this.buflen - this.bufpos;
- if (chunk > len) {
- chunk = len;
- }
- System.arraycopy(this.buffer, this.bufpos, b, off, chunk);
- this.bufpos += chunk;
- return chunk;
- }
-
- public int read(final byte[] b) throws IOException {
- if (b == null) {
- return 0;
- }
- return read(b, 0, b.length);
- }
-
- public boolean markSupported() {
- return false;
- }
-
-
- public int readLine(final ByteArrayBuffer linebuf) throws IOException {
- if (linebuf == null) {
- throw new IllegalArgumentException("Buffer may not be null");
- }
- int total = 0;
- boolean found = false;
- int bytesRead = 0;
- while (!found) {
- if (!hasBufferedData()) {
- bytesRead = fillBuffer();
- if (bytesRead == -1) {
- break;
- }
- }
- int i = indexOf((byte)'\n');
- int chunk;
- if (i != -1) {
- found = true;
- chunk = i + 1 - pos();
- } else {
- chunk = length();
- }
- if (chunk > 0) {
- linebuf.append(buf(), pos(), chunk);
- skip(chunk);
- total += chunk;
- }
- }
- if (total == 0 && bytesRead == -1) {
- return -1;
- } else {
- return total;
- }
- }
-
- /**
- * Implements quick search algorithm as published by
- * <p>
- * SUNDAY D.M., 1990,
- * A very fast substring search algorithm,
- * Communications of the ACM . 33(8):132-142.
- * </p>
- */
- public int indexOf(final byte[] pattern, int off, int len) {
- if (pattern == null) {
- throw new IllegalArgumentException("Pattern may not be null");
- }
- if (off < this.bufpos || len < 0 || off + len > this.buflen) {
- throw new IndexOutOfBoundsException();
- }
- if (len < pattern.length) {
- return -1;
- }
-
- int[] shiftTable = new int[255];
- for (int i = 0; i < shiftTable.length; i++) {
- shiftTable[i] = pattern.length + 1;
- }
- for (int i = 0; i < pattern.length; i++) {
- int x = pattern[i] & 0xff;
- shiftTable[x] = pattern.length - i;
- }
-
- int j = 0;
- while (j <= len - pattern.length) {
- int cur = this.bufpos + j;
- boolean match = true;
- for (int i = 0; i < pattern.length; i++) {
- if (this.buffer[cur + i] != pattern[i]) {
- match = false;
- break;
- }
- }
- if (match) {
- return cur;
- }
-
- int pos = cur + pattern.length;
- if (pos >= this.buffer.length) {
- break;
- }
- int x = this.buffer[pos] & 0xff;
- j += shiftTable[x];
- }
- return -1;
- }
-
- /**
- * Implements quick search algorithm as published by
- * <p>
- * SUNDAY D.M., 1990,
- * A very fast substring search algorithm,
- * Communications of the ACM . 33(8):132-142.
- * </p>
- */
- public int indexOf(final byte[] pattern) {
- return indexOf(pattern, this.bufpos, this.buflen - this.bufpos);
- }
-
- public int indexOf(byte b, int off, int len) {
- if (off < this.bufpos || len < 0 || off + len > this.buflen) {
- throw new IndexOutOfBoundsException();
- }
- for (int i = off; i < off + len; i++) {
- if (this.buffer[i] == b) {
- return i;
- }
- }
- return -1;
- }
-
- public int indexOf(byte b) {
- return indexOf(b, this.bufpos, this.buflen - this.bufpos);
- }
-
- public byte charAt(int pos) {
- if (pos < this.bufpos || pos > this.buflen) {
- throw new IndexOutOfBoundsException();
- }
- return this.buffer[pos];
- }
-
- public byte[] buf() {
- return this.buffer;
- }
-
- public int pos() {
- return this.bufpos;
- }
-
- public int limit() {
- return this.buflen;
- }
-
- public int length() {
- return this.buflen - this.bufpos;
- }
-
- public int skip(int n) {
- int chunk = Math.min(n, this.buflen - this.bufpos);
- this.bufpos += chunk;
- return chunk;
- }
-
- public void clear() {
- this.bufpos = 0;
- this.buflen = 0;
- }
-
- public String toString() {
- StringBuffer buffer = new StringBuffer();
- buffer.append("[pos: ");
- buffer.append(this.bufpos);
- buffer.append("]");
- buffer.append("[limit: ");
- buffer.append(this.buflen);
- buffer.append("]");
- buffer.append("[");
- for (int i = this.bufpos; i < this.buflen; i++) {
- buffer.append((char) this.buffer[i]);
- }
- buffer.append("]");
- return buffer.toString();
- }
-
- public void reset() {
- }
-
-}
+/****************************************************************
+ * 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;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Input buffer that can be used to search for patterns using Quick Search
+ * algorithm in data read from an {@link InputStream}.
+ */
+public class BufferedLineReaderInputStream extends LineReaderInputStream {
+
+ private final byte[] buffer;
+
+ private int bufpos;
+ private int buflen;
+
+ public BufferedLineReaderInputStream(final InputStream instream, int buffersize) {
+ super(instream);
+ if (instream == null) {
+ throw new IllegalArgumentException("Input stream may not be null");
+ }
+ if (buffersize <= 0) {
+ throw new IllegalArgumentException("Buffer size may not be negative or zero");
+ }
+ this.buffer = new byte[buffersize];
+ this.bufpos = 0;
+ this.buflen = 0;
+ }
+
+ public int fillBuffer() throws IOException {
+ // compact the buffer if necessary
+ if (this.bufpos > 0) {
+ int len = this.buflen - this.bufpos;
+ if (len > 0) {
+ System.arraycopy(this.buffer, this.bufpos, this.buffer, 0, len);
+ }
+ this.bufpos = 0;
+ this.buflen = len;
+ }
+ int l;
+ int off = this.buflen;
+ int len = this.buffer.length - off;
+ l = in.read(this.buffer, off, len);
+ if (l == -1) {
+ return -1;
+ } else {
+ this.buflen = off + l;
+ return l;
+ }
+ }
+
+ public boolean hasBufferedData() {
+ return this.bufpos < this.buflen;
+ }
+
+ public int read() throws IOException {
+ int noRead = 0;
+ while (!hasBufferedData()) {
+ noRead = fillBuffer();
+ if (noRead == -1) {
+ return -1;
+ }
+ }
+ return this.buffer[this.bufpos++] & 0xff;
+ }
+
+ public int read(final byte[] b, int off, int len) throws IOException {
+ if (b == null) {
+ return 0;
+ }
+ int noRead = 0;
+ while (!hasBufferedData()) {
+ noRead = fillBuffer();
+ if (noRead == -1) {
+ return -1;
+ }
+ }
+ int chunk = this.buflen - this.bufpos;
+ if (chunk > len) {
+ chunk = len;
+ }
+ System.arraycopy(this.buffer, this.bufpos, b, off, chunk);
+ this.bufpos += chunk;
+ return chunk;
+ }
+
+ public int read(final byte[] b) throws IOException {
+ if (b == null) {
+ return 0;
+ }
+ return read(b, 0, b.length);
+ }
+
+ public boolean markSupported() {
+ return false;
+ }
+
+
+ public int readLine(final ByteArrayBuffer linebuf) throws IOException {
+ if (linebuf == null) {
+ throw new IllegalArgumentException("Buffer may not be null");
+ }
+ int total = 0;
+ boolean found = false;
+ int bytesRead = 0;
+ while (!found) {
+ if (!hasBufferedData()) {
+ bytesRead = fillBuffer();
+ if (bytesRead == -1) {
+ break;
+ }
+ }
+ int i = indexOf((byte)'\n');
+ int chunk;
+ if (i != -1) {
+ found = true;
+ chunk = i + 1 - pos();
+ } else {
+ chunk = length();
+ }
+ if (chunk > 0) {
+ linebuf.append(buf(), pos(), chunk);
+ skip(chunk);
+ total += chunk;
+ }
+ }
+ if (total == 0 && bytesRead == -1) {
+ return -1;
+ } else {
+ return total;
+ }
+ }
+
+ /**
+ * Implements quick search algorithm as published by
+ * <p>
+ * SUNDAY D.M., 1990,
+ * A very fast substring search algorithm,
+ * Communications of the ACM . 33(8):132-142.
+ * </p>
+ */
+ public int indexOf(final byte[] pattern, int off, int len) {
+ if (pattern == null) {
+ throw new IllegalArgumentException("Pattern may not be null");
+ }
+ if (off < this.bufpos || len < 0 || off + len > this.buflen) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (len < pattern.length) {
+ return -1;
+ }
+
+ int[] shiftTable = new int[255];
+ for (int i = 0; i < shiftTable.length; i++) {
+ shiftTable[i] = pattern.length + 1;
+ }
+ for (int i = 0; i < pattern.length; i++) {
+ int x = pattern[i] & 0xff;
+ shiftTable[x] = pattern.length - i;
+ }
+
+ int j = 0;
+ while (j <= len - pattern.length) {
+ int cur = this.bufpos + j;
+ boolean match = true;
+ for (int i = 0; i < pattern.length; i++) {
+ if (this.buffer[cur + i] != pattern[i]) {
+ match = false;
+ break;
+ }
+ }
+ if (match) {
+ return cur;
+ }
+
+ int pos = cur + pattern.length;
+ if (pos >= this.buffer.length) {
+ break;
+ }
+ int x = this.buffer[pos] & 0xff;
+ j += shiftTable[x];
+ }
+ return -1;
+ }
+
+ /**
+ * Implements quick search algorithm as published by
+ * <p>
+ * SUNDAY D.M., 1990,
+ * A very fast substring search algorithm,
+ * Communications of the ACM . 33(8):132-142.
+ * </p>
+ */
+ public int indexOf(final byte[] pattern) {
+ return indexOf(pattern, this.bufpos, this.buflen - this.bufpos);
+ }
+
+ public int indexOf(byte b, int off, int len) {
+ if (off < this.bufpos || len < 0 || off + len > this.buflen) {
+ throw new IndexOutOfBoundsException();
+ }
+ for (int i = off; i < off + len; i++) {
+ if (this.buffer[i] == b) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public int indexOf(byte b) {
+ return indexOf(b, this.bufpos, this.buflen - this.bufpos);
+ }
+
+ public byte charAt(int pos) {
+ if (pos < this.bufpos || pos > this.buflen) {
+ throw new IndexOutOfBoundsException();
+ }
+ return this.buffer[pos];
+ }
+
+ public byte[] buf() {
+ return this.buffer;
+ }
+
+ public int pos() {
+ return this.bufpos;
+ }
+
+ public int limit() {
+ return this.buflen;
+ }
+
+ public int length() {
+ return this.buflen - this.bufpos;
+ }
+
+ public int skip(int n) {
+ int chunk = Math.min(n, this.buflen - this.bufpos);
+ this.bufpos += chunk;
+ return chunk;
+ }
+
+ public void clear() {
+ this.bufpos = 0;
+ this.buflen = 0;
+ }
+
+ public String toString() {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("[pos: ");
+ buffer.append(this.bufpos);
+ buffer.append("]");
+ buffer.append("[limit: ");
+ buffer.append(this.buflen);
+ buffer.append("]");
+ buffer.append("[");
+ for (int i = this.bufpos; i < this.buflen; i++) {
+ buffer.append((char) this.buffer[i]);
+ }
+ buffer.append("]");
+ return buffer.toString();
+ }
+
+}
Modified: james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStream.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStream.java?rev=676874&r1=676873&r2=676874&view=diff
==============================================================================
--- james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStream.java (original)
+++ james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStream.java Tue Jul 15 04:20:38 2008
@@ -43,9 +43,4 @@
*/
public abstract int readLine(final ByteArrayBuffer dst) throws IOException;
- /**
- * Resets the internal state.
- */
- public abstract void reset();
-
}
Modified: james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStreamAdaptor.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStreamAdaptor.java?rev=676874&r1=676873&r2=676874&view=diff
==============================================================================
--- james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStreamAdaptor.java (original)
+++ james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/LineReaderInputStreamAdaptor.java Tue Jul 15 04:20:38 2008
@@ -1,104 +1,98 @@
-/****************************************************************
- * 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;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * <code>InputStream</code> used by the MIME parser to detect whether the
- * underlying data stream was used (read from) and whether the end of the
- * stream was reached.
- *
- * @version $Id$
- */
-class LineReaderInputStreamAdaptor extends LineReaderInputStream {
-
- private final LineReaderInputStream bis;
- private boolean used = false;
- private boolean eof = false;
-
- public LineReaderInputStreamAdaptor(final InputStream is) {
- super(is);
- if (is instanceof LineReaderInputStream) {
- this.bis = (LineReaderInputStream) is;
- } else {
- this.bis = null;
- }
- }
-
- public int read() throws IOException {
- int i = in.read();
- this.eof = i == -1;
- this.used = true;
- return i;
- }
-
- public int read(byte[] b, int off, int len) throws IOException {
- int i = in.read(b, off, len);
- this.eof = i == -1;
- this.used = true;
- return i;
- }
-
- public int readLine(final ByteArrayBuffer dst) throws IOException {
- int i;
- if (this.bis != null) {
- i = this.bis.readLine(dst);
- } else {
- i = doReadLine(dst);
- }
- this.eof = i == -1;
- this.used = true;
- return i;
- }
-
- private int doReadLine(final ByteArrayBuffer dst) throws IOException {
- int total = 0;
- int ch;
- while ((ch = in.read()) != -1) {
- dst.append(ch);
- total++;
- if (ch == '\n') {
- break;
- }
- }
- if (total == 0 && ch == -1) {
- return -1;
- } else {
- return total;
- }
- }
-
- public boolean eof() {
- return this.eof;
- }
-
- public boolean isUsed() {
- return this.used;
- }
-
- public void reset() {
- if (this.bis != null) {
- this.bis.reset();
- }
- }
-
-}
+/****************************************************************
+ * 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;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * <code>InputStream</code> used by the MIME parser to detect whether the
+ * underlying data stream was used (read from) and whether the end of the
+ * stream was reached.
+ *
+ * @version $Id$
+ */
+class LineReaderInputStreamAdaptor extends LineReaderInputStream {
+
+ private final LineReaderInputStream bis;
+ private boolean used = false;
+ private boolean eof = false;
+
+ public LineReaderInputStreamAdaptor(final InputStream is) {
+ super(is);
+ if (is instanceof LineReaderInputStream) {
+ this.bis = (LineReaderInputStream) is;
+ } else {
+ this.bis = null;
+ }
+ }
+
+ public int read() throws IOException {
+ int i = in.read();
+ this.eof = i == -1;
+ this.used = true;
+ return i;
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ int i = in.read(b, off, len);
+ this.eof = i == -1;
+ this.used = true;
+ return i;
+ }
+
+ public int readLine(final ByteArrayBuffer dst) throws IOException {
+ int i;
+ if (this.bis != null) {
+ i = this.bis.readLine(dst);
+ } else {
+ i = doReadLine(dst);
+ }
+ this.eof = i == -1;
+ this.used = true;
+ return i;
+ }
+
+ private int doReadLine(final ByteArrayBuffer dst) throws IOException {
+ int total = 0;
+ int ch;
+ while ((ch = in.read()) != -1) {
+ dst.append(ch);
+ total++;
+ if (ch == '\n') {
+ break;
+ }
+ }
+ if (total == 0 && ch == -1) {
+ return -1;
+ } else {
+ return total;
+ }
+ }
+
+ public boolean eof() {
+ return this.eof;
+ }
+
+ public boolean isUsed() {
+ return this.used;
+ }
+
+}
Modified: james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeBoundaryInputStream.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeBoundaryInputStream.java?rev=676874&r1=676873&r2=676874&view=diff
==============================================================================
--- james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeBoundaryInputStream.java (original)
+++ james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeBoundaryInputStream.java Tue Jul 15 04:20:38 2008
@@ -23,51 +23,51 @@
/**
* Stream that constrains itself to a single MIME body part.
- * After the stream ends (i.e. read() returns -1) {@link #isLastPart()}
+ * After the stream ends (i.e. read() returns -1) {@link #isLastPart()}
* can be used to determine if a final boundary has been seen or not.
*
* @version $Id: MimeBoundaryInputStream.java,v 1.2 2004/11/29 13:15:42 ntherning Exp $
*/
public class MimeBoundaryInputStream extends LineReaderInputStream {
-
+
private final byte[] boundary;
private boolean eof;
- private int limit;
- private boolean atBoundary;
- private int boundaryLen;
- private boolean lastPart;
+ private int limit;
+ private boolean atBoundary;
+ private int boundaryLen;
+ private boolean lastPart;
private boolean completed;
- private BufferedLineReaderInputStream buffer;
+ private BufferedLineReaderInputStream buffer;
/**
* Creates a new MimeBoundaryInputStream.
* @param s The underlying stream.
* @param boundary Boundary string (not including leading hyphens).
*/
- public MimeBoundaryInputStream(BufferedLineReaderInputStream inbuffer, String boundary)
+ public MimeBoundaryInputStream(BufferedLineReaderInputStream inbuffer, String boundary)
throws IOException {
super(inbuffer);
this.buffer = (BufferedLineReaderInputStream) in;
- this.eof = false;
- this.limit = -1;
- this.atBoundary = false;
- this.boundaryLen = 0;
- this.lastPart = false;
+ this.eof = false;
+ this.limit = -1;
+ this.atBoundary = false;
+ this.boundaryLen = 0;
+ this.lastPart = false;
this.completed = false;
- this.boundary = new byte[boundary.length() + 2];
- this.boundary[0] = (byte) '-';
- this.boundary[1] = (byte) '-';
- for (int i = 0; i < boundary.length(); i++) {
- byte ch = (byte) boundary.charAt(i);
- if (ch == '\r' || ch == '\n') {
- throw new IllegalArgumentException("Boundary may not contain CR or LF");
- }
- this.boundary[i + 2] = ch;
+ this.boundary = new byte[boundary.length() + 2];
+ this.boundary[0] = (byte) '-';
+ this.boundary[1] = (byte) '-';
+ for (int i = 0; i < boundary.length(); i++) {
+ byte ch = (byte) boundary.charAt(i);
+ if (ch == '\r' || ch == '\n') {
+ throw new IllegalArgumentException("Boundary may not contain CR or LF");
+ }
+ this.boundary[i + 2] = ch;
}
- fillBuffer();
+ fillBuffer();
}
/**
@@ -79,59 +79,50 @@
}
/**
- * @see java.io.InputStream#markSupported()
+ * @see java.io.InputStream#markSupported()
*/
- public boolean markSupported() {
- return false;
+ public boolean markSupported() {
+ return false;
}
/**
- * @see java.io.InputStream#read()
+ * @see java.io.InputStream#read()
*/
- public int read() throws IOException {
- if (completed) {
- return -1;
- }
+ public int read() throws IOException {
+ if (completed) {
+ return -1;
+ }
if (endOfStream() && !hasData()) {
- skipBoundary();
- return -1;
- }
- for (;;) {
- if (hasData()) {
- return buffer.read();
- } else if (endOfStream()) {
- skipBoundary();
- return -1;
- }
- fillBuffer();
- }
+ skipBoundary();
+ return -1;
+ }
+ for (;;) {
+ if (hasData()) {
+ return buffer.read();
+ } else if (endOfStream()) {
+ skipBoundary();
+ return -1;
+ }
+ fillBuffer();
+ }
}
- public int read(byte[] b, int off, int len) throws IOException {
- if (completed) {
- return -1;
- }
- if (endOfStream() && !hasData()) {
- skipBoundary();
- return -1;
- }
- fillBuffer();
- if (!hasData()) {
- return read(b, off, len);
- }
- int chunk = Math.min(len, limit - buffer.pos());
- return buffer.read(b, off, chunk);
- }
-
- /**
- * Resets the internal state. This will force the boundary to be rescanned.
- */
- public void reset() {
- atBoundary = false;
- limit = -1;
- eof = false;
+ public int read(byte[] b, int off, int len) throws IOException {
+ if (completed) {
+ return -1;
+ }
+ if (endOfStream() && !hasData()) {
+ skipBoundary();
+ return -1;
+ }
+ fillBuffer();
+ if (!hasData()) {
+ return read(b, off, len);
+ }
+ int chunk = Math.min(len, limit - buffer.pos());
+ return buffer.read(b, off, chunk);
}
-
+
public int readLine(final ByteArrayBuffer dst) throws IOException {
if (dst == null) {
throw new IllegalArgumentException("Destination buffer may not be null");
@@ -177,83 +168,83 @@
return total;
}
}
-
- private boolean endOfStream() {
- return eof || atBoundary;
+
+ private boolean endOfStream() {
+ return eof || atBoundary;
+ }
+
+ private boolean hasData() {
+ return limit > buffer.pos() && limit <= buffer.limit();
}
- private boolean hasData() {
- return limit > buffer.pos() && limit <= buffer.limit();
- }
-
private int fillBuffer() throws IOException {
if (eof) {
return -1;
}
- int bytesRead;
+ int bytesRead;
if (!hasData()) {
- bytesRead = buffer.fillBuffer();
- } else {
- bytesRead = 0;
- }
- eof = bytesRead == -1;
+ bytesRead = buffer.fillBuffer();
+ } else {
+ bytesRead = 0;
+ }
+ eof = bytesRead == -1;
- int i = buffer.indexOf(boundary);
- if (i != -1) {
- limit = i;
- atBoundary = true;
- calculateBoundaryLen();
- } else {
- if (eof) {
- limit = buffer.limit();
- } else {
- limit = buffer.limit() - (boundary.length + 1);
- // \r\n + (boundary - one char)
+ int i = buffer.indexOf(boundary);
+ if (i != -1) {
+ limit = i;
+ atBoundary = true;
+ calculateBoundaryLen();
+ } else {
+ if (eof) {
+ limit = buffer.limit();
+ } else {
+ limit = buffer.limit() - (boundary.length + 1);
+ // \r\n + (boundary - one char)
}
}
- return bytesRead;
- }
-
- private void calculateBoundaryLen() throws IOException {
- boundaryLen = boundary.length;
- int len = limit - buffer.pos();
- if (len > 0) {
- if (buffer.charAt(limit - 1) == '\n') {
- boundaryLen++;
- limit--;
+ return bytesRead;
+ }
+
+ private void calculateBoundaryLen() throws IOException {
+ boundaryLen = boundary.length;
+ int len = limit - buffer.pos();
+ if (len > 0) {
+ if (buffer.charAt(limit - 1) == '\n') {
+ boundaryLen++;
+ limit--;
}
}
- if (len > 1) {
- if (buffer.charAt(limit - 1) == '\r') {
- boundaryLen++;
- limit--;
- }
+ if (len > 1) {
+ if (buffer.charAt(limit - 1) == '\r') {
+ boundaryLen++;
+ limit--;
+ }
}
}
private void skipBoundary() throws IOException {
- if (!completed) {
- completed = true;
- buffer.skip(boundaryLen);
+ if (!completed) {
+ completed = true;
+ buffer.skip(boundaryLen);
for (;;) {
- if (buffer.length() > 1) {
- int ch1 = buffer.charAt(buffer.pos());
- int ch2 = buffer.charAt(buffer.pos() + 1);
+ if (buffer.length() > 1) {
+ int ch1 = buffer.charAt(buffer.pos());
+ int ch2 = buffer.charAt(buffer.pos() + 1);
if (ch1 == '-' && ch2 == '-') {
- this.lastPart = true;
- buffer.skip(2);
+ this.lastPart = true;
+ buffer.skip(2);
}
skipLineDelimiter();
- break;
- } else {
- fillBuffer();
+ break;
+ } else {
+ fillBuffer();
}
- if (eof) {
- break;
+ if (eof) {
+ break;
}
}
}
- }
+ }
private void skipLineDelimiter() {
int ch1 = 0;
@@ -271,13 +262,13 @@
buffer.skip(1);
}
}
-
- public boolean isLastPart() {
- return lastPart;
- }
-
- public boolean eof() {
- return eof && !buffer.hasBufferedData();
+
+ public boolean isLastPart() {
+ return lastPart;
+ }
+
+ public boolean eof() {
+ return eof && !buffer.hasBufferedData();
}
public String toString() {
Modified: james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeEntity.java
URL: http://svn.apache.org/viewvc/james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeEntity.java?rev=676874&r1=676873&r2=676874&view=diff
==============================================================================
--- james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeEntity.java (original)
+++ james/mime4j/branches/streams-refactoring/src/main/java/org/apache/james/mime4j/MimeEntity.java Tue Jul 15 04:20:38 2008
@@ -168,9 +168,6 @@
mimeStream = new MimeBoundaryInputStream(inbuffer, body.getBoundary());
}
dataStream = new LineReaderInputStreamAdaptor(mimeStream);
- // If multipart message is embedded into another multipart message
- // make sure to reset parent's mime stream
- inbuffer.reset();
}
private void clearMimeStream() {
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org