KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > http > JniStream


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.server.http;
30
31 import java.io.*;
32 import java.net.*;
33 import java.util.*;
34
35 import com.caucho.util.*;
36 import com.caucho.vfs.*;
37
38 /**
39  * Stream using with JNI.
40  */

41 public class JniStream extends StreamImpl {
42   private static boolean hasInitJni;
43   
44   private static NullPath nullPath;
45
46   private int fd;
47   private boolean flushOnNewline;
48   private boolean closeChildOnClose = true;
49
50   /**
51    * Create a new JniStream based on the java.io.* stream.
52    */

53   public JniStream(int fd)
54   {
55     if (! hasInitJni)
56       initJni();
57     
58     init(fd);
59     if (nullPath == null)
60       nullPath = new NullPath("jni-stream");
61     setPath(nullPath);
62   }
63   
64   public JniStream()
65   {
66     if (! hasInitJni)
67       initJni();
68     
69     if (nullPath == null)
70       nullPath = new NullPath("jni-stream");
71     setPath(nullPath);
72   }
73
74   public void init(int fd)
75   {
76     this.fd = fd;
77   }
78
79   void initJni()
80   {
81     hasInitJni = true;
82   }
83
84   public boolean canRead()
85   {
86     return true;
87   }
88
89   public int read(byte []buf, int offset, int length)
90     throws IOException
91   {
92     if (buf == null)
93       throw new NullPointerException();
94     else if (offset < 0 || buf.length < offset + length)
95       throw new ArrayIndexOutOfBoundsException();
96     
97     return readNative(fd, buf, offset, length);
98   }
99
100   native int readNative(int fd, byte []buf, int offset, int length)
101     throws IOException;
102
103   // XXX: needs update
104
public int getAvailable() throws IOException
105   {
106     return -1;
107   }
108
109   public boolean canWrite()
110   {
111     return true;
112   }
113
114   public void write(byte []buf, int offset, int length, boolean isEnd)
115     throws IOException
116   {
117     if (buf == null)
118       throw new NullPointerException();
119     else if (offset < 0 || buf.length < offset + length)
120       throw new ArrayIndexOutOfBoundsException();
121     
122     int result = writeNative(fd, buf, offset, length);
123
124     if (result < 0)
125       throw new ClientDisconnectException("connection reset by peer");
126   }
127
128   native int writeNative(int fd, byte []buf, int offset, int length)
129     throws IOException;
130
131   public void flush()
132     throws IOException
133   {
134     int result = flushNative(fd);
135
136     if (result < 0)
137       throw new ClientDisconnectException("connection reset by peer");
138   }
139
140   native int flushNative(int fd) throws IOException;
141
142   public void close() throws IOException
143   {
144   }
145 }
146
Popular Tags