~slub.team/goobi-production/bug-1013622

« back to all changes in this revision

Viewing changes to src/de/unigoettingen/sub/commons/util/stream/SimpleInputStreamSaver.java

  • Committer: Ralf Claussnitzer
  • Date: 2012-05-29 10:55:34 UTC
  • mfrom: (66.1.1 integrate-util)
  • Revision ID: ralf.claussnitzer@slub-dresden.de-20120529105534-t5u5vxj9x5v2vifb
fixes lp:1005844 integrate sub-commons util library

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the SUB Commons project.
 
3
 * Visit the websites for more information. 
 
4
 *              - http://gdz.sub.uni-goettingen.de 
 
5
 * 
 
6
 * Copyright 2009, Center for Retrospective Digitization, Göttingen (GDZ),
 
7
 * 
 
8
 * Licensed under the Apache License, Version 2.0 (the “License”);
 
9
 * you may not use this file except in compliance with the License.
 
10
 * You may obtain a copy of the License at
 
11
 * 
 
12
 *  http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
14
 * Unless required by applicable law or agreed to in writing, software
 
15
 * distributed under the License is distributed on an “AS IS” BASIS,
 
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
17
 * See the License for the specific language governing permissions and
 
18
 * limitations under the License.
 
19
 */
 
20
 
 
21
package de.unigoettingen.sub.commons.util.stream;
 
22
 
 
23
import java.io.BufferedInputStream;
 
24
import java.io.File;
 
25
import java.io.FileOutputStream;
 
26
import java.io.IOException;
 
27
import java.io.InputStream;
 
28
 
 
29
 
 
30
/**
 
31
 * The Class SimpleInputStreamSaver provides a simple way to save the contents of an InputSream in a File.
 
32
 */
 
33
public class SimpleInputStreamSaver {
 
34
        
 
35
        /** The file. */
 
36
        File file;
 
37
        
 
38
        /** The is. */
 
39
        InputStream is;
 
40
        
 
41
        /**
 
42
         * Instantiates a new simple input stream saver.
 
43
         */
 
44
        public SimpleInputStreamSaver () {
 
45
                
 
46
        }
 
47
        
 
48
        /**
 
49
         * Instantiates a new simple input stream saver.
 
50
         * 
 
51
         * @param file the file to the the contents to.
 
52
         * @param is the InputStream to save
 
53
         */
 
54
        public SimpleInputStreamSaver (File file, InputStream is) {
 
55
                this.file = file;
 
56
                this.is = is;
 
57
        }
 
58
        
 
59
        /**
 
60
         * Safe the contents of the stream
 
61
         * 
 
62
         * @throws IOException Signals that an I/O exception has occurred.
 
63
         */
 
64
        public void safe () throws IOException {
 
65
                BufferedInputStream bis = new BufferedInputStream(is);
 
66
                FileOutputStream fos = new FileOutputStream(file);
 
67
                try {
 
68
                        int bufSize = 1024 * 8;
 
69
                        byte[] bytes = new byte[bufSize];
 
70
                        int count = bis.read(bytes);
 
71
                        while (count != -1 && count <= bufSize) {
 
72
                                fos.write(bytes, 0, count);
 
73
                                count = bis.read(bytes);
 
74
                        }
 
75
                        if (count != -1) {
 
76
                                fos.write(bytes, 0, count);
 
77
                        }
 
78
                        fos.close();
 
79
                } finally {
 
80
                        bis.close();
 
81
                        fos.close();
 
82
                }
 
83
        }
 
84
 
 
85
        /**
 
86
         * Gets the file.
 
87
         * 
 
88
         * @return the file
 
89
         */
 
90
        public File getFile() {
 
91
                return file;
 
92
        }
 
93
 
 
94
        /**
 
95
         * Sets the file.
 
96
         * 
 
97
         * @param file the new file
 
98
         */
 
99
        public void setFile(File file) {
 
100
                this.file = file;
 
101
        }
 
102
 
 
103
        /**
 
104
         * Gets the InputStream.
 
105
         * 
 
106
         * @return the InputStream.
 
107
         */
 
108
        public InputStream getIs() {
 
109
                return is;
 
110
        }
 
111
 
 
112
        /**
 
113
         * Sets the InputStream.
 
114
         * 
 
115
         * @param is the InputStream.
 
116
         */
 
117
        public void setIs(InputStream is) {
 
118
                this.is = is;
 
119
        }
 
120
        
 
121
        
 
122
 
 
123
}