~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/sun/nio/ch/IOUtil.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
 
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
4
 *
 
5
 * This code is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License version 2 only, as
 
7
 * published by the Free Software Foundation.  Oracle designates this
 
8
 * particular file as subject to the "Classpath" exception as provided
 
9
 * by Oracle in the LICENSE file that accompanied this code.
 
10
 *
 
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
 
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
14
 * version 2 for more details (a copy is included in the LICENSE file that
 
15
 * accompanied this code).
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License version
 
18
 * 2 along with this work; if not, write to the Free Software Foundation,
 
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 *
 
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 
22
 * or visit www.oracle.com if you need additional information or have any
 
23
 * questions.
 
24
 */
 
25
 
 
26
package sun.nio.ch;
 
27
 
 
28
import java.io.FileDescriptor;
 
29
import java.io.IOException;
 
30
import java.net.*;
 
31
import java.nio.ByteBuffer;
 
32
import java.nio.channels.*;
 
33
import java.nio.channels.spi.*;
 
34
 
 
35
 
 
36
/**
 
37
 * File-descriptor based I/O utilities that are shared by NIO classes.
 
38
 */
 
39
 
 
40
class IOUtil {
 
41
 
 
42
    private IOUtil() { }                // No instantiation
 
43
 
 
44
    static boolean randomBytes(byte[] someBytes)
 
45
    {
 
46
        try
 
47
        {
 
48
            if (false) throw new cli.System.Security.Cryptography.CryptographicException();
 
49
            cli.System.Security.Cryptography.RNGCryptoServiceProvider csp = new cli.System.Security.Cryptography.RNGCryptoServiceProvider();
 
50
            csp.GetBytes(someBytes);
 
51
            return true;
 
52
        }
 
53
        catch (cli.System.Security.Cryptography.CryptographicException _)
 
54
        {
 
55
            return false;
 
56
        }
 
57
    }
 
58
 
 
59
    static void configureBlocking(FileDescriptor fd, boolean blocking) throws IOException
 
60
    {
 
61
        fd.setSocketBlocking(blocking);
 
62
    }
 
63
 
 
64
    // this is a dummy method to allow us to use unmodified socket channel impls
 
65
    static int fdVal(FileDescriptor fd)
 
66
    {
 
67
        return 0xbadc0de;
 
68
    }
 
69
 
 
70
    static int read(FileDescriptor fd, ByteBuffer dst, long position,
 
71
                    NativeDispatcher nd, Object lock)
 
72
        throws IOException
 
73
    {
 
74
        if (dst.isReadOnly())
 
75
            throw new IllegalArgumentException("Read-only buffer");
 
76
 
 
77
        if (position != -1)
 
78
        {
 
79
            synchronized (lock)
 
80
            {
 
81
                long prevpos = fd.getFilePointer();
 
82
                try
 
83
                {
 
84
                    fd.seek(position);
 
85
                    return read(fd, dst, -1, nd, null);
 
86
                }
 
87
                finally
 
88
                {
 
89
                    fd.seek(prevpos);
 
90
                }
 
91
            }
 
92
        }
 
93
 
 
94
        if (dst.hasArray())
 
95
        {
 
96
            byte[] buf = dst.array();
 
97
            int len = nd.read(fd, buf, dst.arrayOffset() + dst.position(), dst.remaining());
 
98
            if (len > 0)
 
99
            {
 
100
                dst.position(dst.position() + len);
 
101
            }
 
102
            return len;
 
103
        }
 
104
        else
 
105
        {
 
106
            byte[] buf = new byte[dst.remaining()];
 
107
            int len = nd.read(fd, buf, 0, buf.length);
 
108
            if (len > 0)
 
109
            {
 
110
                dst.put(buf, 0, len);
 
111
            }
 
112
            return len;
 
113
        }
 
114
    }
 
115
 
 
116
    static long read(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length, NativeDispatcher nd)
 
117
        throws IOException
 
118
    {
 
119
        return nd.read(fd, bufs, offset, length);
 
120
    }
 
121
 
 
122
    static int write(FileDescriptor fd, ByteBuffer src, long position,
 
123
                     NativeDispatcher nd, Object lock)
 
124
        throws IOException
 
125
    {
 
126
        if (position != -1)
 
127
        {
 
128
            synchronized (lock)
 
129
            {
 
130
                long prevpos = fd.getFilePointer();
 
131
                try
 
132
                {
 
133
                    fd.seek(position);
 
134
                    return write(fd, src, -1, nd, null);
 
135
                }
 
136
                finally
 
137
                {
 
138
                    fd.seek(prevpos);
 
139
                }
 
140
            }
 
141
        }
 
142
 
 
143
        if (src.hasArray())
 
144
        {
 
145
            byte[] buf = src.array();
 
146
            int len = nd.write(fd, buf, src.arrayOffset() + src.position(), src.remaining());
 
147
            if (len > 0)
 
148
            {
 
149
                src.position(src.position() + len);
 
150
            }
 
151
            return len;
 
152
        }
 
153
        else
 
154
        {
 
155
            int pos = src.position();
 
156
            byte[] buf = new byte[src.remaining()];
 
157
            src.get(buf);
 
158
            src.position(pos);
 
159
            int len = nd.write(fd, buf, 0, buf.length);
 
160
            if (len > 0)
 
161
            {
 
162
                src.position(pos + len);
 
163
            }
 
164
            return len;
 
165
        }
 
166
    }
 
167
 
 
168
    static long write(FileDescriptor fd, ByteBuffer[] bufs, NativeDispatcher nd)
 
169
        throws IOException
 
170
    {
 
171
        return nd.write(fd, bufs, 0, bufs.length);
 
172
    }
 
173
 
 
174
    static long write(FileDescriptor fd, ByteBuffer[] bufs, int offset, int length, NativeDispatcher nd)
 
175
        throws IOException
 
176
    {
 
177
        return nd.write(fd, bufs, offset, length);
 
178
    }
 
179
}