Here is the one liner that appends the string with a prefix if the string doesn’t have a prefix
1 2 |
String newString = oldString.startsWith("someprefix") ? oldString : "someprefix" + oldString; |
Here is the one liner that appends the string with a prefix if the string doesn’t have a prefix
1 2 |
String newString = oldString.startsWith("someprefix") ? oldString : "someprefix" + oldString; |
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods. Maven If you use Maven to manage the dependencies in your Java project, you do not need to download; just place the following into your […]
Here is a real world example of using java.util.ArrayList add method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
/* Soot - a J*va Optimization Framework * Copyright (C) 2003 Jennifer Lhotak * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ import java.util.ArrayList; import java.util.Iterator; import org.eclipse.jface.action.*; /** * Launches Soot with -f J for all classes in output dir of * selected project */ public class SootJimpleJavaProjectLauncher extends SootProjectLauncher { public void run(IAction action) { super.run(action); setCmd(); runSootDirectly(); runFinish(); } private void setCmd() { ArrayList commands = new ArrayList(); commands.add("--"+LaunchCommands.SOOT_CLASSPATH); Iterator it = getJavaProcessPath().iterator(); String cp = (String)it.next(); while (it.hasNext()){ cp = cp + getSootClasspath().getSeparator() + (String)it.next(); } cp = cp + getSootClasspath().getSeparator() + getClasspathAppend(); commands.add(cp); commands.add("--"+LaunchCommands.OUTPUT_DIR); commands.add(getOutputLocation()); getSootCommandList().addSingleOpt("--"+LaunchCommands.KEEP_LINE_NUMBER); getSootCommandList().addSingleOpt("--"+LaunchCommands.XML_ATTRIBUTES); Iterator it2 = getJavaProcessPath().iterator(); while (it2.hasNext()){ commands.add("--"+LaunchCommands.PROCESS_PATH); commands.add((String)it2.next()); } getSootCommandList().addDoubleOpt("--"+LaunchCommands.OUTPUT, LaunchCommands.JIMPLE_OUT); getSootCommandList().addDoubleOpt("--"+LaunchCommands.SRC_PREC,"java"); getSootCommandList().addSingleOpt(commands); } } |
Here is a nice example of sorting of a list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.javagist.sort; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * This program sorts a list of Strings in ascending and descending order. * * @author javagist * */ public class SortExample { private List<String> namesList = Arrays.asList("Kevin", "Brian" , "Steve", "Bill", "Obama"); public static void main(String[] args) { SortExample sortExample = new SortExample(); System.out.println("Before sorting:" + sortExample.namesList); sortExample.sortList(sortExample.namesList); System.out.println("List in ascending order:" + sortExample.namesList); sortExample.reverseSortList(sortExample.namesList); System.out.println("List in descending orde :" + sortExample.namesList); // sort a new list and don't modify the order of original list List<String> keepOriginalOrderList = Arrays.asList("Kevin", "Brian" , "Steve", "Bill", "Obama"); List<String> sortedList = sortExample.sortListWithoutModifyingOriginalList(keepOriginalOrderList); System.out.println("List has preserved order :" + keepOriginalOrderList); System.out.println("New sorted list :" + sortedList); } /** * This method makes it obvious that any object that extends comparable can be sorted. * @param list - list to be sorted */ private <E extends Comparable<E>> void sortList(List<E> list){ Collections.sort(list); } /** * This method makes it obvious that any object that extends comparable can be sorted. * @param list - list to be reverse sorted */ private <E extends Comparable<E>> void reverseSortList(List<E> list){ Collections.sort(list, Collections.reverseOrder()); } /** * This method preserves the original List ordering. And returns a new sorted list * @param list - list to be sorted but not modified */ private <E extends Comparable<E>> List<E> sortListWithoutModifyingOriginalList(List<E> list){ List<E> newList = new ArrayList<E>(list); Collections.sort(newList); return newList; } } |
References http://programtalk.com/java/sort-list-java/
This example demonstrates how forwarding X11 connections from a remote host can be accomplished.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import net.schmizz.sshj.SSHClient; import net.schmizz.sshj.common.StreamCopier; import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.connection.channel.direct.Session.Command; import net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener; import java.io.IOException; import java.net.InetSocketAddress; public class X11 { public static void main(String... args) throws IOException, InterruptedException { final SSHClient ssh = new SSHClient(); // Compression makes X11 more feasible over slower connections // ssh.useCompression(); ssh.loadKnownHosts(); // NOTE: Forwarding incoming X connections to localhost:6000 only works if X is started without the // "-nolisten tcp" option (this is usually not the default for good reason) ssh.registerX11Forwarder(new SocketForwardingConnectListener(new InetSocketAddress("localhost", 6000))); ssh.connect("localhost"); try { ssh.authPublickey(System.getProperty("user.name")); Session sess = ssh.startSession(); // It is recommendable to send a fake cookie, and in your ConnectListener when a connection comes in replace // it with the real one. But here simply one from `xauth list` is being used. sess.reqX11Forwarding("MIT-MAGIC-COOKIE-1", "b0956167c9ad8f34c8a2788878307dc9", 0); final Command cmd = sess.exec("/usr/X11/bin/xcalc"); new StreamCopier(cmd.getInputStream(), System.out).spawn("stdout"); new StreamCopier(cmd.getErrorStream(), System.err).spawn("stderr"); // Wait for session & X11 channel to get closed ssh.getConnection().join(); } finally { ssh.disconnect(); } } } |
A simple web crawler that uses a Retrofit service to turn URLs into webpages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
/* * Copyright (C) 2016 Square, Inc. * * 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. */ package com.example.retrofit; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import okhttp3.ConnectionPool; import okhttp3.Dispatcher; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.ResponseBody; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Converter; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.http.GET; import retrofit2.http.Url; public final class Crawler { private final Set fetchedUrls = Collections.synchronizedSet( new LinkedHashSet()); private final ConcurrentHashMap hostnames = new ConcurrentHashMap<>(); private final PageService pageService; public Crawler(PageService pageService) { this.pageService = pageService; } public void crawlPage(HttpUrl url) { // Skip hosts that we've visited many times. AtomicInteger hostnameCount = new AtomicInteger(); AtomicInteger previous = hostnames.putIfAbsent(url.host(), hostnameCount); if (previous != null) hostnameCount = previous; if (hostnameCount.incrementAndGet() > 100) return; // Asynchronously visit URL. pageService.get(url).enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (!response.isSuccessful()) { System.out.println(call.request().url() + ": failed: " + response.code()); return; } // Print this page's URL and title. Page page = response.body(); HttpUrl base = response.raw().request().url(); System.out.println(base + ": " + page.title); // Enqueue its links for visiting. for (String link : page.links) { HttpUrl linkUrl = base.resolve(link); if (linkUrl != null && !fetchedUrls.add(linkUrl)) { crawlPage(linkUrl); } } } @Override public void onFailure(Call call, Throwable t) { System.out.println(call.request().url() + ": failed: " + t); } }); } public static void main(String... args) throws Exception { Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20)); dispatcher.setMaxRequests(20); dispatcher.setMaxRequestsPerHost(1); OkHttpClient okHttpClient = new OkHttpClient.Builder() .dispatcher(dispatcher) .connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS)) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(HttpUrl.parse("https://example.com/")) .addConverterFactory(PageAdapter.FACTORY) .client(okHttpClient) .build(); PageService pageService = retrofit.create(PageService.class); Crawler crawler = new Crawler(pageService); crawler.crawlPage(HttpUrl.parse(args[0])); } interface PageService { @GET Call get(@Url HttpUrl url); } static class Page { public final String title; public final List links; public Page(String title, List links) { this.title = title; this.links = links; } } static final class PageAdapter implements Converter { static final Converter.Factory FACTORY = new Converter.Factory() { @Override public Converter responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { if (type == Page.class) return new PageAdapter(); return null; } }; @Override public Page convert(ResponseBody responseBody) throws IOException { Document document = Jsoup.parse(responseBody.string()); List links = new ArrayList<>(); for (Element element : document.select("a[href]")) { links.add(element.attr("href")); } return new Page(document.title(), Collections.unmodifiableList(links)); } } } |
Convert byte to Hexadecimal
1 2 3 4 5 6 7 8 9 10 11 |
public static String bytesToHex(byte[] bytes) { final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] hexChars = new char[bytes.length * 2]; int v; for (int j = 0; j < bytes.length; j++) { v = bytes[j] & 0xFF; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); } |
Convert Hexadecimal to byte
1 2 3 4 5 6 7 8 |
public static byte[] hexToBytes(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } |
Here is a code example that shows how to download and upload a file through SFTP in java Download File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; /** * * @author javagists.com * */ public class DownloadFileSFTP { public static void main(String[] args) throws Exception { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("admin", "127.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("pass"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.get("/tmpremote/testDownload.txt", "/tmplocal/testDownload.txt"); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } } |
Upload File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; /** * * @author javagists.com * */ public class UploadFileSFTP { public static void main(String[] args) throws Exception { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("admin", "127.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("pass"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.put("/tmplocal/testUpload.txt", "/tmpremote/testUpload.txt"); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } } |