01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /**
19: * @author Alexander Y. Kleymenov
20: * @version $Revision$
21: */package org.apache.harmony.xnet.provider.jsse;
22:
23: import org.apache.harmony.xnet.provider.jsse.AlertException;
24:
25: import java.nio.ByteBuffer;
26: import javax.net.ssl.SSLException;
27:
28: /**
29: * This class is used to retrieve the application data
30: * arrived for the SSLEngine.
31: */
32: public class SSLEngineAppData implements
33: org.apache.harmony.xnet.provider.jsse.Appendable {
34:
35: /**
36: * Buffer containing received application data.
37: */
38: byte[] buffer;
39:
40: /**
41: * Constructor
42: */
43: protected SSLEngineAppData() {
44: }
45:
46: /**
47: * Stores received data. The source data is not cloned,
48: * just the array reference is remembered into the buffer field.
49: */
50: public void append(byte[] src) {
51: if (buffer != null) {
52: throw new AlertException(AlertProtocol.INTERNAL_ERROR,
53: new SSLException("Attempt to override the data"));
54: }
55: buffer = src;
56: }
57:
58: /**
59: * Places the data from the buffer into the array of destination
60: * ByteBuffer objects.
61: */
62: protected int placeTo(ByteBuffer[] dsts, int offset, int length) {
63: if (buffer == null) {
64: return 0;
65: }
66: int pos = 0;
67: int len = buffer.length;
68: int rem;
69: // write data to the buffers
70: for (int i = offset; i < offset + length; i++) {
71: rem = dsts[i].remaining();
72: // TODO: optimization work - use hasArray, array(), arraycopy
73: if (len - pos < rem) {
74: // can fully write remaining data into buffer
75: dsts[i].put(buffer, pos, len - pos);
76: pos = len;
77: // data was written, exit
78: break;
79: } else {
80: // write chunk of data
81: dsts[i].put(buffer, pos, rem);
82: pos += rem;
83: }
84: }
85: if (pos != len) {
86: // The data did not feet into the buffers,
87: // it should not happen, because the destination buffers
88: // had been checked for the space before record unwrapping.
89: // But if it so, we should allert about internal error.
90: throw new AlertException(AlertProtocol.INTERNAL_ERROR,
91: new SSLException(
92: "The received application data could not be fully written"
93: + "into the destination buffers"));
94: }
95: buffer = null;
96: return len;
97: }
98: }
|