001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.test.util;
020
021import java.io.BufferedOutputStream;
022import java.io.File;
023import java.io.FileInputStream;
024import java.io.FileOutputStream;
025import java.io.IOException;
026import java.io.InputStream;
027import java.io.OutputStream;
028import java.nio.Buffer;
029import java.nio.ByteBuffer;
030import java.nio.charset.StandardCharsets;
031import java.nio.file.Files;
032
033import org.eclipse.aether.spi.io.FileProcessor;
034
035/**
036 * A simple file processor implementation to help satisfy component requirements during tests.
037 */
038public class TestFileProcessor implements FileProcessor {
039
040    public boolean mkdirs(File directory) {
041        if (directory == null) {
042            return false;
043        }
044
045        if (directory.exists()) {
046            return false;
047        }
048        if (directory.mkdir()) {
049            return true;
050        }
051
052        File canonDir = null;
053        try {
054            canonDir = directory.getCanonicalFile();
055        } catch (IOException e) {
056            return false;
057        }
058
059        File parentDir = canonDir.getParentFile();
060        return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
061    }
062
063    public void write(File file, String data) throws IOException {
064        mkdirs(file.getParentFile());
065
066        FileOutputStream fos = null;
067        try {
068            fos = new FileOutputStream(file);
069
070            if (data != null) {
071                fos.write(data.getBytes(StandardCharsets.UTF_8));
072            }
073
074            fos.close();
075            fos = null;
076        } finally {
077            try {
078                if (fos != null) {
079                    fos.close();
080                }
081            } catch (final IOException e) {
082                // Suppressed due to an exception already thrown in the try block.
083            }
084        }
085    }
086
087    public void write(File target, InputStream source) throws IOException {
088        mkdirs(target.getAbsoluteFile().getParentFile());
089
090        OutputStream fos = null;
091        try {
092            fos = new BufferedOutputStream(new FileOutputStream(target));
093
094            copy(fos, source, null);
095
096            fos.close();
097            fos = null;
098        } finally {
099            try {
100                if (fos != null) {
101                    fos.close();
102                }
103            } catch (final IOException e) {
104                // Suppressed due to an exception already thrown in the try block.
105            }
106        }
107    }
108
109    public void copy(File source, File target) throws IOException {
110        copy(source, target, null);
111    }
112
113    public long copy(File source, File target, ProgressListener listener) throws IOException {
114        long total = 0;
115
116        InputStream fis = null;
117        OutputStream fos = null;
118        try {
119            fis = new FileInputStream(source);
120
121            mkdirs(target.getAbsoluteFile().getParentFile());
122
123            fos = new BufferedOutputStream(new FileOutputStream(target));
124
125            total = copy(fos, fis, listener);
126
127            fos.close();
128            fos = null;
129
130            fis.close();
131            fis = null;
132        } finally {
133            try {
134                if (fos != null) {
135                    fos.close();
136                }
137            } catch (final IOException e) {
138                // Suppressed due to an exception already thrown in the try block.
139            } finally {
140                try {
141                    if (fis != null) {
142                        fis.close();
143                    }
144                } catch (final IOException e) {
145                    // Suppressed due to an exception already thrown in the try block.
146                }
147            }
148        }
149
150        return total;
151    }
152
153    private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
154        long total = 0L;
155
156        ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
157        byte[] array = buffer.array();
158
159        while (true) {
160            int bytes = is.read(array);
161            if (bytes < 0) {
162                break;
163            }
164
165            os.write(array, 0, bytes);
166
167            total += bytes;
168
169            if (listener != null && bytes > 0) {
170                try {
171                    ((Buffer) buffer).rewind();
172                    ((Buffer) buffer).limit(bytes);
173                    listener.progressed(buffer);
174                } catch (Exception e) {
175                    // too bad
176                }
177            }
178        }
179
180        return total;
181    }
182
183    public void move(File source, File target) throws IOException {
184        target.delete();
185
186        if (!source.renameTo(target)) {
187            copy(source, target);
188
189            target.setLastModified(source.lastModified());
190
191            source.delete();
192        }
193    }
194
195    @Override
196    public String readChecksum(final File checksumFile) throws IOException {
197        return new String(Files.readAllBytes(checksumFile.toPath()), StandardCharsets.UTF_8);
198    }
199
200    @Override
201    public void writeChecksum(final File checksumFile, final String checksum) throws IOException {
202        write(checksumFile, checksum);
203    }
204}