Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
testDir
testFile

*.class

# Mobile Tools for Java (J2ME)
Expand Down
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
group 'com.githun.nizshee'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Wed Oct 12 12:15:41 MSK 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
164 changes: 164 additions & 0 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'hw12'

48 changes: 48 additions & 0 deletions src/main/java/com/github/nizshee/client/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.nizshee.client;


import com.github.nizshee.shared.ListMethod;
import com.github.nizshee.shared.Method;
import com.github.nizshee.shared.RemoteFile;

import java.io.*;
import java.net.Socket;
import java.util.List;

@SuppressWarnings("all")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше подавлять конкретные варнинги и как можно ближе к месту подавления, иначе какой-н. важный варнинг в будущем будет подавлен

public class Client {

private final String host;
private final int port;

public Client(String host, int port) throws IOException {
this.host = host;
this.port = port;
}

public List<RemoteFile> getList(String name) throws IOException {
return Method.getValue(new Socket(host, port), 1, new ListMethod(), name);
}

public void getFile(String name) throws IOException {
try (Socket socket = new Socket(host, port)) {
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeInt(2);
dos.writeUTF(name);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush

DataInputStream dis = new DataInputStream(socket.getInputStream());
long size = dis.readLong();
byte[] buffer = new byte[1024];
File file = new File(name);
try (FileOutputStream fos = new FileOutputStream(file)) {
long total = 0;
int partSize = 0;
while (partSize != -1 && total < size) {
fos.write(buffer, 0, (int) Math.min(size - total, partSize));
total += partSize;
partSize = dis.read(buffer);
}
if (total != size) throw new IOException("Not enough bytes");
}
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/github/nizshee/client/ClientMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.nizshee.client;


import com.github.nizshee.shared.RemoteFile;

import java.util.List;

public class ClientMain {

public static void main(String[] args) throws Exception {
Client client = new Client("localhost", 8080);

if (args.length == 2 && args[0].equals("list")) {
List<RemoteFile> list = client.getList(args[1]);
for (RemoteFile file: list) {
System.out.print(file + " ");
if (file.isDirectory) {
System.out.println("<dir>");
} else {
System.out.println();
}
}
} else if (args.length == 2 && args[0].equals("get")) {
client.getFile(args[1]);
System.out.println("file downloaded");
} else {
System.out.println("usage: list <path> | get <file>");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а если аргументы неверные? тихонько выйдем и ничего не скажем?)

}
}
27 changes: 27 additions & 0 deletions src/main/java/com/github/nizshee/server/CopyFileHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.nizshee.server;


import java.io.*;


@SuppressWarnings("all")
public class CopyFileHandler implements Handler {
@Override
public void handle(DataInputStream dis, DataOutputStream dos) throws IOException {
String name = dis.readUTF();
File file = new File(name);
try (FileInputStream fis = new FileInputStream(file)) {
if (!file.exists()) {
dos.writeLong(0);
return;
}
dos.writeLong(file.length());
byte[] buffer = new byte[1024];
int size = 0;
while (size != -1) {
dos.write(buffer, 0, size);
size = fis.read(buffer);
}
}
}
}
Loading