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: package org.apache.harmony.luni.net;
19:
20: import java.io.IOException;
21: import java.io.OutputStream;
22: import java.net.Socket;
23: import java.net.SocketImpl;
24:
25: import org.apache.harmony.luni.util.Msg;
26:
27: class SocketOutputStream extends OutputStream {
28:
29: private PlainSocketImpl socket;
30:
31: /**
32: * Constructs a SocketOutputStream for the <code>socket</code>. Write
33: * operations are forwarded to the <code>socket</code>.
34: *
35: * @param socket the socket to be written
36: * @see Socket
37: */
38: public SocketOutputStream(SocketImpl socket) {
39: super ();
40: this .socket = (PlainSocketImpl) socket;
41: }
42:
43: @Override
44: public void close() throws IOException {
45: socket.close();
46: }
47:
48: @Override
49: public void write(byte[] buffer) throws IOException {
50: socket.write(buffer, 0, buffer.length);
51: }
52:
53: @Override
54: public void write(byte[] buffer, int offset, int count)
55: throws IOException {
56: // avoid int overflow
57: if (buffer != null) {
58: if (0 <= offset && offset <= buffer.length && 0 <= count
59: && count <= buffer.length - offset) {
60: socket.write(buffer, offset, count);
61: } else {
62: throw new ArrayIndexOutOfBoundsException(Msg
63: .getString("K002f"));//$NON-NLS-1$
64: }
65: } else {
66: throw new NullPointerException(Msg.getString("K0047"));//$NON-NLS-1$
67: }
68: }
69:
70: @Override
71: public void write(int oneByte) throws IOException {
72: byte[] buffer = new byte[1];
73: buffer[0] = (byte) (oneByte & 0xFF);
74:
75: socket.write(buffer, 0, 1);
76: }
77: }
|