Skip to content

Commit

Permalink
Merge pull request #37 from Jayzyyzyy/master
Browse files Browse the repository at this point in the history
3.12作业
  • Loading branch information
jodie-zss authored Mar 12, 2017
2 parents ff4895d + e40632f commit 91eb89f
Show file tree
Hide file tree
Showing 36 changed files with 1,169 additions and 527 deletions.
1 change: 1 addition & 0 deletions group15/1511_714512544/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

871 changes: 344 additions & 527 deletions group15/1511_714512544/.idea/workspace.xml

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

1. 每个结点的平衡因子就是该结点的左子树的高度减去右子树的高度,平衡二叉树的每个结点的平衡因子的绝对值不会超过2。
2. 右旋口诀:左子作父,父为右子,右孙变左孙
左旋口诀:右子作父,父为左子,左孙变右孙

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ArrayUtil {
* @return
*/
public void reverseArray(int[] origin){
if(origin == null) return ; //边界条件
int len = origin.length;
int temp = 0;
for (int i = 0; i < len/2; i++) {
Expand All @@ -30,6 +31,7 @@ public void reverseArray(int[] origin){
*/

public int[] removeZero(int[] oldArray){
if(oldArray == null) return null; //边界条件
int[] temp = new int[oldArray.length];
int index = 0;
for (int i : oldArray) {
Expand All @@ -52,6 +54,10 @@ public int[] removeZero(int[] oldArray){
*/

public int[] merge(int[] array1, int[] array2){
if(array1 == null && array2 == null ) return null;
if(array1 == null) return array2;
if(array2 == null) return array1;

int[] array3 = new int[array1.length+array2.length];
for (int i = 0; i < array1.length; i++) {
array3[i] = array1[i];
Expand Down
99 changes: 99 additions & 0 deletions group15/1511_714512544/src/com/coderising/download/Demo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.coderising.download;

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class Demo {
public static String path = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png";
public static int threadCount = 3;
public static void main(String[] args) throws Exception{
//1.连接服务器,获取一个文件,获取文件的长度,在本地创建一个跟服务器一样大小的临时文件
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
//服务器端返回的数据的长度,实际上就是文件的长度
int length = conn.getContentLength();
System.out.println("文件总长度:"+length);
//在客户端本地创建出来一个大小跟服务器端一样大小的临时文件
RandomAccessFile raf = new RandomAccessFile("d:/1.png", "rwd");
//指定创建的这个文件的长度
raf.setLength(length);
raf.close();
//假设是3个线程去下载资源。
//平均每一个线程下载的文件大小.
int blockSize = length / threadCount;
for (int threadId = 1; threadId <= threadCount; threadId++) {
//第一个线程下载的开始位置
int startIndex = (threadId - 1) * blockSize;
int endIndex = threadId * blockSize - 1;
if (threadId == threadCount) {//最后一个线程下载的长度要稍微长一点
endIndex = length;
}
System.out.println("线程:"+threadId+"下载:---"+startIndex+"--->"+endIndex);
new DownLoadThread(path, threadId, startIndex, endIndex).start();
}

}else {
System.out.printf("服务器错误!");
}
}

/**
* 下载文件的子线程 每一个线程下载对应位置的文件
* @author jie
*
*/
public static class DownLoadThread extends Thread{
private int threadId;
private int startIndex;
private int endIndex;
/**
* @param path 下载文件在服务器上的路径
* @param threadId 线程Id
* @param startIndex 线程下载的开始位置
* @param endIndex 线程下载的结束位置
*/
public DownLoadThread(String path, int threadId, int startIndex, int endIndex) {
super();
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}

@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
//重要:请求服务器下载部分文件 指定文件的位置
conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
//从服务器请求全部资源返回200 ok如果从服务器请求部分资源 返回 206 ok
int code = conn.getResponseCode();
System.out.println("code:"+code);
InputStream is = conn.getInputStream();//已经设置了请求的位置,返回的是当前位置对应的文件的输入流
RandomAccessFile raf = new RandomAccessFile("d:/1.png", "rwd");
//随机写文件的时候从哪个位置开始写
raf.seek(startIndex);//定位文件

int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
raf.write(buffer, 0, len);
}
is.close();
raf.close();
System.out.println("线程:"+threadId+"下载完毕");
} catch (Exception e) {
e.printStackTrace();
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.coderising.download;

import com.coderising.download.api.Connection;
import com.coderising.download.api.DownloadListener;
import com.coderising.download.impl.ConnectionImpl;
import com.coderising.download.impl.ConnectionManagerImpl;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;

public class DownloadThread extends Thread{
int startPos;
int endPos;
String url;
String savePath;
DownloadListener listener;
private static int count = 0;
private Object lock = new Object(); //对象锁

public DownloadThread(String url, String savePath, DownloadListener listener, int startPos, int endPos){
this.startPos = startPos;
this.endPos = endPos;
this.url = url;
this.savePath = savePath;
this.listener = listener;
}
public void run(){
RandomAccessFile raf = null;
//实现
try {
Connection conn = new ConnectionManagerImpl().open(url);
raf = new RandomAccessFile(savePath,"rwd");

byte[] data= conn.read(startPos,endPos);

raf.seek(startPos);
raf.write(data);
synchronized (lock){ //加对象锁
count ++;
if(count == 3){
listener.notifyFinished();
}
}

} catch (Exception e) {
throw new RuntimeException("读取错误");
}finally {
try {
if(raf != null){
raf.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.coderising.download;

import com.coderising.download.api.Connection;
import com.coderising.download.api.ConnectionException;
import com.coderising.download.api.ConnectionManager;
import com.coderising.download.api.DownloadListener;
import com.coderising.download.impl.ConnectionManagerImpl;

import java.io.RandomAccessFile;

/**
* 文件下载器
*/
public class FileDownloader {
String url; //下载路径
String savePath = "d:/1.png"; //保存路径
DownloadListener listener ; //下载监听器
ConnectionManager cm ; //连接管理

public FileDownloader(String _url) {
this.url = _url;

}

public void execute(){
// 在这里实现你的代码, 注意: 需要用多线程实现下载
// 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码
// (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定)
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。
// 具体的实现思路:
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组
// 3. 把byte数组写入到文件中
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法

// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
Connection conn = null;
RandomAccessFile raf = null;
try {
cm = new ConnectionManagerImpl();
conn = cm.open(this.url);
int length = conn.getContentLength();

raf = new RandomAccessFile(savePath,"rwd");
if(raf.length() == 0){
raf.setLength(length);
}
raf.close();

for (int i = 0; i <= 2; i++) {
int startPos = i*length/3;
int endPos = length*(i+1)/3-1;
if(i == 2) {
endPos = length-1;
}
new DownloadThread(url, savePath,listener ,startPos, endPos).start();
}

} catch (Exception e) {
e.printStackTrace();
}

}

public void setListener(DownloadListener listener) {
this.listener = listener;
}



public void setConnectionManager(ConnectionManager ucm){
this.cm = ucm;
}

public DownloadListener getListener(){
return this.listener;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.coderising.download;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.coderising.download.api.ConnectionManager;
import com.coderising.download.api.DownloadListener;
import com.coderising.download.impl.ConnectionManagerImpl;

public class FileDownloaderTest {
boolean downloadFinished = false; //是否下载完
@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testDownload() {

String url = "https://www.baidu.com/img/bd_logo1.png";

FileDownloader downloader = new FileDownloader(url);

ConnectionManager cm = new ConnectionManagerImpl();
downloader.setConnectionManager(cm); //设置连接管理器

downloader.setListener(new DownloadListener() { //设置监听器
@Override
public void notifyFinished() {
downloadFinished = true;
}

});


downloader.execute(); //执行下载

// 等待多线程下载程序执行完毕
while (!downloadFinished) {
try {
System.out.println("还没有下载完成,休眠五秒");
//休眠5秒
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Down!!!");



}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.coderising.download.api;

import java.io.IOException;

public interface Connection {
/**
* 给定开始和结束位置, 读取数据, 返回值是字节数组
* @param startPos 开始位置, 从0开始
* @param endPos 结束位置
* @return
*/
public byte[] read(int startPos, int endPos) throws IOException;
/**
* 得到数据内容的长度
* @return
*/
public int getContentLength();

/**
* 关闭连接
*/
public void close();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.download.api;

public class ConnectionException extends Exception {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.coderising.download.api;

public interface ConnectionManager {
/**
* 给定一个url , 打开一个连接
* @param url
* @return
*/
public Connection open(String url) throws ConnectionException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.coderising.download.api;

public interface DownloadListener {
public void notifyFinished();
}
Loading

0 comments on commit 91eb89f

Please sign in to comment.