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        try (FileOutputStream fos = new FileOutputStream(file)) {
067            if (data != null) {
068                fos.write(data.getBytes(StandardCharsets.UTF_8));
069            }
070        }
071    }
072
073    public void write(File target, InputStream source) throws IOException {
074        mkdirs(target.getAbsoluteFile().getParentFile());
075
076        try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
077            copy(fos, source, null);
078        }
079    }
080
081    public void copy(File source, File target) throws IOException {
082        copy(source, target, null);
083    }
084
085    public long copy(File source, File target, ProgressListener listener) throws IOException {
086        long total = 0;
087
088        mkdirs(target.getAbsoluteFile().getParentFile());
089
090        try (InputStream fis = new FileInputStream(source);
091                OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
092            total = copy(fos, fis, listener);
093        }
094
095        return total;
096    }
097
098    private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
099        long total = 0L;
100
101        ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
102        byte[] array = buffer.array();
103
104        while (true) {
105            int bytes = is.read(array);
106            if (bytes < 0) {
107                break;
108            }
109
110            os.write(array, 0, bytes);
111
112            total += bytes;
113
114            if (listener != null && bytes > 0) {
115                try {
116                    ((Buffer) buffer).rewind();
117                    ((Buffer) buffer).limit(bytes);
118                    listener.progressed(buffer);
119                } catch (Exception e) {
120                    // too bad
121                }
122            }
123        }
124
125        return total;
126    }
127
128    public void move(File source, File target) throws IOException {
129        target.delete();
130
131        if (!source.renameTo(target)) {
132            copy(source, target);
133
134            target.setLastModified(source.lastModified());
135
136            source.delete();
137        }
138    }
139
140    @Override
141    public String readChecksum(final File checksumFile) throws IOException {
142        return new String(Files.readAllBytes(checksumFile.toPath()), StandardCharsets.UTF_8);
143    }
144
145    @Override
146    public void writeChecksum(final File checksumFile, final String checksum) throws IOException {
147        write(checksumFile, checksum);
148    }
149}