Project

General

Profile

1
/**
2
 *  Copyright: 2010 Regents of the University of California and the
3
 *              National Center for Ecological Analysis and Synthesis
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 2 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, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.restservice;
20

    
21
import java.io.IOException;
22
import java.io.InputStream;
23
import java.io.OutputStream;
24

    
25
import javax.activation.DataSource;
26

    
27
/**
28
 * Encapsulate an InputStream within a DataSource interface so that it is 
29
 * accessible to MIME processors.
30
 * 
31
 * @author Matthew Jones
32
 */
33
public class InputStreamDataSource implements DataSource {
34
    private String name;
35
    private InputStream stream;
36
    private boolean readOnce;
37
    
38
    public InputStreamDataSource(String name, InputStream stream) {
39
        super();
40
        this.name = name;
41
        this.stream = stream;
42
        this.readOnce = false;
43
    }
44

    
45
    public String getContentType() {
46
        return "application/octet-stream";
47
    }
48

    
49
    public InputStream getInputStream() throws IOException {
50
        if (readOnce) {
51
            throw new IOException("Only call getInputStream() once.");
52
        }
53
        readOnce = true;
54
        
55
        return stream;
56
    }
57

    
58
    public String getName() {
59
        return this.name;
60
    }
61

    
62
    public OutputStream getOutputStream() throws IOException {
63
        throw new IOException("Can't get an OutputStream from an InputStreamDataSource.");
64
    }
65
}
(3-3/5)