Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package droidnewz;
- import java.io.*;
- import java.net.*;
- import java.lang.StringBuffer;
- public class NNTPclient {
- private Socket nntpClient;
- private Boolean requiresLogin;
- private Boolean postingAllowed;
- private String userName;
- private String passWord;
- private String hostName;
- private int port;
- private SocketAddress sockAdd;
- private int connectTimeoutMs;
- BufferedReader in = null;
- InputStreamReader isr;
- private OutputStream nntpWriter;
- private InputStream nntpReader;
- private byte[] buffer;
- private final int BUFFER_SIZE = 4 * 1024; //4KB
- NNTPclient() {
- loadConfig();
- try {
- InetAddress addr = InetAddress.getByName(hostName);
- sockAdd = new InetSocketAddress(addr, port);
- nntpClient = new Socket();
- nntpClient.connect(sockAdd, connectTimeoutMs);
- nntpReader = nntpClient.getInputStream();
- nntpWriter = nntpClient.getOutputStream();
- buffer = new byte[BUFFER_SIZE];
- isr = new InputStreamReader(nntpReader, "UTF-8");
- in = new BufferedReader(isr);
- NNTPresponse currentRes = getResponse(null);
- if ((currentRes.Code != 200) && (currentRes.Code != 201)) {
- disconnect();
- throw new UnknownHostException(currentRes.Info);
- }
- if (currentRes.Code == 200) {
- postingAllowed = true;
- } else {
- postingAllowed = false;
- }
- if (requiresLogin) {
- Authenticate(userName, passWord);
- }
- } catch (UnknownHostException e) {
- System.out.print(e.toString());
- } catch (SocketTimeoutException e) {
- System.out.print(e.toString());
- } catch (IOException e) {
- System.out.print(e.toString());
- }
- }
- protected void finalize() {
- disconnect();
- }
- Boolean isConnected() {
- return nntpClient.isConnected();
- }
- private void loadConfig() {
- //This is where XML Settings load should go
- requiresLogin = true;
- hostName = "news.astraweb.com";
- port = 119;
- userName = "yhnujm";
- passWord = "i1bxl5ao";
- connectTimeoutMs = 2000;
- }
- public void selectGroup(String group) {
- NNTPresponse res = getResponse("GROUP " + group);
- if (res.Code != 211) {
- //Throw Exception
- }
- }
- public void DownloadBody2(String articleID, File f) {
- NNTPresponse res = readLine("BODY <" + articleID + ">");
- // StringBuffer Body = new StringBuffer(512 * 1024);
- byte[] newLine = new byte[2];
- newLine[0] = 13;
- newLine[1] = 10;
- int iRx = 0;
- Boolean Done = false;
- String response;
- if (res.Code != 222) {
- //Problem handle
- }
- try {
- in.readLine(); //read off CR/LF
- FileOutputStream fw = new FileOutputStream(f);
- while (!Done) {
- response = in.readLine();
- if (response.equals(".")) {
- Done = true;
- }
- if (response.startsWith("..")) {
- }//response = response.substring(1, response.length());
- iRx += response.length();
- fw.write(response.getBytes(), 0, response.length());
- fw.write(newLine, 0, 2);
- }
- // System.out.print(new String(Buffer,0, iRx - 5));
- fw.close();
- } catch (Exception e) {
- //TODO handle exception
- System.out.print(e.toString());
- int o = 1;
- }
- }
- public void DownloadBody3(String articleID, File f) {
- NNTPresponse res = readLine("BODY <" + articleID + ">");
- // StringBuffer Body = new StringBuffer(512 * 1024);
- byte[] newLine = {13, 10};
- int iRx = 0;
- byte[] b = new byte[5];
- Boolean Done = false;
- if (res.Code != 222) {
- //Problem handle
- }
- try {
- nntpReader.read(newLine, 0, 2); //read off first crlf
- FileOutputStream fw = new FileOutputStream(f);
- while (!Done) {
- nntpReader.read(b, 0, 1);
- if (b[0] == 13) {
- nntpReader.read(b, 1, 4);
- if (b[1] == 10 && b[2] == 46 && b[3] == 13 && b[4] == 10) { //If \r\n.\r\n ... the end of the file has been reached.
- Done = true;
- fw.write(b, 0, 2);
- } else if ((b[1] == 10) && (b[2] == 46) && (b[3] == 46)) //If the beginning of the line has two dots, remove one.
- {
- fw.write(b, 0, 1);
- fw.write(b, 1, 1);
- fw.write(b, 2, 1);
- fw.write(b, 4, 1);
- // fw.write(b, 0, 5);
- } else {
- fw.write(b, 0, 5); //CR with no other special chars
- }
- } else {
- fw.write(b, 0, 1);
- }
- //
- }
- fw.flush();
- fw.close();
- } catch (Exception e) {
- //TODO handle exception
- System.out.print(e.toString());
- int o = 1;
- }
- }
- public void DownloadBody(String articleID, File f) {
- NNTPresponse res = readLine("BODY <" + articleID + ">");
- // StringBuffer Body = new StringBuffer(512 * 1024);
- byte[] newLine = {13, 10};
- int iRx = 0;
- byte[] b = new byte[5];
- Boolean Done = false;
- if (res.Code != 222) {
- //Problem handle
- }
- try {
- nntpReader.read(newLine, 0, 2); //read off first crlf
- FileOutputStream fw = new FileOutputStream(f);
- while (!Done) {
- nntpReader.read(b, 0, 1);
- fw.write(b,0,1);
- if (b[0] == 13) {
- nntpReader.read(b, 1, 1);
- fw.write(b,1,1);
- if (b[1] == 10) {
- nntpReader.read(b, 2, 1);
- if (b[2] == 46) {
- nntpReader.read(b, 3, 1);
- if(b[3] == 46)
- {
- fw.write(b,2,1);
- }
- else if(b[3] == 13)
- {
- nntpReader.read(b, 4, 1);
- if(b[4]==10)
- {
- Done = true;
- }else
- {
- fw.write(b,2,3);
- }
- }
- }
- }
- }
- fw.flush();
- }
- fw.close();
- } catch (Exception e) {
- //TODO handle exception
- System.out.print(e.toString());
- int o = 1;
- }
- }
- public void sendLine(String message) {
- // Ensures that a CR/LF is sent at the end of every line
- message += "\r\n";
- byte[] byteMsg = message.getBytes();
- try {
- nntpWriter.write(byteMsg);
- nntpWriter.flush();
- } catch (IOException e) {
- //TODO add error handling for close sockets etc..
- }
- System.out.print(new String(byteMsg, 0, byteMsg.length - 1));
- }
- public Boolean Authenticate(String Username, String Password) {
- NNTPresponse res;
- res = getResponse("AUTHINFO USER " + userName);
- if (res.Code == 381) //More Authentication Information Required
- {
- res = getResponse("AUTHINFO PASS " + passWord);
- }
- if (res.Code != 281) //Authentication rejected
- {
- //TODO Throw NTNP exception
- }
- return true;
- }
- public void disconnect() {
- try {
- getResponse("QUIT");
- nntpClient.close();
- nntpWriter = null;
- nntpReader = null;
- buffer = null;
- } catch (IOException e) {
- //TODO handle error
- }
- }
- private NNTPresponse readLine(String message) {
- if (message != null) {
- sendLine(message);
- }
- byte b = -1;
- try {
- for (int i = 0;; i++) {
- b = (byte) nntpReader.read();
- if (b == 13) { //Check for CR
- b = (byte) nntpReader.read();
- if (b == 10) //Next should be LF
- {
- return new NNTPresponse(buffer, i - 1);
- } else {
- //TODO Throw Exception
- }
- } else if (b == -1) //Read failed
- {
- //TODO ERROR handle....
- }
- buffer[i] = b;
- }
- } catch (IOException e) {
- //TODO Handle Exception
- }
- return null;
- }
- public NNTPresponse getResponse(String message) {
- if (message != null) {
- sendLine(message);
- }
- int iRx = -1;
- try {
- iRx = nntpReader.read(buffer);
- System.out.print(new String(buffer, 0, iRx));
- } catch (IOException e) {
- //TODO handle exception
- }
- return new NNTPresponse(buffer, iRx);
- }
- public class NNTPresponse {
- public int Code;
- public String Info;
- public NNTPresponse(String fullResponse) {
- Code = Integer.parseInt(fullResponse.substring(0, 3));
- Info = fullResponse;
- }
- public NNTPresponse(int code, String fullResponse) {
- Code = code;
- Info = fullResponse;
- }
- public NNTPresponse(byte[] readBytes, int numOfBytes) {
- Info = new String(readBytes, 0, numOfBytes);
- Code = Integer.parseInt(Info.substring(0, 3));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment