summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/main/java/org/simpleframework/http/message/PartConsumer.java
diff options
context:
space:
mode:
authormikaelpeltier <mikaelpeltier@google.com>2015-06-24 14:31:11 +0200
committerMikael Peltier <mikaelpeltier@google.com>2015-06-24 14:59:36 +0000
commit04563874ddaac702d6c715eaa89c29b253f4c54e (patch)
treec305fa98670c3e80be494cc054a8e31b51bfe7f2 /simple/simple-http/src/main/java/org/simpleframework/http/message/PartConsumer.java
parentf1828481ebcfee3bddc323fca178a4502a60ceef (diff)
downloadtoolchain_jack-04563874ddaac702d6c715eaa89c29b253f4c54e.zip
toolchain_jack-04563874ddaac702d6c715eaa89c29b253f4c54e.tar.gz
toolchain_jack-04563874ddaac702d6c715eaa89c29b253f4c54e.tar.bz2
Add simpleframework source files
Change-Id: I18d01df16de2868ca5458f79a88e6070b75db2c3 (cherry picked from commit 3e9f84cf7b22f6970eb8041ca38d12d75c6bb270)
Diffstat (limited to 'simple/simple-http/src/main/java/org/simpleframework/http/message/PartConsumer.java')
-rw-r--r--simple/simple-http/src/main/java/org/simpleframework/http/message/PartConsumer.java135
1 files changed, 135 insertions, 0 deletions
diff --git a/simple/simple-http/src/main/java/org/simpleframework/http/message/PartConsumer.java b/simple/simple-http/src/main/java/org/simpleframework/http/message/PartConsumer.java
new file mode 100644
index 0000000..cc54558
--- /dev/null
+++ b/simple/simple-http/src/main/java/org/simpleframework/http/message/PartConsumer.java
@@ -0,0 +1,135 @@
+/*
+ * PartConsumer.java February 2007
+ *
+ * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
+ *
+ * Licensed 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.simpleframework.http.message;
+
+import java.io.IOException;
+
+import org.simpleframework.common.buffer.Allocator;
+import org.simpleframework.transport.ByteCursor;
+
+/**
+ * The <code>PartConsumer</code> object is used to consume a part
+ * from a part list. A part consists of a header and a body, which
+ * can be either a simple chunk of data or another part list. This
+ * must be able to cope with either a simple body or a part list.
+ *
+ * @author Niall Gallagher
+ */
+class PartConsumer implements ByteConsumer {
+
+ /**
+ * This is used to consume the header message of the part.
+ */
+ private SegmentConsumer header;
+
+ /**
+ * This is used to consume the body data from the part.
+ */
+ private BodyConsumer body;
+
+ /**
+ * This is used to determine what type the body data is.
+ */
+ private PartFactory factory;
+
+ /**
+ * This is used to add the consumed parts to when finished.
+ */
+ private PartSeries series;
+
+ /**
+ * This is the current consumer used to read from the cursor.
+ */
+ private ByteConsumer current;
+
+ /**
+ * This is the terminal token that ends the part payload.
+ */
+ private byte[] terminal;
+
+ /**
+ * Constructor for the <code>PartConsumer</code> object. This is
+ * used to create a consumer used to read the contents of a part
+ * and the boundary that terminates the content. Any parts that
+ * are created by this are added to the provided part list.
+ *
+ * @param allocator this is the allocator used to creat buffers
+ * @param series this is the part list used to store the parts
+ * @param terminal this is the terminal token for the part
+ * @param length this is the length of the parent part series
+ */
+ public PartConsumer(Allocator allocator, PartSeries series, byte[] terminal, long length) {
+ this.header = new PartHeaderConsumer(allocator);
+ this.factory = new PartFactory(allocator, header, length);
+ this.terminal = terminal;
+ this.current = header;
+ this.series = series;
+ }
+
+ /**
+ * This is used to create a new body consumer used to consume the
+ * part body from for the list. This will ensure that the part
+ * data is created based on the part header consumed. The types
+ * of part supported are part lists and part body.
+ *
+ * @return this returns a consumed for the part content
+ */
+ private BodyConsumer getConsumer() {
+ return factory.getInstance(series, terminal);
+ }
+
+ /**
+ * This is used to consume the part body from the cursor. This
+ * initially reads the body of the part, which represents the
+ * actual payload exposed via the <code>Part</code> interface
+ * once the payload has been consumed the terminal is consumed.
+ *
+ * @param cursor this is the cursor to consume the body from
+ */
+ public void consume(ByteCursor cursor) throws IOException {
+ while(cursor.isReady()) {
+ if(header.isFinished()) {
+ if(body == null) {
+ body = getConsumer();
+ current = body;
+ } else {
+ if(body.isFinished())
+ break;
+ }
+ }
+ current.consume(cursor);
+ }
+ }
+
+ /**
+ * This is used to determine whether the part body has been read
+ * from the cursor successfully. In order to determine if all of
+ * the bytes have been read successfully this will check to see
+ * of the terminal token had been consumed.
+ *
+ * @return true if the part body and terminal have been read
+ */
+ public boolean isFinished() {
+ if(body != null) {
+ return body.isFinished();
+ }
+ return false;
+ }
+}
+