Initial commit

This commit is contained in:
github-classroom[bot]
2022-04-28 08:39:46 +00:00
commit 2a34df16f4
47 changed files with 6848 additions and 0 deletions
@@ -0,0 +1,50 @@
/*
* Gradle build configuration for specific lab module / exercise
*/
// enabled plugins
plugins {
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
// Project/Module information
description = 'Lab05 ByteCharStream Solution'
group = 'ch.zhaw.prog2'
version = '2022.1'
// Dependency configuration
repositories {
mavenCentral()
}
dependencies {
}
// Configuration for Application plugin
application {
// Define the main class for the application.
mainClass = 'ch.zhaw.prog2.io.FileCopy'
}
// enable console input when running with gradle
run {
standardInput = System.in
}
// Java plugin configuration
java {
// By default the Java version of the gradle process is used as source/target version.
// This can be overridden, to ensure a specific version. Enable only if required.
// sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
// targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
// Java compiler specific options
compileJava {
// source files should be UTF-8 encoded
options.encoding = 'UTF-8'
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
}
}
@@ -0,0 +1,30 @@
ENGINE
Engine 449cc, 4-stroke, liquid-cooled, single cylinder, DOHC
Bore Stroke 96.0 x 62.1 mm (3.78 x 2.4 in)
Compression Ratio 12.5:1
Fuel System Fuel Injection
Starter Primary kick
Lubrication Semi-dry sump
DRIVE TRAIN
Transmission 5-speed constant mesh
Clutch Wet multi-plate type, manual release
Final Drive Chain, DID520MXV4, 114 links
CHASSIS
Suspension Front Inverted telescopic, air spring, oil damped
Suspension Rear Link type, coil spring, oil damped
Brakes Front Disc brake, single rotor
Brakes Rear Disc brake, single rotor
Tires Front 80/100-21 51M, tube type
Tires Rear 110/90-19 62M, tube type
Fuel Tank Capacity 6.2 L (1.6 US gallons)
Color Champion Yellow No.2 / Solid Black
ELECTRICAL
Ignition Electronic Ignition (CDI)
DIMENSIONS
Overall Length 2190 mm (86.2 in)
Overall Width 830 mm (32.7 in)
Overall Height 1270 mm (50.0 in)
Wheelbase 1495 mm (58.9 in)
Ground Clearance 325 mm (12.8 in)
Seat Height 955 mm (37.6 in)
Curb Weight 112 kg (247 lbs)
Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

@@ -0,0 +1,111 @@
package ch.zhaw.prog2.io;
import java.io.*;
import java.util.*;
public class FileCopy {
public static void main(String[] args) throws IOException {
// get the filename from the arguments. By default, use 'files'-directory in current working directory.
String sourceDirPath = args.length >= 1 ? args[0] : "./files";
File sourceDir = new File(sourceDirPath);
// Part a Verify the directory structure
// Implement the method 'verifySourceDir()'
System.out.format("Verifying Source Directory: %s%n", sourceDirPath);
try {
verifySourceDir(sourceDir);
} catch (FileNotFoundException error) {
System.err.format("Directory %s does not comply with predefined structure: %s%n", sourceDir.getPath(), error.getMessage());
System.err.println("Terminating programm!");
System.exit(1);
}
System.out.printf("Source Directory verified successfully.");
// Part b Copy the files byte resp. char wise.
// Implement the method 'verifySourceDir()'
System.out.println("Initiating file copies.");
try {
copyFiles(sourceDir);
} catch (IOException error) {
System.err.format("Error creating file copies: %s%n", error.getMessage());
System.err.println("Terminating programm!");
System.exit(2);
}
System.out.println("Files copied successfully.");
}
/**
* Part a directory structure
*
* Verify the directory structure for correctness.
* Correct means, that the directory exists and beside the two files rmz450.jpg and rmz450-spec.txt does not contain
* any other files or directories.
*
* @param sourceDir File source directory to verify it contains the correct structure
* @throws FileNotFoundException if the source directory or required file are missing
* @throws InvalidObjectException if the source directory contains invalid files or directories.
*/
private static void verifySourceDir(File sourceDir) throws FileNotFoundException, InvalidObjectException {
if (sourceDir.isDirectory()) {
System.out.format("Directory %s exists.", sourceDir.getPath());
} else {
throw new FileNotFoundException("Directory %s does not exist.".formatted(sourceDir.getPath()));
}
// mutable list of required files
List<String> requiredFiles = new ArrayList<>(List.of("rmz450.jpg", "rmz450-spec.txt" ));
// check all files in the directory
for (File file : sourceDir.listFiles()) {
// if found, remove file from required list, otherwise throw an error (invalid file)
if (!requiredFiles.remove(file.getName())) {
throw new InvalidObjectException("Directory %s contains invalid element %s".formatted(sourceDir.getPath(), file.getName()));
}
// check for valid file type
if (file.isDirectory()) {
throw new InvalidObjectException("File %s is a directory. Must be a standard file.".formatted(file.getName()));
}
}
// verify all required files are available
if (!requiredFiles.isEmpty()) {
throw new FileNotFoundException("Required file(s) not found:" + String.join(", ", requiredFiles));
}
}
/**
* Teilaufgabe b Kopieren von Dateien
*
* Copies each file of the source directory twice, once character-oriented and once byte-oriented.
* Source and target files should be opened and copied byte by byte respectively char by char.
* The target files should be named, so the type of copy can be identified.
*
* @param sourceDir File representing the source directory containing the files to copy
* @throws IOException if an error is happening while copying the files
*/
private static void copyFiles(File sourceDir) throws IOException {
Objects.requireNonNull(sourceDir, "Source directory must not be null");
for (File file : sourceDir.listFiles()) {
try (FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream("copy-bin-" + file.getName());
FileReader reader = new FileReader(file);
FileWriter writer = new FileWriter("copy-char-" + file.getName()))
{
int byteValue;
System.out.println("Binary copy.");
while ((byteValue = inputStream.read()) >= 0) {
outputStream.write(byteValue);
}
int charValue;
System.out.println("Character-oriented copy.");
while ((charValue = reader.read()) >= 0) {
writer.write(charValue);
}
}
}
}
}