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.impl; 020 021import javax.inject.Named; 022import javax.inject.Singleton; 023 024import java.io.BufferedInputStream; 025import java.io.BufferedOutputStream; 026import java.io.BufferedReader; 027import java.io.File; 028import java.io.IOException; 029import java.io.InputStream; 030import java.io.InputStreamReader; 031import java.io.OutputStream; 032import java.io.UncheckedIOException; 033import java.nio.ByteBuffer; 034import java.nio.charset.StandardCharsets; 035import java.nio.file.Files; 036import java.nio.file.StandardCopyOption; 037 038/** 039 * A utility class helping with file-based operations. 040 * <p> 041 * The deprecated types it uses are referenced by their fully qualified names: with {@code --release 8}, javac reports 042 * an import of a deprecated type even in a deprecated class. 043 * 044 * @deprecated 045 */ 046@Deprecated 047@Singleton 048@Named 049public class DefaultFileProcessor implements org.eclipse.aether.spi.io.FileProcessor { 050 051 /** 052 * Thread-safe variant of {@link File#mkdirs()}. Creates the directory named by the given abstract pathname, 053 * including any necessary but nonexistent parent directories. Note that if this operation fails it may have 054 * succeeded in creating some of the necessary parent directories. 055 * 056 * @param directory The directory to create, may be {@code null}. 057 * @return {@code true} if and only if the directory was created, along with all necessary parent directories; 058 * {@code false} otherwise 059 */ 060 @Override 061 public boolean mkdirs(File directory) { 062 if (directory == null) { 063 return false; 064 } 065 066 if (directory.exists()) { 067 return false; 068 } 069 if (directory.mkdir()) { 070 return true; 071 } 072 073 File canonDir; 074 try { 075 canonDir = directory.getCanonicalFile(); 076 } catch (IOException e) { 077 throw new UncheckedIOException(e); 078 } 079 080 File parentDir = canonDir.getParentFile(); 081 return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir()); 082 } 083 084 @Override 085 public void write(File target, String data) throws IOException { 086 org.eclipse.aether.util.FileUtils.writeFile( 087 target.toPath(), p -> Files.write(p, data.getBytes(StandardCharsets.UTF_8))); 088 } 089 090 @Override 091 public void write(File target, InputStream source) throws IOException { 092 org.eclipse.aether.util.FileUtils.writeFile( 093 target.toPath(), p -> Files.copy(source, p, StandardCopyOption.REPLACE_EXISTING)); 094 } 095 096 @Override 097 public void copy(File source, File target) throws IOException { 098 copy(source, target, null); 099 } 100 101 @Override 102 public long copy(File source, File target, ProgressListener listener) throws IOException { 103 try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath())); 104 org.eclipse.aether.util.FileUtils.CollocatedTempFile tempTarget = 105 org.eclipse.aether.util.FileUtils.newTempFile(target.toPath()); 106 OutputStream out = new BufferedOutputStream(Files.newOutputStream(tempTarget.getPath()))) { 107 long result = copy(out, in, listener); 108 tempTarget.move(); 109 return result; 110 } 111 } 112 113 private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException { 114 long total = 0L; 115 byte[] buffer = new byte[1024 * 32]; 116 while (true) { 117 int bytes = is.read(buffer); 118 if (bytes < 0) { 119 break; 120 } 121 122 os.write(buffer, 0, bytes); 123 124 total += bytes; 125 126 if (listener != null && bytes > 0) { 127 try { 128 listener.progressed(ByteBuffer.wrap(buffer, 0, bytes)); 129 } catch (Exception e) { 130 // too bad 131 } 132 } 133 } 134 135 return total; 136 } 137 138 @Override 139 public void move(File source, File target) throws IOException { 140 if (!source.renameTo(target)) { 141 copy(source, target); 142 143 target.setLastModified(source.lastModified()); 144 145 source.delete(); 146 } 147 } 148 149 @Override 150 public String readChecksum(final File checksumFile) throws IOException { 151 String checksum; 152 try (BufferedReader br = new BufferedReader( 153 new InputStreamReader(Files.newInputStream(checksumFile.toPath()), StandardCharsets.UTF_8), 512)) { 154 checksum = DefaultChecksumProcessor.readFirstNonEmptyLine(br, checksumFile.toString()); 155 } 156 157 if (DefaultChecksumProcessor.isAlgorithmHeaderFormat(checksum)) { 158 int lastSpacePos = checksum.lastIndexOf(' '); 159 checksum = checksum.substring(lastSpacePos + 1); 160 } else { 161 int spacePos = checksum.indexOf(' '); 162 163 if (spacePos != -1) { 164 checksum = checksum.substring(0, spacePos); 165 } 166 } 167 168 return checksum; 169 } 170 171 @Override 172 public void writeChecksum(final File checksumFile, final String checksum) throws IOException { 173 // for now do exactly same as happened before, but FileProcessor is a component and can be replaced 174 write(checksumFile, checksum); 175 } 176}