-
Notifications
You must be signed in to change notification settings - Fork 0
Java02. ДЗ 02, Васильев Роман #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| testDir | ||
| testFile | ||
|
|
||
| *.class | ||
|
|
||
| # Mobile Tools for Java (J2ME) | ||
|
|
||
| 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' | ||
| } |
| 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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| rootProject.name = 'hw12' | ||
|
|
| 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") | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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>"); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а если аргументы неверные? тихонько выйдем и ничего не скажем?) |
||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше подавлять конкретные варнинги и как можно ближе к месту подавления, иначе какой-н. важный варнинг в будущем будет подавлен