2 Copyright 2009 senju@users.sourceforge.jp
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 package net.excentrics.bandWidth.sample;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.net.InetSocketAddress;
22 import java.nio.ByteBuffer;
23 import java.nio.channels.FileChannel;
24 import java.nio.channels.ServerSocketChannel;
25 import java.nio.channels.SocketChannel;
27 import net.excentrics.bandWidth.SocketChannelWriter;
40 * <td>送信するファイルのパス(jpeg)</td>
47 public class OneFileWebServer {
53 public static void main(String[] args) {
54 int bps = Integer.parseInt(args[0]) * 1024; // kbpsをbpsに変換
57 // 送信するファイルとバッファーのセットアップ
58 ByteBuffer buf = ByteBuffer.allocate(8000);
59 FileInputStream in = new FileInputStream(args[1]);
60 FileChannel file = in.getChannel();
63 ServerSocketChannel ssc = ServerSocketChannel.open();
64 ssc.socket().bind(new InetSocketAddress(8080));
67 SocketChannel sc = ssc.accept();
73 String respHeader = "HTTP/1.0 200 OK\nContent-Type: image/jpeg\nConnection: Close\n\n";
74 buf.put(respHeader.getBytes("ascii"));
79 // SocketChannelWriteの準備
80 SocketChannelWriter scw = new SocketChannelWriter(sc, bps);
82 boolean isEof = false;
84 if (file.read(buf) == -1) {
87 Thread.sleep(sleeptime);
89 sleeptime = scw.write(buf);
97 } catch (IOException e) {
99 } catch (InterruptedException e) {