/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.io.*;
import java.net.*;

import java.nio.*;
import java.nio.channels.*;

public class TSReader extends Thread {
 public static final int TS_PACKET_SIZE = 0xbc;
// public static final int BUFSIZE        = 0x400 * TS_PACKET_SIZE;
 public static final int BUFSIZE        = 0x2000000;

 public static final int PORT_START     = 3200;

 public static final String TS_DIR  = "TS";
 public static final String TS_EXT  = ".mpeg";

 private static int bindport;

 String filename;
 private ServerSocket srv;
 private byte buf[];

 static {
  bindport=PORT_START;
 }

 private InputStream get_net_stream() {
  InputStream is=null;

  try {
   srv=new ServerSocket(bindport,5);
   Socket sck=srv.accept();

   sck.setSoTimeout(0);
   is=sck.getInputStream();

   bindport++;
  } catch(Throwable t) {}
  
  return is;
 }

 private OutputStream get_file_stream() {
  OutputStream os=null;

  try {
   String name=ConsoleIf.FS_ROOT+"\\"+TS_DIR+"\\"+filename+TS_EXT;

   int i=name.lastIndexOf('\\');
   String dirname=name.substring(0, i);
  
   new File(dirname).mkdirs();

   FileOutputStream fos=new FileOutputStream(name);

   os=fos;
  } catch(Throwable t) {}
  
  return os;
 }

 public void run() {
  InputStream is=get_net_stream();
  OutputStream os=get_file_stream();

  try {
   while(true) {
    int size=is.read(buf);

    if (size<0) break;

    os.write(buf,0,size);
   }
  } catch(Throwable t) {}

  try {
   os.close();
   is.close();
  } catch(Throwable t) {}
 }

 public String get_host() {
  while(srv==null) {
   ConsoleUtils.sleep(100);
  }

  String myaddr=null;

  try {
   myaddr=InetAddress.getLocalHost().getHostAddress();
  } catch(Throwable t) {}

  return myaddr;
 }

 public int get_port() {
  while(srv==null) {
   ConsoleUtils.sleep(100);
  }

  return srv.getLocalPort();
 }

 public TSReader(String name) {
  this.filename=name;
  buf=new byte[BUFSIZE];
 }

 public static TSReader start(String name) {
  TSReader reader=null;

  try {
   reader=new TSReader(name);
   reader.setPriority(10);

   reader.start();
  } catch(Throwable t) {}

  return reader;
 }
}
