Skip to content

Instantly share code, notes, and snippets.

View ldclakmal's full-sized avatar
🎯
Focusing

Chanaka Lakmal ldclakmal

🎯
Focusing
View GitHub Profile
public static void main(String[] arg) throws IOException, JSchException {
String remoteA = "/tmp/scp/remote-a/";
String remoteB = "/tmp/scp/remote-b/";
String local = "/tmp/scp/local/";
String fileName = "abc.txt";
String user = "chanaka";
String host = "localhost";
int port = 22;
@ldclakmal
ldclakmal / SCPTo.java
Last active February 14, 2017 20:42
Secure copy files to remote server
private static void copyLocalToRemote(Session session, String from, String to, String fileName) throws JSchException, IOException {
boolean ptimestamp = true;
from = from + File.separator + fileName;
// exec 'scp -t rfile' remotely
String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + to;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
@ldclakmal
ldclakmal / SCPFrom.java
Last active February 14, 2017 20:42
Secure copy files from remote server
private static void copyRemoteToLocal(Session session, String from, String to, String fileName) throws JSchException, IOException {
from = from + File.separator + fileName;
String prefix = null;
if (new File(to).isDirectory()) {
prefix = to + File.separator;
}
// exec 'scp -f rfile' remotely
String command = "scp -f " + from;
@ldclakmal
ldclakmal / PathMatcher.java
Last active January 12, 2017 14:47
Check whether picked file matches with the given path pattern
public boolean isMatchPattern(GRPattern GRPattern, Path file) {
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(GRPattern.getPatternSyntax() +
Paths.get(GRPattern.getPathPattern()).toString());
Path name = file.getFileName();
return name != null && matcher.matches(file);
}
@ldclakmal
ldclakmal / CreateWatchKey.java
Last active January 12, 2017 19:12
Create a watch key and poll the events
WatchKey key = watcher.take();
for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
}
@ldclakmal
ldclakmal / CreateWatchService.java
Last active January 8, 2017 12:28
Create a WatchService
Path dir = Paths.get("/tmp");
WatchService watcher = FileSystems.getDefault().newWatchService();
WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
@ldclakmal
ldclakmal / BinarySearchTree.cpp
Created January 25, 2015 08:01
Binary Search Tree
// BinarySearchTree.cpp : Defines the entry point for the console application.
// L.D.C.Lakmal
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef int element;
@ldclakmal
ldclakmal / BinaryTree.cpp
Created January 25, 2015 03:38
Binary Tree
// BinaryTree.cpp : Defines the entry point for the console application.
// L.D.C.Lakmal
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef int element;
@ldclakmal
ldclakmal / DynamicArray.java
Created January 24, 2015 16:03
Dynamic Array
import java.io.PrintWriter;
import java.util.Random;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
@ldclakmal
ldclakmal / Dictionary.cpp
Created January 24, 2015 16:02
Dictionary
// Dictionary.cpp : Defines the entry point for the console application.
// L.D.C.Lakmal
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;