-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Immueggpain
authored and
Immueggpain
committed
Dec 28, 2019
1 parent
3697ffa
commit 048e165
Showing
6 changed files
with
190 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/com/github/immueggpain/simplestreaming/StreamUpload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.github.immueggpain.simplestreaming; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.RandomAccessFile; | ||
import java.net.Socket; | ||
import java.util.concurrent.Callable; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Option; | ||
|
||
@Command(description = "Start uploader", name = "upload", mixinStandardHelpOptions = true, version = Launcher.VERSTR) | ||
public class StreamUpload implements Callable<Void> { | ||
|
||
@Option(names = { "-p", "--port" }, required = true, description = "server's upload port") | ||
public int serverPort; | ||
@Option(names = { "-s", "--server" }, required = true, description = "server's name(ip or domain)") | ||
public String serverName; | ||
@Option(names = { "-f", "--file" }, required = true, description = "path to ts file") | ||
public String filepath; | ||
|
||
@Override | ||
public Void call() throws Exception { | ||
try { | ||
Socket socket = new Socket(serverName, serverPort); | ||
|
||
OutputStream os = socket.getOutputStream(); | ||
byte[] buf = new byte[Launcher.BUFLEN]; | ||
|
||
RandomAccessFile file = new RandomAccessFile(filepath, "r"); | ||
file.seek(file.length()); | ||
|
||
try { | ||
while (true) { | ||
long pos = copyLarge(file, os, buf); | ||
// to EOF, check if file is overwriten | ||
if (file.length() < pos) { | ||
file.close(); | ||
file = new RandomAccessFile(filepath, "r"); | ||
file.seek(file.length()); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
System.out.println("close socket"); | ||
file.close(); | ||
socket.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private static long copyLarge(final RandomAccessFile input, final OutputStream output, final byte[] buffer) | ||
throws IOException { | ||
long count = 0; | ||
int n; | ||
while (-1 != (n = input.read(buffer))) { | ||
output.write(buffer, 0, n); | ||
count += n; | ||
} | ||
return input.getFilePointer(); | ||
} | ||
|
||
} |