1 /*
2 * Copyright (C) 2019 sw4j.org
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 package org.sw4j.tool.barcode.random.codedata;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import org.sw4j.tool.barcode.random.config.CodeType;
26 import org.sw4j.tool.barcode.random.config.Config;
27 import org.sw4j.tool.barcode.random.config.EncodingConfig;
28
29 /**
30 * <p>
31 * This is a concrete implementation of the {@link CodeData} interface that handles files for input and output.
32 * </p>
33 * <p>
34 * <em>This class is not thread safe.</em>
35 * </p>
36 * @author Uwe Plonus <u.plonus@gmail.com>
37 */
38 public class FileCodeData implements CodeData {
39
40 /**
41 * <p>
42 * The configuration with the data for the input and output files.
43 * </p>
44 */
45 private final Config config;
46
47 /**
48 * <p>
49 * Create a new {@code FileCodeData} with the given configuration.
50 * </p>
51 * @param config the configuration (files and folders) to use for the input and output data.
52 */
53 public FileCodeData(final Config config) {
54 this.config = config;
55 }
56
57 /**
58 * <p>
59 * Create a new {@code FileInputStream} for the input file given in the configuration. The {@code InputStream} must
60 * be closed by the caller.
61 * </p>
62 * @return a new {@code FileInputStream} for reading the input data.
63 * @throws IOException if the creation of the {@code FileInputStream} fails.
64 */
65 @Override
66 public InputStream getInput() throws IOException {
67 return new FileInputStream(config.getInput().getName());
68 }
69
70 /**
71 * <p>
72 * Create a new {@code FileOutputStream} for writing the csv output file. If the folder for the output file does not
73 * exists then it will be created.
74 * </p>
75 * @return a new {@code FileOutputStream} for writing the csv data.
76 * @throws IOException if the creation of the {@code FileOutputStream} or the folder fails.
77 */
78 @Override
79 public OutputStream getOutput() throws IOException {
80 File folder = createOutputFolder();
81 return new FileOutputStream(new File(folder, config.getOutput().getFile().getName()));
82 }
83
84 /**
85 * <p>
86 * Create a new {@code FileOutputStream} for writing a barcode. If the folder for the output file does not exists
87 * then it will be created.
88 * </p>
89 * <p>
90 * This method may be called from different threads as long as the parameters are different.
91 * </p>
92 * @param type the type to write.
93 * @param format the encoding format to write.
94 * @param ident the ident of the data.
95 * @param suffix the suffix of the file to write.
96 * @return a new {@code FileOutputStream} for writing the barcode data.
97 * @throws IOException if the creation of the {@code FileOutputStream} or the folder fails.
98 */
99 @Override
100 public OutputStream getOutputForIdent(final CodeType type, final EncodingConfig format, final String ident,
101 final String suffix)
102 throws IOException {
103 File folder = createOutputFolder();
104 // TODO Check file name for encoding
105 return new FileOutputStream(new File(folder,
106 String.format("%s-%s-%s.%s", type.getType(), format.getType(), ident, suffix)));
107 }
108
109 /**
110 * <p>
111 * Create the output folder if it does not already exists.
112 * </p>
113 * @return the output folder.
114 * @throws IOException if the folder cannot be created.
115 */
116 private File createOutputFolder() throws IOException {
117 File folder = new File(config.getOutput().getFolder());
118 if (!folder.exists() && !folder.mkdirs()) {
119 throw new IOException(
120 String.format("Cannot create output folder '%s'.", folder.getAbsoluteFile()));
121 }
122 return folder;
123 }
124
125 }