提交 024066e4 编写于 作者: 沉默王二's avatar 沉默王二 💬

源码提交

上级 40ca6451
......@@ -64,6 +64,47 @@
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5-fluent</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>5.0.0-alpha.2</version>
</dependency>
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-core</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
......
package com.itwanger.alibaba;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public enum Singleton {
INSTANCE;
}
package com.itwanger.http;
import com.dtflys.forest.annotation.Body;
import com.dtflys.forest.annotation.Post;
import com.dtflys.forest.annotation.Request;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public interface ForRestClient {
@Post("http://httpbin.org/post")
String simplePost(@Body("name") String name);
}
package com.itwanger.http;
import com.dtflys.forest.config.ForestConfiguration;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class ForRestDemo {
public static void main(String[] args) {
// 实例化Forest配置对象
ForestConfiguration configuration = ForestConfiguration.configuration();
configuration.setBackendName("httpclient");
// 通过Forest配置对象实例化Forest请求接口
ForRestClient myClient = configuration.createInstance(ForRestClient.class);
// 调用Forest请求接口,并获取响应返回结果
String result = myClient.simplePost("二哥");
System.out.println(result);
}
}
package com.itwanger.http;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class HttpClientDemo {
public static void main(String[] args) throws URISyntaxException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://httpbin.org/post"))
.headers("Content-Type", "text/plain;charset=UTF-8")
.POST(HttpRequest.BodyPublishers.ofString("二哥牛逼"))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
}
package com.itwanger.http;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class HttpComponentsDemo {
public static void main(String[] args) throws IOException, IOException, ParseException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost("http://httpbin.org/post");
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("name", "二哥"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, Charset.forName("UTF-8")));
try (CloseableHttpResponse response2 = httpclient.execute(httpPost)) {
System.out.println(response2.getCode() + " " + EntityUtils.toString(response2.getEntity()));
}
}
}
}
package com.itwanger.http;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class HttpUrlConnectionDemo {
public static void main(String[] args) throws IOException {
String urlString = "https://httpbin.org/post";
String bodyString = "name=二哥";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(bodyString.getBytes("utf-8"));
os.flush();
os.close();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
System.out.println("响应内容:" + sb.toString());
} else {
System.out.println("响应码:" + conn.getResponseCode());
}
}
}
package com.itwanger.http;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class OkHttpPostDemo {
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, JSON);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void main(String[] args) throws IOException {
OkHttpPostDemo example = new OkHttpPostDemo();
String json = "{'name':'二哥'}";
String response = example.post("https://httpbin.org/post", json);
System.out.println(response);
}
}
package com.itwanger.thirty.box;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class Test {
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
long sum = 0L;
for (int i = 0; i < Integer.MAX_VALUE;i++) {
sum += i;
}
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
}
}
package com.itwanger.thirty.copy1;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class TestClone {
public static void main(String[] args) throws CloneNotSupportedException {
Writer writer1 = new Writer(18,"二哥");
Writer writer2 = (Writer) writer1.clone();
System.out.println("浅拷贝后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
writer2.setName("三妹");
System.out.println("调整了 writer2 的 name 后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
}
}
package com.itwanger.thirty.copy1;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class Writer implements Cloneable{
private int age;
private String name;
public Writer(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return super.toString().substring(26) + "{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package com.itwanger.thirty.copy2;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class Book {
private String bookName;
private int price;
public Book(String bookName, int price) {
this.bookName = bookName;
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return super.toString().substring(26) +
" bookName='" + bookName + '\'' +
", price=" + price +
'}';
}
}
package com.itwanger.thirty.copy2;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class TestClone {
public static void main(String[] args) throws CloneNotSupportedException {
Writer writer1 = new Writer(18,"二哥");
Book book1 = new Book("编译原理",100);
writer1.setBook(book1);
Writer writer2 = (Writer) writer1.clone();
System.out.println("浅拷贝后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
Book book2 = writer2.getBook();
book2.setBookName("永恒的图灵");
book2.setPrice(70);
System.out.println("writer2.book 变更后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
}
}
package com.itwanger.thirty.copy2;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class Writer implements Cloneable{
private int age;
private String name;
private Book book;
public Writer(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return super.toString().substring(26) +
" age=" + age +
", name='" + name + '\'' +
", book=" + book +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package com.itwanger.thirty.copy3;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class Book implements Cloneable{
private String bookName;
private int price;
public Book(String bookName, int price) {
this.bookName = bookName;
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return super.toString().substring(26) +
" bookName='" + bookName + '\'' +
", price=" + price +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package com.itwanger.thirty.copy3;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class TestClone {
public static void main(String[] args) throws CloneNotSupportedException {
Writer writer1 = new Writer(18,"二哥");
Book book1 = new Book("编译原理",100);
writer1.setBook(book1);
Writer writer2 = (Writer) writer1.clone();
System.out.println("深拷贝后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
Book book2 = writer2.getBook();
book2.setBookName("永恒的图灵");
book2.setPrice(70);
System.out.println("writer2.book 变更后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
}
}
package com.itwanger.thirty.copy3;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class Writer implements Cloneable{
private int age;
private String name;
private Book book;
public Writer(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return super.toString().substring(26) +
" age=" + age +
", name='" + name + '\'' +
", book=" + book +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
Writer writer = (Writer) super.clone();
writer.setBook((Book) writer.getBook().clone());
return writer;
}
}
package com.itwanger.thirty.copy4;
import java.io.Serializable;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class Book implements Serializable {
private String bookName;
private int price;
public Book(String bookName, int price) {
this.bookName = bookName;
this.price = price;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return super.toString().substring(26) +
" bookName='" + bookName + '\'' +
", price=" + price +
'}';
}
}
package com.itwanger.thirty.copy4;
import java.io.IOException;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class TestClone {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Writer writer1 = new Writer(18,"二哥");
Book book1 = new Book("编译原理",100);
writer1.setBook(book1);
Writer writer2 = (Writer) writer1.deepClone();
System.out.println("深拷贝后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
Book book2 = writer2.getBook();
book2.setBookName("永恒的图灵");
book2.setPrice(70);
System.out.println("writer2.book 变更后:");
System.out.println("writer1:" + writer1);
System.out.println("writer2:" + writer2);
}
}
package com.itwanger.thirty.copy4;
import java.io.*;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class Writer implements Serializable {
private int age;
private String name;
private Book book;
public Writer(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return super.toString().substring(26) +
" age=" + age +
", name='" + name + '\'' +
", book=" + book +
'}';
}
//深度拷贝
public Object deepClone() throws IOException, ClassNotFoundException {
// 序列化
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
// 反序列化
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
}
package com.itwanger.thirtyfive;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class StringInternDemo {
public static void main(String[] args) {
String s1 = new String("二哥") + new String("三妹");
String s2 = s1.intern();
System.out.println(s1 == s2);
}
}
package com.itwanger.thirtyfive;
import java.util.Random;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class StringInternDemo1 {
static final int MAX = 1000 * 10000;
static final String[] arr = new String[MAX];
public static void main(String[] args) throws Exception {
Integer[] DB_DATA = new Integer[10];
Random random = new Random(10 * 10000);
for (int i = 0; i < DB_DATA.length; i++) {
DB_DATA[i] = random.nextInt();
}
long t = System.currentTimeMillis();
for (int i = 0; i < MAX; i++) {
arr[i] = new String(String.valueOf(DB_DATA[i % DB_DATA.length]));
}
System.out.println((System.currentTimeMillis() - t) + "ms");
}
}
package com.itwanger.thirtyfive;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class StringInternDemo2 {
public static void main(String[] args) {
String s1 = new String("二哥三妹");
String s2 = s1.intern();
System.out.println(s1 == s2);
}
}
package com.itwanger.thirtyone;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class PrimitiveTypeDemo {
public static void main(String[] args) {
int age = 18;
modify(age);
System.out.println(age);
}
private static void modify(int age1) {
age1 = 30;
}
}
package com.itwanger.thirtyone;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class PrimitiveTypeDemo1 {
public static void main(String[] args) {
int age = 18;
age = modify(age);
System.out.println(age);
}
private static int modify(int age1) {
age1 = 30;
return age1;
}
}
package com.itwanger.thirtyone;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
class ReferenceTypeDemo {
public static void main(String[] args) {
String name = "二哥";
modify(name);
System.out.println(name);
}
private static void modify(String name1) {
name1 = "三妹";
}
}
package com.itwanger.thirtyone;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class Test {
public static void main(String[] args) {
int age = 18;
String name = "二哥";
}
}
package com.itwanger.thirtythree;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class StringTest {
public void test() {
int i = 8;
while ((i -= 3) > 0);
System.out.println("i = " + i);
}
public static void main(String[] args) {
StringTest hello = new StringTest();
for (int i = 0; i < 50_000; i++) {
hello.test();
}
}
}
package com.itwanger.twentyeight;
import java.util.ArrayList;
import java.util.Arrays;
/**
* @author 微信搜「沉默王二」,回复关键字 Java
*/
public class Test {
ArrayList a;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册