source: trunk/src/org/expeditee/network/ImageSaver.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 4.1 KB
Line 
1/**
2 * ImageSaver.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.network;
20
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.OutputStream;
26import java.net.ServerSocket;
27import java.net.Socket;
28import java.util.ArrayList;
29import java.util.List;
30
31import org.expeditee.gui.FrameIO;
32
33public class ImageSaver extends DefaultServer {
34
35 public static boolean recvImage(File file, Socket socket) throws IOException {
36 InputStream is = socket.getInputStream();
37 // file length is the first 4 bytes
38 int fileLen = (is.read() << 24) | (is.read() << 16) | (is.read() << 8) | is.read();
39 // if the above reads failed, fileLen will be -1
40 if(fileLen <= 0) {
41 return false;
42 }
43 if(file.exists()) {
44 System.out.println("Ignoring " + file.getName() + " (already exists on filesystem)");
45 is.skip(fileLen);
46 return false;
47 }
48 System.out.println("Receiving " + file.getName() + " (" + fileLen + " bytes)");
49 file.getParentFile().mkdirs();
50 FileOutputStream out = new FileOutputStream(file);
51 // file is the remaining bytes of the stream
52 byte[] buf = new byte[1024];
53 int r = -1;
54 int len = 0;
55 while((r = is.read(buf)) > 0) {
56 out.write(buf, 0, r);
57 len += r;
58 if(fileLen - len <= 0) {
59 break;
60 }
61 if(fileLen - len < 1024) {
62 buf = new byte[fileLen - len];
63 }
64 }
65 out.flush();
66 out.close();
67 return true;
68 }
69
70 /**
71 * Reads an image storage request, and saves the image on the server
72 *
73 * @author jts21
74 *
75 */
76 private static class ImageReceiver extends Thread {
77
78 private Socket socket;
79
80 public ImageReceiver(Socket client) {
81 super("ImageReceiver");
82 this.socket = client;
83 }
84
85 @Override
86 public void run() {
87 try {
88 InputStream is = this.socket.getInputStream();
89 OutputStream os = this.socket.getOutputStream();
90 // number of files is the first byte
91 int numFiles = is.read() << 8 | is.read();
92 if(numFiles == -1)
93 return;
94 // read the filenames
95 File[] files = new File[numFiles];
96 List<Integer> wantFiles = new ArrayList<Integer>();
97 for(int i = 0; i < numFiles; i++) {
98 int fileNameLen = is.read();
99 byte[] fileName = new byte[fileNameLen];
100 is.read(fileName);
101 files[i] = new File(FrameIO.IMAGES_PATH + new String(fileName, FrameShare.CHARSET));
102 // pick out which files we want
103 if(!files[i].exists()) {
104 wantFiles.add(i);
105 os.write(i);
106 }
107 }
108 // close the output stream since we won't be sending anything more
109 this.socket.shutdownOutput();
110 // get the files
111 for(Integer i : wantFiles) {
112 recvImage(files[i], socket);
113 }
114 this.socket.close();
115 } catch (IOException e) {
116 e.printStackTrace();
117 }
118 }
119 }
120
121 public final static int OFFSET = 6;
122
123 private ServerSocket socket;
124
125 public ImageSaver(int port) throws IOException {
126 super("ImageSaver");
127 this.socket = new ServerSocket(port + OFFSET);
128 }
129
130 @Override
131 protected void listenForMessages() throws IOException {
132 Socket client = socket.accept();
133 // should probably only allow a limited number of threads here
134 new ImageReceiver(client).start();
135 }
136
137 @Override
138 protected void closeSocket() {
139 try {
140 socket.close();
141 } catch (IOException e) {
142 e.printStackTrace();
143 }
144 }
145}
Note: See TracBrowser for help on using the repository browser.