Skip to content

Commit

Permalink
Merge pull request quamotion#179 from quamotion/fixes/use-native-code
Browse files Browse the repository at this point in the history
Use built-in methods where possible
  • Loading branch information
qmfrederik authored May 28, 2020
2 parents ee2ae83 + 9671e14 commit 6b6b99c
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 142 deletions.
5 changes: 1 addition & 4 deletions SharpAdbClient.Tests/DummySyncService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SharpAdbClient.Tests
{
Expand Down Expand Up @@ -35,7 +32,7 @@ public void Pull(string remotePath, Stream stream, IProgress<int> progress, Canc
{
}

public void Push(Stream stream, string remotePath, int permissions, DateTime timestamp, IProgress<int> progress, CancellationToken cancellationToken)
public void Push(Stream stream, string remotePath, int permissions, DateTimeOffset timestamp, IProgress<int> progress, CancellationToken cancellationToken)
{
this.UploadedFiles.Add(remotePath, stream);
}
Expand Down
23 changes: 0 additions & 23 deletions SharpAdbClient.Tests/Extensions/ArrayHelperTests.cs

This file was deleted.

4 changes: 2 additions & 2 deletions SharpAdbClient.Tests/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task ReadLogTests()
Assert.Equal(3u, log.Id);
Assert.NotNull(log.Data);
Assert.Equal(179, log.Data.Length);
Assert.Equal(new DateTime(2015, 11, 14, 23, 38, 20, 590, DateTimeKind.Utc), log.TimeStamp);
Assert.Equal(new DateTime(2015, 11, 14, 23, 38, 20, DateTimeKind.Utc), log.TimeStamp);

var androidLog = (AndroidLogEntry)log;
Assert.Equal(Priority.Info, androidLog.Priority);
Expand Down Expand Up @@ -58,7 +58,7 @@ public async Task ReadEventLogTest()
Assert.Equal(2u, entry.Id);
Assert.NotNull(entry.Data);
Assert.Equal(39, entry.Data.Length);
Assert.Equal(new DateTime(2015, 11, 16, 1, 48, 40, 525, DateTimeKind.Utc), entry.TimeStamp);
Assert.Equal(new DateTime(2015, 11, 16, 1, 48, 40, DateTimeKind.Utc), entry.TimeStamp);

var eventLog = (EventLogEntry)entry;
Assert.Equal(0, eventLog.Tag);
Expand Down
2 changes: 1 addition & 1 deletion SharpAdbClient/DeviceCommands/GetPropReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override void ProcessNewLines(IEnumerable<string> lines)
continue;
}

var m = line.Match(GetpropRegex, RegexOptions.Compiled);
var m = Regex.Match(line, GetpropRegex, RegexOptions.Compiled);
if (m.Success)
{
string label = m.Groups[1].Value.Trim();
Expand Down
2 changes: 1 addition & 1 deletion SharpAdbClient/DeviceCommands/InstallReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override void ProcessNewLines(IEnumerable<string> lines)
}
else
{
var m = line.Match(FailurePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
var m = Regex.Match(line, FailurePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
this.ErrorMessage = UnknownError;

if (m.Success)
Expand Down
43 changes: 0 additions & 43 deletions SharpAdbClient/Extensions/ArrayHelper.cs

This file was deleted.

53 changes: 0 additions & 53 deletions SharpAdbClient/Extensions/StringHelper.cs

This file was deleted.

2 changes: 1 addition & 1 deletion SharpAdbClient/FileStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public int Size
/// <summary>
/// Gets or sets the time of last modification.
/// </summary>
public DateTime Time
public DateTimeOffset Time
{
get;
set;
Expand Down
2 changes: 1 addition & 1 deletion SharpAdbClient/ISyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface ISyncService : IDisposable
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> that can be used to cancel the task.
/// </param>
void Push(Stream stream, string remotePath, int permissions, DateTime timestamp, IProgress<int> progress, CancellationToken cancellationToken);
void Push(Stream stream, string remotePath, int permissions, DateTimeOffset timestamp, IProgress<int> progress, CancellationToken cancellationToken);

/// <summary>
/// Pulls (downloads) a file from the remote device.
Expand Down
2 changes: 1 addition & 1 deletion SharpAdbClient/Logs/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LogEntry
/// <summary>
/// Gets or sets the date and time at which the message was logged.
/// </summary>
public DateTime TimeStamp { get; set; }
public DateTimeOffset TimeStamp { get; set; }

/// <summary>
/// Gets or sets the log id (v3) of the payload effective UID of logger (v2);
Expand Down
3 changes: 1 addition & 2 deletions SharpAdbClient/Logs/LogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ public async Task<LogEntry> ReadEntry(CancellationToken cancellationToken)
return null;
}

DateTime timestamp = DateTimeHelper.Epoch.AddSeconds(sec);
timestamp = timestamp.AddMilliseconds(nsec / 1000000d);
var timestamp = DateTimeOffset.FromUnixTimeSeconds(sec);

switch ((LogId)id)
{
Expand Down
12 changes: 5 additions & 7 deletions SharpAdbClient/Receivers/ConsoleOutputReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ namespace SharpAdbClient
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// Recieves console output, and makes the console output available as a <see cref="string"/>. To
/// fetch the console output that was received, used the <see cref="ToString"/> method.
/// </summary>
public class ConsoleOutputReceiver : MultiLineReceiver
{
/// <summary>
/// Logging tag
/// </summary>
private const string Tag = nameof(ConsoleOutputReceiver);
private const RegexOptions DefaultRegexOptions = RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase;

/// <summary>
/// The logger to use when logging messages.
Expand Down Expand Up @@ -84,23 +82,23 @@ public void ThrowOnError(string line)
}

// for "aborting" commands
if (line.IsMatch("Aborting.$"))
if (Regex.IsMatch(line, "Aborting.$", DefaultRegexOptions))
{
this.logger.LogWarning($"The remote execution returned: {line}");
throw new CommandAbortingException($"The remote execution returned: '{line}'");
}

// for busybox applets
// cmd: applet not found
if (line.IsMatch("applet not found$"))
if (Regex.IsMatch(line, "applet not found$", DefaultRegexOptions))
{
this.logger.LogWarning($"The remote execution returned: '{line}'");
throw new FileNotFoundException($"The remote execution returned: '{line}'");
}

// checks if the permission to execute the command was denied.
// workitem: 16822
if (line.IsMatch("(permission|access) denied$"))
if (Regex.IsMatch(line, "(permission|access) denied$", DefaultRegexOptions))
{
this.logger.LogWarning($"The remote execution returned: '{line}'");
throw new PermissionDeniedException($"The remote execution returned: '{line}'");
Expand Down
6 changes: 3 additions & 3 deletions SharpAdbClient/SyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void Open()
}

/// <inheritdoc/>
public void Push(Stream stream, string remotePath, int permissions, DateTime timestamp, IProgress<int> progress, CancellationToken cancellationToken)
public void Push(Stream stream, string remotePath, int permissions, DateTimeOffset timestamp, IProgress<int> progress, CancellationToken cancellationToken)
{
if (stream == null)
{
Expand Down Expand Up @@ -198,7 +198,7 @@ public void Push(Stream stream, string remotePath, int permissions, DateTime tim
}

// create the DONE message
int time = (int)timestamp.ToUnixEpoch();
int time = (int)timestamp.ToUnixTimeSeconds();
this.Socket.SendSyncRequest(SyncCommand.DONE, time);

// read the result, in a byte array containing 2 ints
Expand Down Expand Up @@ -365,7 +365,7 @@ private void ReadStatistics(FileStatistics value)

value.FileMode = (UnixFileMode)BitConverter.ToInt32(statResult, 0);
value.Size = BitConverter.ToInt32(statResult, 4);
value.Time = DateTimeHelper.Epoch.AddSeconds(BitConverter.ToInt32(statResult, 8)).ToLocalTime();
value.Time = DateTimeOffset.FromUnixTimeSeconds(BitConverter.ToInt32(statResult, 8));
}
}
}

0 comments on commit 6b6b99c

Please sign in to comment.