Skip to content

Commit

Permalink
Add part 1 : Cat program
Browse files Browse the repository at this point in the history
  • Loading branch information
ledoyen committed Jul 13, 2024
1 parent ac1beae commit 2cc35ff
Show file tree
Hide file tree
Showing 22 changed files with 1,422 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

charset = utf-8

indent_style = space
indent_size = 4

[*.yml]
indent_size = 2
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: maven
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
13 changes: 13 additions & 0 deletions .github/workflows/automerge-dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Dependabot auto-merge

on: pull_request_target

permissions:
pull-requests: write
contents: write

jobs:
dependabot:
if: ${{ github.actor == 'dependabot[bot]' }}
uses: lernejo/reusable-workflows/.github/workflows/automerge-dependabot.yml@main
secrets: inherit
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Build

on:
push:
workflow_dispatch:

jobs:
build:
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
uses: lernejo/reusable-workflows/.github/workflows/ci.yml@main
secrets: inherit
with:
runs-on: ${{ matrix.os }}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
target/
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/
*.iml

### Mac OS ###
.DS_Store
117 changes: 117 additions & 0 deletions .mvn/wrapper/MavenWrapperDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2007-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;

public class MavenWrapperDownloader {

private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";

/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";

/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";

/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";

public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());

// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if(mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if(mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);

File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if(!outputFile.getParentFile().exists()) {
if(!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}

private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}

}
Binary file added .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.8/apache-maven-3.9.8-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
45 changes: 45 additions & 0 deletions EXERCISE_fr.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
= Lire des fichiers en Java

ifdef::env-github[]
:tip-caption: :bulb:
:note-caption: :information_source:
endif::[]
:hardbreaks-option:

Préfixé par ✔️, des "checkpoints" pour vous aider à vérifier que vous avez tout bon.

== Objectif

Manipuler différentes façons de lire un fichier, et apprendre comment traiter le contenu comme un flux pour limiter l'utilisation de la mémoire.

== Prérequis

* Git
* Java 21
* Maven 3.9.x
* (Optionnel, mais fortement recommandé) IntelliJ edition _community_ 2024

---

* Sur la page du template https://github.com/lernejo/maven-starter-template, cliquer sur "Use this template"
* ⚠️ Renseigner comme nom de dépôt : *java_load_file_training*
* Marquer le futur dépôt comme *private*
* Une fois le dépôt créé, installer l'app https://github.com/apps/korekto[Korekto]
* Cloner le dépôt en utilisant l'*url SSH*
* La branche par défaut est la branche *main*, c'est sur celle-ci que nous allons travailler

== Partie 1

Afficher le contenu d'un fichier dans la console

* Créer une nouvelle classe `fr.lernejo.file.Cat`
* Créer une fonction `main` de telle sorte que :
** Le programme prenne un chemin (relatif ou absolu) comme unique argument
** En cas d'absence d'argument, le programme sort avec le code 3 et le message "Missing argument" dans la sortie standard
** Dans le cas de deux arguments ou plus, le programme sort avec le code 4 et le message "Too many arguments" dans la sortie standard
** Dans le cas où le fichier indiqué n'existe pas, le programme sort avec le code 5 et le message "File not found"
** Dans le cas où le chemin indiqué est un répertoire, le programme sort avec le code 6 et le message "A file is required"
** Dans le cas où le fichier fait plus de 3KiB (soit 3*1024 = 3072 bytes), le programme sort avec le code 7 et le message "File too large"
** Dans le cas où le fichier est lisible et fait moins de 3KiB, afficher le contenu du fichier dans la console et sortir

* ✔️ Running `java -jar target/maven-training.jar README.md` should display the content of the `README.md` file
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# korekto-load-file-grader
An exercise about loading files in Java
[![Build](https://github.com/lernejo/korekto-load-file-grader/actions/workflows/ci.yml/badge.svg)](https://github.com/lernejo/korekto-load-file-grader/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/lernejo/korekto-load-file-grader/branch/main/graph/badge.svg?token=A6kYtPT5DX)](https://codecov.io/gh/lernejo/korekto-load-file-grader)
![License](https://img.shields.io/badge/License-Elastic_License_v2-blue)

Korekto grader & exercise about loading files in Java.

Exercise subject: [here](EXERCISE_fr.adoc)
Loading

0 comments on commit 2cc35ff

Please sign in to comment.