Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartyAnsh authored Apr 20, 2019
2 parents 274cd5b + e164ab8 commit 550c59b
Show file tree
Hide file tree
Showing 157 changed files with 3,957 additions and 500 deletions.
18 changes: 0 additions & 18 deletions core-java-8-2/src/test/java/com/baeldung/UnitTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package com.baeldung.filechannel;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

public class FileChannelUnitTest {

@Test
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {

try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel();
ByteArrayOutputStream out = new ByteArrayOutputStream();) {

int bufferSize = 1024;
if (bufferSize > channel.size()) {
bufferSize = (int) channel.size();
}
ByteBuffer buff = ByteBuffer.allocate(bufferSize);

while (channel.read(buff) > 0) {
out.write(buff.array(), 0, buff.position());
buff.clear();
}

String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);

assertEquals("Hello world", fileContent);
}
}

@Test
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {

try (ByteArrayOutputStream out = new ByteArrayOutputStream();
FileInputStream fin = new FileInputStream("src/test/resources/test_read.in");
FileChannel channel = fin.getChannel();) {

int bufferSize = 1024;
if (bufferSize > channel.size()) {
bufferSize = (int) channel.size();
}
ByteBuffer buff = ByteBuffer.allocate(bufferSize);

while (channel.read(buff) > 0) {
out.write(buff.array(), 0, buff.position());
buff.clear();
}
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);

assertEquals("Hello world", fileContent);
}
}

@Test
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {

try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel();
ByteArrayOutputStream out = new ByteArrayOutputStream();) {

MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);

if (buff.hasRemaining()) {
byte[] data = new byte[buff.remaining()];
buff.get(data);
assertEquals("world", new String(data, StandardCharsets.UTF_8));
}
}
}

@Test
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
String file = "src/test/resources/test_write_using_filechannel.txt";
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
FileChannel channel = writer.getChannel();) {
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));

channel.write(buff);

// now we verify whether the file was written correctly
RandomAccessFile reader = new RandomAccessFile(file, "r");
assertEquals("Hello world", reader.readLine());
reader.close();
}
}

@Test
public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException {
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw");
FileChannel channel = reader.getChannel();
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) {

assertNotNull(fileLock);
}
}

@Test
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {

try (ByteArrayOutputStream out = new ByteArrayOutputStream();
RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel();) {

int bufferSize = 1024;
if (bufferSize > channel.size()) {
bufferSize = (int) channel.size();
}
ByteBuffer buff = ByteBuffer.allocate(bufferSize);

while (channel.read(buff) > 0) {
out.write(buff.array(), 0, buff.position());
buff.clear();
}

// the original file is 11 bytes long, so that's where the position pointer should be
assertEquals(11, channel.position());

channel.position(4);
assertEquals(4, channel.position());
}
}

@Test
public void whenGetFileSize_thenCorrect() throws IOException {

try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel();) {

// the original file is 11 bytes long, so that's where the position pointer should be
assertEquals(11, channel.size());
}
}

@Test
public void whenTruncateFile_thenCorrect() throws IOException {
String input = "this is a test input";

FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt");
FileChannel channel = fout.getChannel();

ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
channel.write(buff);
buff.flip();

channel = channel.truncate(5);
assertEquals(5, channel.size());

fout.close();
channel.close();
}
}
1 change: 1 addition & 0 deletions core-java-io/src/test/resources/test_truncate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
22 changes: 16 additions & 6 deletions core-kotlin-2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version '1.0-SNAPSHOT'


buildscript {
ext.kotlin_version = '1.2.41'
ext.kotlin_version = '1.3.30'

repositories {
mavenCentral()
Expand All @@ -26,8 +26,6 @@ sourceCompatibility = 1.8
compileKotlin { kotlinOptions.jvmTarget = "1.8" }
compileTestKotlin { kotlinOptions.jvmTarget = "1.8" }

kotlin { experimental { coroutines "enable" } }

repositories {
mavenCentral()
jcenter()
Expand All @@ -39,10 +37,22 @@ sourceSets {
srcDirs 'com/baeldung/ktor'
}
}
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}

dependencies {
compile "ch.qos.logback:logback-classic:1.2.1"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
implementation "ch.qos.logback:logback-classic:1.2.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}"
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testImplementation 'junit:junit:4.12'
testImplementation 'org.assertj:assertj-core:3.12.2'
testImplementation 'org.mockito:mockito-core:2.27.0'
testImplementation "org.jetbrains.kotlin:kotlin-test:${kotlin_version}"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5:${kotlin_version}"
}
Binary file modified core-kotlin-2/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion core-kotlin-2/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
18 changes: 17 additions & 1 deletion core-kotlin-2/gradlew
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env sh

#
# Copyright 2015 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.
#

##############################################################################
##
## Gradle start up script for UN*X
Expand Down Expand Up @@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
18 changes: 17 additions & 1 deletion core-kotlin-2/gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
Expand All @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
33 changes: 26 additions & 7 deletions core-kotlin-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${byte-buddy.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -37,6 +49,12 @@
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -69,10 +87,11 @@
</build>

<properties>
<kotlin.version>1.2.71</kotlin.version>
<junit.platform.version>1.1.1</junit.platform.version>
<junit.vintage.version>5.2.0</junit.vintage.version>
<kotlin.version>1.3.30</kotlin.version>
<junit.jupiter.version>5.4.2</junit.jupiter.version>
<mockito.version>2.27.0</mockito.version>
<byte-buddy.version>1.9.12</byte-buddy.version>
<assertj.version>3.10.0</assertj.version>
</properties>

</project>
</project>
Loading

0 comments on commit 550c59b

Please sign in to comment.