sdblib

lib.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.channels.SocketChannel;
import org.tizen.sdblib.IDevice;
import org.tizen.sdblib.SdbHelper;
import org.tizen.sdblib.SdbResponse;
import org.tizen.sdblib.SmartDevelopmentBridge;
import org.tizen.sdblib.SmartDevelopmentBridgetConstants;
import org.tizen.sdblib.exception.SyncException;
import org.tizen.sdblib.exception.TimeoutException;
import org.tizen.sdblib.util.Assert;
import org.tizen.sdblib.util.ByteUtil;
import org.tizen.sdblib.util.IOUtil;
import org.tizen.sdblib.util.Log;

public class FileEntryInputStream
  extends InputStream
{
  protected final FileEntry entry;
  protected final int timeOut;
  protected byte[] header = new byte[8];
  protected final byte[] data = new byte[65544];
  protected int index = 0;
  protected int length = 0;
  protected final SocketChannel channel;
  
  protected static byte[] createFileReq(byte[] command, byte[] path)
  {
    byte[] array = new byte[8 + path.length];
    
    System.arraycopy(command, 0, array, 0, 4);
    ByteUtil.toBytes(path.length, array, 4);
    System.arraycopy(path, 0, array, 8, path.length);
    
    return array;
  }
  
  public FileEntryInputStream(FileEntry entry, int timeOut)
    throws IOException
  {
    Assert.notNull(entry);
    this.entry = entry;
    this.timeOut = timeOut;
    channel = SmartDevelopmentBridge.getBridge().openChannel();
    channel.configureBlocking(true);
    
    SdbHelper.initializeDevice(channel, this.entry.getDevice().getSerialNumber());
    
    SdbHelper.write(channel, SdbHelper.formSdbRequest("sync:"));
    
    SdbResponse res = SdbHelper.readSdbResponse(channel);
    if (!okay)
    {
      Log.w("sdb", "Got unhappy response from sdb sync req: " + message);
      channel.close();
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_ERROR);
    }
    byte[] remotePath = SdbHelper.string2bytes(this.entry.getFullPath());
    try
    {
      if (1024 < remotePath.length) {
        throw new SyncException(SyncServiceReturnCode.RESULT_REMOTE_PATH_LENGTH);
      }
      SdbHelper.write(channel, createFileReq(SmartDevelopmentBridgetConstants.ID_RECV, remotePath), -1, timeOut);
      
      SdbHelper.read(channel, header, -1, timeOut);
    }
    catch (UnsupportedEncodingException e)
    {
      throw new SyncException(SyncServiceReturnCode.RESULT_REMOTE_PATH_ENCODING, e);
    }
    catch (TimeoutException e)
    {
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_TIMEOUT, e);
    }
    catch (IOException e)
    {
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
    }
  }
  
  public int read()
    throws IOException
  {
    if ((!SdbHelper.check(header, 0, SmartDevelopmentBridgetConstants.ID_DATA, 0, 4)) && (!SdbHelper.check(header, 0, SmartDevelopmentBridgetConstants.ID_DONE, 0, 4))) {
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_ERROR);
    }
    if (length <= index)
    {
      if (SdbHelper.check(header, 0, SmartDevelopmentBridgetConstants.ID_DONE, 0, 4)) {
        return -1;
      }
      length = ByteUtil.fromBytes(header, 4);
      if (length > 65536) {
        throw new SyncException(SyncServiceReturnCode.RESULT_BUFFER_OVERRUN);
      }
      try
      {
        SdbHelper.read(channel, data, length, timeOut);
        index = 0;
        
        SdbHelper.read(channel, header, -1, timeOut);
      }
      catch (TimeoutException e)
      {
        throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_TIMEOUT, e);
      }
      catch (IOException e)
      {
        throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
      }
    }
    return 0xFF & data[(index++)];
  }
  
  private static byte[] createReq(byte[] command, int value)
  {
    byte[] array = new byte[8];
    
    System.arraycopy(command, 0, array, 0, 4);
    ByteUtil.toBytes(value, array, 4);
    
    return array;
  }
  
  public void close()
    throws IOException
  {
    SdbHelper.write(channel, createReq(SmartDevelopmentBridgetConstants.ID_QUIT, 0));
    IOUtil.tryClose(channel);
    super.close();
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.FileEntryInputStream
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.service;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.channels.SocketChannel;
import org.tizen.sdblib.IDevice;
import org.tizen.sdblib.SdbHelper;
import org.tizen.sdblib.SdbResponse;
import org.tizen.sdblib.SmartDevelopmentBridge;
import org.tizen.sdblib.SmartDevelopmentBridgetConstants;
import org.tizen.sdblib.exception.SyncException;
import org.tizen.sdblib.exception.TimeoutException;
import org.tizen.sdblib.util.Assert;
import org.tizen.sdblib.util.ByteUtil;
import org.tizen.sdblib.util.Log;

public class FileEntryOutputStream
  extends OutputStream
{
  protected final FileEntry entry;
  protected final int timeOut;
  protected final byte[] buffer = new byte[65544];
  protected int index = 8;
  protected final SocketChannel channel;
  
  public FileEntryOutputStream(FileEntry entry, int timeOut)
    throws IOException
  {
    Assert.notNull(entry);
    this.entry = entry;
    this.timeOut = timeOut;
    channel = SmartDevelopmentBridge.getBridge().openChannel();
    channel.configureBlocking(true);
    
    SdbHelper.initializeDevice(channel, this.entry.getDevice().getSerialNumber());
    
    byte[] request = SdbHelper.formSdbRequest("sync:");
    
    SdbHelper.write(channel, request);
    
    SdbResponse resp = SdbHelper.readSdbResponse(channel);
    if (!okay)
    {
      Log.w("sdb", "Got unhappy response from sdb sync req: " + message);
      channel.close();
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_ERROR);
    }
    byte[] remotePath = SdbHelper.string2bytes(this.entry.getFullPath());
    try
    {
      if (1024 < remotePath.length) {
        throw new SyncException(SyncServiceReturnCode.RESULT_REMOTE_PATH_LENGTH);
      }
      byte[] msg = createSendFileReq(SmartDevelopmentBridgetConstants.ID_SEND, remotePath, 493);
      
      SdbHelper.write(channel, msg, -1, timeOut);
    }
    catch (UnsupportedEncodingException e)
    {
      throw new SyncException(SyncServiceReturnCode.RESULT_REMOTE_PATH_ENCODING, e);
    }
    catch (TimeoutException e)
    {
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_TIMEOUT, e);
    }
    catch (IOException e)
    {
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
    }
    System.arraycopy(SmartDevelopmentBridgetConstants.ID_DATA, 0, buffer, 0, SmartDevelopmentBridgetConstants.ID_DATA.length);
  }
  
  public void write(int b)
    throws IOException
  {
    if (buffer.length <= index) {
      flush();
    }
    buffer[(index++)] = ((byte)b);
  }
  
  public void flush()
    throws IOException
  {
    Log.d("sync", "flush " + System.currentTimeMillis());
    if (index <= 8) {
      return;
    }
    ByteUtil.toBytes(index - 8, buffer, 4);
    try
    {
      SdbHelper.write(channel, buffer, index, timeOut);
    }
    catch (TimeoutException e)
    {
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_TIMEOUT, e);
    }
    catch (IOException e)
    {
      throw new SyncException(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
    }
    index = 8;
    Log.d("sync", "flushd " + System.currentTimeMillis());
    
    super.flush();
  }
  
  private static byte[] createSendFileReq(byte[] command, byte[] path, int mode)
  {
    String modeStr = "," + (mode & 0x1FF);
    try
    {
      byte[] modeContent = modeStr.getBytes("ISO-8859-1");
      byte[] array = new byte[8 + path.length + modeContent.length];
      
      System.arraycopy(command, 0, array, 0, 4);
      ByteUtil.toBytes(path.length + modeContent.length, array, 4);
      System.arraycopy(path, 0, array, 8, path.length);
      System.arraycopy(modeContent, 0, array, 8 + path.length, modeContent.length);
      
      return array;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException) {}
    return null;
  }
  
  private static byte[] createReq(byte[] command, int value)
  {
    byte[] array = new byte[8];
    
    System.arraycopy(command, 0, array, 0, 4);
    ByteUtil.toBytes(value, array, 4);
    
    return array;
  }
  
  public void close()
    throws IOException
  {
    flush();
    
    long time = System.currentTimeMillis() / 1000L;
    
    SdbHelper.write(channel, createReq(SmartDevelopmentBridgetConstants.ID_DONE, (int)time), -1, timeOut);
    
    byte[] result = new byte[8];
    SdbHelper.read(channel, result, -1, timeOut);
    if (!SdbHelper.check(result, 0, SmartDevelopmentBridgetConstants.ID_OKAY, 0, 4))
    {
      if (SdbHelper.check(result, 0, SmartDevelopmentBridgetConstants.ID_FAIL, 0, 4))
      {
        int len = ByteUtil.fromBytes(result, 4);
        
        SdbHelper.read(channel, buffer, len, timeOut);
        
        String message = new String(buffer, 0, len);
        Log.e("sdb", "transfer error: " + message);
        throw new SyncException(SyncServiceReturnCode.RESULT_UNKNOWN_ERROR, message);
      }
      throw new SyncException(SyncServiceReturnCode.RESULT_UNKNOWN_ERROR);
    }
    SdbHelper.write(channel, createReq(SmartDevelopmentBridgetConstants.ID_QUIT, 0));
    super.close();
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.FileEntryOutputStream
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.service;

import java.util.Map;
import org.tizen.sdblib.util.MapUtil;
import org.tizen.sdblib.util.ObjectUtil;

public enum FileEntryType
{
  RootDevice(
  
    "Device"),  RootEmulator(
  
    "Emulator"),  File(
  
    "File"),  Directory(
  
    "Directory"),  Link(
  
    "Type Link"),  DirectoryLink(
  
    "Directory Link"),  BlockDevice(
  
    "Block"),  CharacterDevice(
  
    "Character"),  Socket(
  
    "Socket"),  FIFO(
  
    "Type Fifo"),  Unknown(
  
    "Unknown");
  
  private String name;
  
  private FileEntryType(String name)
  {
    this.name = name;
  }
  
  private static final Map<Character, FileEntryType> SYMBOL2TYPE = MapUtil.asMap(new Object[][] {
    { Character.valueOf('-'), File }, 
    { Character.valueOf('d'), Directory }, 
    { Character.valueOf('l'), Link }, 
    { Character.valueOf('b'), BlockDevice }, 
    { Character.valueOf('c'), CharacterDevice }, 
    { Character.valueOf('s'), Socket }, 
    { Character.valueOf('p'), FIFO } });
  
  public static FileEntryType from(char flag)
  {
    return (FileEntryType)ObjectUtil.nvl(new FileEntryType[] { (FileEntryType)SYMBOL2TYPE.get(Character.valueOf(flag)), Unknown });
  }
  
  public boolean isRoot()
  {
    return (equals(RootDevice)) || (equals(RootEmulator));
  }
  
  public boolean isDirectory()
  {
    return (equals(Directory)) || (equals(DirectoryLink));
  }
  
  public boolean isLink()
  {
    return (equals(Link)) || (equals(DirectoryLink));
  }
  
  public boolean canHaveChild()
  {
    return (isDirectory()) || (isRoot());
  }
  
  public String toString()
  {
    return name;
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.FileEntryType
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.service;

public abstract interface ISyncProgressMonitor
{
  public abstract void start(long paramLong);
  
  public abstract void stop();
  
  public abstract boolean isCanceled();
  
  public abstract void startSubTask(String paramString);
  
  public abstract void advance(long paramLong);
  
  public abstract long getTotal();
  
  public abstract long getWorked();
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.ISyncProgressMonitor
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.service;

public class NullSyncProgressMonitor
  implements ISyncProgressMonitor
{
  protected static final NullSyncProgressMonitor instance = new NullSyncProgressMonitor();
  
  public static NullSyncProgressMonitor getInstance()
  {
    return instance;
  }
  
  public void advance(long work) {}
  
  public boolean isCanceled()
  {
    return false;
  }
  
  public void start(long totalWork) {}
  
  public void startSubTask(String name) {}
  
  public void stop() {}
  
  public long getTotal()
  {
    return 0L;
  }
  
  public long getWorked()
  {
    return 0L;
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.NullSyncProgressMonitor
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.service;

import org.tizen.sdblib.util.Assert;

public class SyncResult
{
  protected final int code;
  protected final String message;
  
  protected static String errorCodeToString(int code)
  {
    String message = SyncServiceReturnCode.getMessage(code);
    Assert.notNull(message);
    return message;
  }
  
  public SyncResult(int code)
  {
    this(code, errorCodeToString(code));
  }
  
  public SyncResult(int code, String message)
  {
    this.code = code;
    this.message = message;
  }
  
  SyncResult(int code, Exception e)
  {
    this.code = code;
    String errorMessage = errorCodeToString(code);
    String exceptionMessage = e.getMessage();
    if (exceptionMessage != null) {
      message = (errorMessage + "\n" + exceptionMessage);
    } else {
      message = errorMessage;
    }
  }
  
  public SyncResult()
  {
    this(SyncServiceReturnCode.RESULT_OK);
  }
  
  public SyncResult(SyncServiceReturnCode code)
  {
    this(code.value());
  }
  
  public SyncResult(SyncServiceReturnCode code, String message)
  {
    this(code.value(), message);
  }
  
  SyncResult(SyncServiceReturnCode code, Exception e)
  {
    this(code.value(), e);
  }
  
  public int getCode()
  {
    return code;
  }
  
  public String getMessage()
  {
    return message;
  }
  
  public boolean isOk()
  {
    return SyncServiceReturnCode.RESULT_OK.value() == getCode();
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.SyncResult
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.service;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import org.tizen.sdblib.Device;
import org.tizen.sdblib.SmartDevelopmentBridge;
import org.tizen.sdblib.exception.SyncException;
import org.tizen.sdblib.util.Assert;
import org.tizen.sdblib.util.FilenameUtil;
import org.tizen.sdblib.util.IOUtil;
import org.tizen.sdblib.util.Log;
import org.tizen.sdblib.util.ObjectUtil;
import org.tizen.sdblib.util.Preferences;

public class SyncService
  implements Closeable
{
  protected final Device device;
  
  public SyncService(SmartDevelopmentBridge sdb, Device device)
  {
    Assert.notNull(device);
    this.device = device;
  }
  
  public void close() {}
  
  public SyncResult pull(FileEntry entry, String localPath)
  {
    return pull(new FileEntry[] { entry }, localPath);
  }
  
  public SyncResult pull(FileEntry[] entries, String localPath)
  {
    return pull(entries, localPath, null);
  }
  
  public SyncResult pull(FileEntry[] entries, String localPath, ISyncProgressMonitor monitor)
  {
    return pull(entries, localPath, monitor, Preferences.getTimeOut());
  }
  
  /* Error */
  public SyncResult pull(FileEntry[] entries, String destination, ISyncProgressMonitor monitor, int timeOut)
  {
    // Byte code:
    //   0: iconst_2
    //   1: anewarray 57	org/tizen/sdblib/service/ISyncProgressMonitor
    //   4: dup
    //   5: iconst_0
    //   6: aload_3
    //   7: aastore
    //   8: dup
    //   9: iconst_1
    //   10: invokestatic 59	org/tizen/sdblib/service/NullSyncProgressMonitor:getInstance	()Lorg/tizen/sdblib/service/NullSyncProgressMonitor;
    //   13: aastore
    //   14: invokestatic 65	org/tizen/sdblib/util/ObjectUtil:nvl	([Ljava/lang/Object;)Ljava/lang/Object;
    //   17: checkcast 57	org/tizen/sdblib/service/ISyncProgressMonitor
    //   20: astore_3
    //   21: new 71	java/io/File
    //   24: dup
    //   25: aload_2
    //   26: invokespecial 73	java/io/File:<init>	(Ljava/lang/String;)V
    //   29: astore 5
    //   31: aload 5
    //   33: invokevirtual 76	java/io/File:exists	()Z
    //   36: ifne +14 -> 50
    //   39: new 80	org/tizen/sdblib/service/SyncResult
    //   42: dup
    //   43: getstatic 82	org/tizen/sdblib/service/SyncServiceReturnCode:RESULT_NO_DIR_TARGET	Lorg/tizen/sdblib/service/SyncServiceReturnCode;
    //   46: invokespecial 88	org/tizen/sdblib/service/SyncResult:<init>	(Lorg/tizen/sdblib/service/SyncServiceReturnCode;)V
    //   49: areturn
    //   50: aload 5
    //   52: invokevirtual 91	java/io/File:isDirectory	()Z
    //   55: ifne +14 -> 69
    //   58: new 80	org/tizen/sdblib/service/SyncResult
    //   61: dup
    //   62: getstatic 94	org/tizen/sdblib/service/SyncServiceReturnCode:RESULT_TARGET_IS_FILE	Lorg/tizen/sdblib/service/SyncServiceReturnCode;
    //   65: invokespecial 88	org/tizen/sdblib/service/SyncResult:<init>	(Lorg/tizen/sdblib/service/SyncServiceReturnCode;)V
    //   68: areturn
    //   69: aload_3
    //   70: ldc2_w 97
    //   73: invokeinterface 99 3 0
    //   78: aload_3
    //   79: ldc 103
    //   81: invokeinterface 105 2 0
    //   86: aload_1
    //   87: aload_3
    //   88: invokestatic 108	org/tizen/sdblib/util/FileEntryUtil:getTotalSize	([Lorg/tizen/sdblib/service/FileEntry;Lorg/tizen/sdblib/service/ISyncProgressMonitor;)J
    //   91: lstore 7
    //   93: aload_3
    //   94: ldc 114
    //   96: invokeinterface 105 2 0
    //   101: aload_3
    //   102: invokeinterface 116 1 0
    //   107: aload_3
    //   108: lload 7
    //   110: invokeinterface 99 3 0
    //   115: aload_0
    //   116: aload_1
    //   117: aload_2
    //   118: aload_3
    //   119: iload 4
    //   121: invokevirtual 119	org/tizen/sdblib/service/SyncService:doPull	([Lorg/tizen/sdblib/service/FileEntry;Ljava/lang/String;Lorg/tizen/sdblib/service/ISyncProgressMonitor;I)Lorg/tizen/sdblib/service/SyncResult;
    //   124: astore 6
    //   126: goto +36 -> 162
    //   129: pop
    //   130: new 80	org/tizen/sdblib/service/SyncResult
    //   133: dup
    //   134: getstatic 122	org/tizen/sdblib/service/SyncServiceReturnCode:RESULT_CANCELED	Lorg/tizen/sdblib/service/SyncServiceReturnCode;
    //   137: invokespecial 88	org/tizen/sdblib/service/SyncResult:<init>	(Lorg/tizen/sdblib/service/SyncServiceReturnCode;)V
    //   140: astore 10
    //   142: aload_3
    //   143: invokeinterface 116 1 0
    //   148: aload 10
    //   150: areturn
    //   151: astore 9
    //   153: aload_3
    //   154: invokeinterface 116 1 0
    //   159: aload 9
    //   161: athrow
    //   162: aload_3
    //   163: invokeinterface 116 1 0
    //   168: aload 6
    //   170: areturn
    // Line number table:
    //   Java source line #193	-> byte code offset #0
    //   Java source line #195	-> byte code offset #21
    //   Java source line #196	-> byte code offset #31
    //   Java source line #198	-> byte code offset #39
    //   Java source line #200	-> byte code offset #50
    //   Java source line #202	-> byte code offset #58
    //   Java source line #205	-> byte code offset #69
    //   Java source line #206	-> byte code offset #78
    //   Java source line #212	-> byte code offset #86
    //   Java source line #213	-> byte code offset #93
    //   Java source line #214	-> byte code offset #101
    //   Java source line #217	-> byte code offset #107
    //   Java source line #218	-> byte code offset #115
    //   Java source line #220	-> byte code offset #129
    //   Java source line #222	-> byte code offset #130
    //   Java source line #226	-> byte code offset #142
    //   Java source line #222	-> byte code offset #148
    //   Java source line #225	-> byte code offset #151
    //   Java source line #226	-> byte code offset #153
    //   Java source line #227	-> byte code offset #159
    //   Java source line #226	-> byte code offset #162
    //   Java source line #229	-> byte code offset #168
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	171	0	this	SyncService
    //   0	171	1	entries	FileEntry[]
    //   0	171	2	destination	String
    //   0	171	3	monitor	ISyncProgressMonitor
    //   0	171	4	timeOut	int
    //   29	22	5	f	File
    //   124	3	6	result	SyncResult
    //   162	7	6	result	SyncResult
    //   91	18	7	total	long
    //   151	9	9	localObject	Object
    //   140	9	10	localSyncResult1	SyncResult
    //   129	1	11	localInterruptedException	InterruptedException
    // Exception table:
    //   from	to	target	type
    //   86	126	129	java/lang/InterruptedException
    //   86	142	151	finally
  }
  
  public SyncResult doPull(FileEntry[] entries, String destination, ISyncProgressMonitor monitor, int timeOut)
  {
    monitor = (ISyncProgressMonitor)ObjectUtil.nvl(new ISyncProgressMonitor[] { monitor, NullSyncProgressMonitor.getInstance() });
    try
    {
      FileEntry[] arrayOfFileEntry1;
      int j = (arrayOfFileEntry1 = entries).length;
      for (int i = 0; i < j; i++)
      {
        FileEntry entry = arrayOfFileEntry1[i];
        
        String path = FilenameUtil.addTailingPath(destination, entry.getName());
        if (entry.isDirectory())
        {
          new File(path).mkdir();
          
          FileEntry[] children = entry.getChildren();
          doPull(children, path, monitor, timeOut);
        }
        else
        {
          monitor.startSubTask(String.format("%s\t%s", new Object[] { entry.getFullPath(), path }));
          
          FileOutputStream fileOut = null;
          try
          {
            fileOut = new FileOutputStream(path);
            SyncResult result = doPull(entry, fileOut, monitor, timeOut);
            if (!result.isOk()) {
              return result;
            }
          }
          finally
          {
            IOUtil.tryClose(fileOut);
          }
          IOUtil.tryClose(fileOut);
        }
      }
      return new SyncResult();
    }
    catch (IOException e)
    {
      return new SyncResult(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
    }
  }
  
  public SyncResult doPull(FileEntry remote, OutputStream out, ISyncProgressMonitor monitor, int timeOut)
  {
    monitor = (ISyncProgressMonitor)ObjectUtil.nvl(new ISyncProgressMonitor[] { monitor, NullSyncProgressMonitor.getInstance() });
    
    FileEntryInputStream in = null;
    try
    {
      in = new FileEntryInputStream(remote, timeOut);
      
      byte[] data = new byte['?'];
      int nBytes = 0;
      while ((nBytes = in.read(data)) > 0)
      {
        if (monitor.isCanceled()) {
          return new SyncResult(SyncServiceReturnCode.RESULT_CANCELED);
        }
        out.write(data, 0, nBytes);
        monitor.advance(nBytes);
      }
      return new SyncResult();
    }
    catch (SyncException e)
    {
      return new SyncResult(e.getCode());
    }
    catch (FileNotFoundException e)
    {
      return new SyncResult(SyncServiceReturnCode.RESULT_FILE_WRITE_ERROR, e);
    }
    catch (IOException e)
    {
      SyncResult localSyncResult;
      return new SyncResult(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
    }
    finally
    {
      IOUtil.tryClose(out);
      try
      {
        if (in != null) {
          in.close();
        }
      }
      catch (SyncException e)
      {
        return new SyncResult(e.getCode());
      }
      catch (IOException e)
      {
        return new SyncResult(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
      }
    }
  }
  
  public SyncResult push(String local, FileEntry entry)
  {
    return push(new String[] { local }, entry);
  }
  
  public SyncResult push(String[] locals, FileEntry entry)
  {
    return push(locals, entry, null);
  }
  
  public SyncResult push(String[] locals, FileEntry entry, ISyncProgressMonitor monitor)
  {
    ArrayList<File> files = new ArrayList();
    String[] arrayOfString;
    int j = (arrayOfString = locals).length;
    for (int i = 0; i < j; i++)
    {
      String path = arrayOfString[i];
      files.add(new File(path));
    }
    return push((File[])files.toArray(new File[files.size()]), entry, monitor, Preferences.getTimeOut());
  }
  
  /* Error */
  public SyncResult push(File[] locals, FileEntry remote, ISyncProgressMonitor monitor, int timeOut)
  {
    // Byte code:
    //   0: iconst_2
    //   1: anewarray 57	org/tizen/sdblib/service/ISyncProgressMonitor
    //   4: dup
    //   5: iconst_0
    //   6: aload_3
    //   7: aastore
    //   8: dup
    //   9: iconst_1
    //   10: invokestatic 59	org/tizen/sdblib/service/NullSyncProgressMonitor:getInstance	()Lorg/tizen/sdblib/service/NullSyncProgressMonitor;
    //   13: aastore
    //   14: invokestatic 65	org/tizen/sdblib/util/ObjectUtil:nvl	([Ljava/lang/Object;)Ljava/lang/Object;
    //   17: checkcast 57	org/tizen/sdblib/service/ISyncProgressMonitor
    //   20: astore_3
    //   21: aload_2
    //   22: invokevirtual 149	org/tizen/sdblib/service/FileEntry:isDirectory	()Z
    //   25: ifne +24 -> 49
    //   28: getstatic 281	org/tizen/sdblib/service/FileEntryType:Unknown	Lorg/tizen/sdblib/service/FileEntryType;
    //   31: aload_2
    //   32: invokevirtual 287	org/tizen/sdblib/service/FileEntry:getType	()Lorg/tizen/sdblib/service/FileEntryType;
    //   35: if_acmpeq +14 -> 49
    //   38: new 80	org/tizen/sdblib/service/SyncResult
    //   41: dup
    //   42: getstatic 291	org/tizen/sdblib/service/SyncServiceReturnCode:RESULT_REMOTE_IS_FILE	Lorg/tizen/sdblib/service/SyncServiceReturnCode;
    //   45: invokespecial 88	org/tizen/sdblib/service/SyncResult:<init>	(Lorg/tizen/sdblib/service/SyncServiceReturnCode;)V
    //   48: areturn
    //   49: aload_3
    //   50: ldc2_w 97
    //   53: invokeinterface 99 3 0
    //   58: aload_3
    //   59: ldc 103
    //   61: invokeinterface 105 2 0
    //   66: aload_1
    //   67: aload_3
    //   68: invokestatic 294	org/tizen/sdblib/service/SyncService:getTotalLocalFileSizeLong	([Ljava/io/File;Lorg/tizen/sdblib/service/ISyncProgressMonitor;)J
    //   71: lstore 6
    //   73: aload_3
    //   74: ldc_w 298
    //   77: invokeinterface 105 2 0
    //   82: aload_3
    //   83: invokeinterface 116 1 0
    //   88: aload_3
    //   89: lload 6
    //   91: invokeinterface 99 3 0
    //   96: aload_0
    //   97: aload_1
    //   98: aload_2
    //   99: aload_3
    //   100: iload 4
    //   102: invokevirtual 300	org/tizen/sdblib/service/SyncService:doPush	([Ljava/io/File;Lorg/tizen/sdblib/service/FileEntry;Lorg/tizen/sdblib/service/ISyncProgressMonitor;I)Lorg/tizen/sdblib/service/SyncResult;
    //   105: astore 5
    //   107: goto +36 -> 143
    //   110: pop
    //   111: new 80	org/tizen/sdblib/service/SyncResult
    //   114: dup
    //   115: getstatic 122	org/tizen/sdblib/service/SyncServiceReturnCode:RESULT_CANCELED	Lorg/tizen/sdblib/service/SyncServiceReturnCode;
    //   118: invokespecial 88	org/tizen/sdblib/service/SyncResult:<init>	(Lorg/tizen/sdblib/service/SyncServiceReturnCode;)V
    //   121: astore 9
    //   123: aload_3
    //   124: invokeinterface 116 1 0
    //   129: aload 9
    //   131: areturn
    //   132: astore 8
    //   134: aload_3
    //   135: invokeinterface 116 1 0
    //   140: aload 8
    //   142: athrow
    //   143: aload_3
    //   144: invokeinterface 116 1 0
    //   149: aload 5
    //   151: areturn
    // Line number table:
    //   Java source line #459	-> byte code offset #0
    //   Java source line #460	-> byte code offset #21
    //   Java source line #462	-> byte code offset #38
    //   Java source line #465	-> byte code offset #49
    //   Java source line #467	-> byte code offset #58
    //   Java source line #473	-> byte code offset #66
    //   Java source line #474	-> byte code offset #73
    //   Java source line #475	-> byte code offset #82
    //   Java source line #478	-> byte code offset #88
    //   Java source line #480	-> byte code offset #96
    //   Java source line #482	-> byte code offset #110
    //   Java source line #484	-> byte code offset #111
    //   Java source line #488	-> byte code offset #123
    //   Java source line #484	-> byte code offset #129
    //   Java source line #487	-> byte code offset #132
    //   Java source line #488	-> byte code offset #134
    //   Java source line #489	-> byte code offset #140
    //   Java source line #488	-> byte code offset #143
    //   Java source line #491	-> byte code offset #149
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	152	0	this	SyncService
    //   0	152	1	locals	File[]
    //   0	152	2	remote	FileEntry
    //   0	152	3	monitor	ISyncProgressMonitor
    //   0	152	4	timeOut	int
    //   105	3	5	result	SyncResult
    //   143	7	5	result	SyncResult
    //   71	19	6	total	long
    //   132	9	8	localObject	Object
    //   121	9	9	localSyncResult1	SyncResult
    //   110	1	10	localInterruptedException	InterruptedException
    // Exception table:
    //   from	to	target	type
    //   66	107	110	java/lang/InterruptedException
    //   66	123	132	finally
  }
  
  public SyncResult doPush(File[] locals, FileEntry remote, ISyncProgressMonitor monitor, int timeOut)
  {
    monitor = (ISyncProgressMonitor)ObjectUtil.nvl(new ISyncProgressMonitor[] { monitor, NullSyncProgressMonitor.getInstance() });
    try
    {
      String remotePath = remote.getFullPath();
      File[] arrayOfFile;
      int j = (arrayOfFile = locals).length;
      for (int i = 0; i < j; i++)
      {
        File f = arrayOfFile[i];
        if (monitor.isCanceled()) {
          return new SyncResult(SyncServiceReturnCode.RESULT_CANCELED);
        }
        if (f.exists())
        {
          String path = FilenameUtil.addTailingPath(remotePath, f.getName());
          FileEntry remoteFile;
          FileEntry remoteFile;
          if (f.getName().contains("'")) {
            remoteFile = device.getSimpleFileEntry(path);
          } else {
            remoteFile = device.getFileEntry(path);
          }
          monitor.startSubTask(String.format("%s\t%s", new Object[] { f.getAbsolutePath(), path }));
          if (f.isDirectory())
          {
            SyncResult result = doPush(f.listFiles(), remoteFile, monitor, timeOut);
            if (!result.isOk()) {
              return result;
            }
          }
          else if (f.isFile())
          {
            FileInputStream fileIn = new FileInputStream(f);
            try
            {
              SyncResult result = doPush(fileIn, remoteFile, monitor, timeOut);
              if (!result.isOk()) {
                return result;
              }
            }
            finally
            {
              IOUtil.tryClose(fileIn);
            }
            IOUtil.tryClose(fileIn);
          }
        }
      }
      return new SyncResult();
    }
    catch (IOException e)
    {
      return new SyncResult(SyncServiceReturnCode.RESULT_FILE_READ_ERROR, e);
    }
  }
  
  protected static long getTotalLocalFileSizeLong(File[] files, ISyncProgressMonitor monitor)
    throws InterruptedException
  {
    long count = 0L;
    
    File[] arrayOfFile = files;int j = files.length;
    for (int i = 0; i < j; i++)
    {
      File f = arrayOfFile[i];
      if (monitor.isCanceled()) {
        throw new InterruptedException("The long running operation was cancelled");
      }
      if (f.exists()) {
        if (f.isDirectory()) {
          count += getTotalLocalFileSizeLong(f.listFiles(), monitor);
        } else if (f.isFile()) {
          count += f.length();
        }
      }
    }
    return count;
  }
  
  public SyncResult doPush(InputStream in, FileEntry remote, ISyncProgressMonitor monitor, int timeOut)
  {
    monitor = (ISyncProgressMonitor)ObjectUtil.nvl(new ISyncProgressMonitor[] { monitor, NullSyncProgressMonitor.getInstance() });
    
    long startTime = System.currentTimeMillis();
    Log.d("sync", "start at " + startTime + " ms");
    FileEntryOutputStream out = null;
    int nBytes = 0;
    byte[] buffer = new byte['?'];
    
    long size = 0L;
    try
    {
      out = new FileEntryOutputStream(remote, timeOut);
      while ((nBytes = in.read(buffer)) > 0)
      {
        if (monitor.isCanceled()) {
          return new SyncResult(SyncServiceReturnCode.RESULT_CANCELED);
        }
        out.write(buffer, 0, nBytes);
        size += nBytes;
        monitor.advance(nBytes);
      }
      return new SyncResult();
    }
    catch (SyncException e)
    {
      return new SyncResult(e.getCode(), e);
    }
    catch (IOException e)
    {
      SyncResult localSyncResult;
      return new SyncResult(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
    }
    finally
    {
      IOUtil.tryClose(in);
      try
      {
        out.close();
      }
      catch (SyncException e)
      {
        return new SyncResult(e.getCode());
      }
      catch (IOException e)
      {
        return new SyncResult(SyncServiceReturnCode.RESULT_CONNECTION_ERROR, e);
      }
      Log.d("sync", "Push " + size + " bytes in " + (System.currentTimeMillis() - startTime) + " ms");
    }
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.SyncService
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.service;

public class SyncServiceConstants
{
  static final String MESSAGE_SIZECHECKING = "File size checking: it can take some time.";
  static final String MESSAGE_PULL_FILES = "Pulling file(s) from the device";
  static final String MESSAGE_PUSH_FILES = "Pushing file(s) to the device";
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.SyncServiceConstants
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.service;

import java.util.Map;
import org.tizen.sdblib.util.MapUtil;

public enum SyncServiceReturnCode
{
  RESULT_OK(0),  RESULT_CANCELED(1),  RESULT_UNKNOWN_ERROR(2),  RESULT_CONNECTION_ERROR(3),  RESULT_NO_REMOTE_OBJECT(4),  RESULT_TARGET_IS_FILE(5),  RESULT_NO_DIR_TARGET(6),  RESULT_REMOTE_PATH_ENCODING(7),  RESULT_REMOTE_PATH_LENGTH(8),  RESULT_FILE_WRITE_ERROR(9),  RESULT_FILE_READ_ERROR(10),  RESULT_NO_LOCAL_FILE(11),  RESULT_LOCAL_IS_DIRECTORY(12),  RESULT_REMOTE_IS_FILE(13),  RESULT_BUFFER_OVERRUN(14),  RESULT_CONNECTION_TIMEOUT(15),  RESULT_FILE_EXIST(16),  RESULT_FAIL_DELETE_FILE(17),  RESULT_FAIL_CREATE_FILE(18);
  
  private int value;
  
  private SyncServiceReturnCode(int value)
  {
    this.value = value;
  }
  
  public int value()
  {
    return value;
  }
  
  protected static final Map<Integer, String> CODE2MESSAGE = MapUtil.asMap(new Object[][] {
    { Integer.valueOf(RESULT_OK.value()), "Success." }, 
    { Integer.valueOf(RESULT_CANCELED.value()), "Transfer canceled by the user." }, 
    { Integer.valueOf(RESULT_UNKNOWN_ERROR.value()), "Unknown Error." }, 
    { Integer.valueOf(RESULT_CONNECTION_ERROR.value()), "sdb Connection Error." }, 
    { Integer.valueOf(RESULT_NO_REMOTE_OBJECT.value()), "Remote object doesn't exist!" }, 
    { Integer.valueOf(RESULT_TARGET_IS_FILE.value()), "Target object is a file." }, 
    { Integer.valueOf(RESULT_NO_DIR_TARGET.value()), "Target directory doesn't exist." }, 
    { Integer.valueOf(RESULT_REMOTE_PATH_ENCODING.value()), "Remote Path encoding is not supported." }, 
    { Integer.valueOf(RESULT_REMOTE_PATH_LENGTH.value()), "Remove path is too long." }, 
    { Integer.valueOf(RESULT_FILE_WRITE_ERROR.value()), "Writing local file failed!" }, 
    { Integer.valueOf(RESULT_FILE_READ_ERROR.value()), "Reading local file failed!" }, 
    { Integer.valueOf(RESULT_NO_LOCAL_FILE.value()), "Local file doesn't exist." }, 
    { Integer.valueOf(RESULT_LOCAL_IS_DIRECTORY.value()), "Local path is a directory." }, 
    { Integer.valueOf(RESULT_REMOTE_IS_FILE.value()), "Remote path is a file." }, 
    { Integer.valueOf(RESULT_BUFFER_OVERRUN.value()), "Receiving too much data." }, 
    { Integer.valueOf(RESULT_CONNECTION_TIMEOUT.value()), "timeout" }, 
    { Integer.valueOf(RESULT_FILE_EXIST.value()), "File exists." }, 
    { Integer.valueOf(RESULT_FAIL_DELETE_FILE.value()), "Fail delete file." }, 
    { Integer.valueOf(RESULT_FAIL_CREATE_FILE.value()), "Fail create file." } });
  
  public static String getMessage(int val)
  {
    return (String)CODE2MESSAGE.get(Integer.valueOf(val));
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.service.SyncServiceReturnCode
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.util;

public final class ArrayHelper
{
  public static void swap32bitsToArray(int value, byte[] dest, int offset)
  {
    dest[offset] = ((byte)(value & 0xFF));
    dest[(offset + 1)] = ((byte)((value & 0xFF00) >> 8));
    dest[(offset + 2)] = ((byte)((value & 0xFF0000) >> 16));
    dest[(offset + 3)] = ((byte)((value & 0xFF000000) >> 24));
  }
  
  public static int swap32bitFromArray(byte[] value, int offset)
  {
    return value[offset] & 0xFF | 
      (value[(offset + 1)] & 0xFF) << 8 | 
      (value[(offset + 2)] & 0xFF) << 16 | 
      (value[(offset + 3)] & 0xFF) << 24;
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.util.ArrayHelper
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.util;

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayUtil$ArrayIterator<K>
  implements Iterator<K>
{
  protected final K[] objs;
  protected final K[] origin;
  protected int index = 0;
  
  public ArrayUtil$ArrayIterator(K[] objs)
  {
    origin = objs;
    
    this.objs = (origin == null ? null : (Object[])origin.clone());
  }
  
  public boolean hasNext()
  {
    if (objs == null) {
      return false;
    }
    return index < objs.length;
  }
  
  public K next()
  {
    if (objs == null) {
      throw new NoSuchElementException();
    }
    if (objs.length <= index) {
      throw new NoSuchElementException();
    }
    if (objs[index] != origin[index]) {
      throw new ConcurrentModificationException();
    }
    return (K)objs[(index++)];
  }
  
  public void remove()
  {
    throw new UnsupportedOperationException();
  }
}

/* Location:
 * Qualified Name:     org.tizen.sdblib.util.ArrayUtil.ArrayIterator
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.tizen.sdblib.util;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayUtil
{
  public static <T> boolean isEmpty(T[] array)
  {
    if (array == null) {
      return true;
    }
    return array.length == 0;
  }
  
  public static <T> T[] newArray(Class<? extends T> type, int length)
  {
    return (Object[])Array.newInstance(type, length);
  }
  
  public static class ArrayIterator<K>
    implements Iterator<K>
  {
    protected final K[] objs;
    protected final K[] origin;
    protected int index = 0;
    
    public ArrayIterator(K[] objs)
    {
      origin = objs;
      
      this.objs = (origin == null ? null : (Object[])origin.clone());
    }
    
    public boolean hasNext()
    {
      if (objs == null) {
        return false;
      }
      return index < objs.length;
    }
    
    public K next()
    {
      if (objs == null) {
        throw new NoSuchElementException();
      }
      if (objs.length <= index) {
        throw new NoSuchElementException();
      }
      if (objs[index] != origin[index]) {
        throw new ConcurrentModificationException();
      }
      return (K)objs[(index++)];
    }
    
    public void remove()
    {
      throw new 
1 2 3 4 5 6 7

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd