2015-08-20 01:06:44 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
using System.Net;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
using System.Net.Http;
|
2018-04-15 21:27:43 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using System.Linq;
|
2015-08-20 01:06:44 -04:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2018-02-08 02:23:45 -05:00
|
|
|
|
public class RemoteFile : IFile
|
|
|
|
|
{
|
|
|
|
|
string Location;
|
2018-02-16 23:24:03 -05:00
|
|
|
|
string[] FileCache = new string[0];
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
|
|
|
|
public RemoteFile(string location) : base(string.Empty)
|
|
|
|
|
{
|
|
|
|
|
Location = location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Retrieve()
|
|
|
|
|
{
|
|
|
|
|
using (var cl = new HttpClient())
|
|
|
|
|
FileCache = cl.GetStringAsync(Location).Result.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override long Length()
|
|
|
|
|
{
|
|
|
|
|
Retrieve();
|
2018-04-26 02:13:04 -04:00
|
|
|
|
return FileCache.Sum(l => l.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task<string[]> Tail(int lineCount)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(FileCache);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 01:06:44 -04:00
|
|
|
|
public class IFile
|
|
|
|
|
{
|
|
|
|
|
public IFile(String fileName)
|
|
|
|
|
{
|
2018-02-08 02:23:45 -05:00
|
|
|
|
if (fileName != string.Empty)
|
|
|
|
|
{
|
|
|
|
|
Name = fileName;
|
2018-04-21 18:18:20 -04:00
|
|
|
|
Handle = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, true), Utilities.EncodingType);
|
2018-04-15 21:27:43 -04:00
|
|
|
|
|
2018-02-08 02:23:45 -05:00
|
|
|
|
sze = Handle.BaseStream.Length;
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 02:23:45 -05:00
|
|
|
|
public virtual long Length()
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
sze = Handle.BaseStream.Length;
|
|
|
|
|
return sze;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
|
{
|
2017-06-12 13:50:00 -04:00
|
|
|
|
Handle?.Close();
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 13:50:00 -04:00
|
|
|
|
public String[] ReadAllLines()
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2017-06-12 13:50:00 -04:00
|
|
|
|
return Handle?.ReadToEnd().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-12 13:50:00 -04:00
|
|
|
|
public String GetText()
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
2017-06-12 13:50:00 -04:00
|
|
|
|
return Handle?.ReadToEnd();
|
2015-08-20 01:06:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 21:27:43 -04:00
|
|
|
|
public virtual async Task<String[]> Tail(int lineCount)
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
var buffer = new List<string>(lineCount);
|
|
|
|
|
string line;
|
|
|
|
|
for (int i = 0; i < lineCount; i++)
|
|
|
|
|
{
|
2018-04-15 21:27:43 -04:00
|
|
|
|
line = await Handle.ReadLineAsync();
|
2015-08-20 01:06:44 -04:00
|
|
|
|
if (line == null) return buffer.ToArray();
|
|
|
|
|
buffer.Add(line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int lastLine = lineCount - 1; //The index of the last line read from the buffer. Everything > this index was read earlier than everything <= this indes
|
|
|
|
|
|
2018-04-15 21:27:43 -04:00
|
|
|
|
while (null != (line = await Handle.ReadLineAsync()))
|
2015-08-20 01:06:44 -04:00
|
|
|
|
{
|
|
|
|
|
lastLine++;
|
|
|
|
|
if (lastLine == lineCount) lastLine = 0;
|
|
|
|
|
buffer[lastLine] = line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lastLine == lineCount - 1) return buffer.ToArray();
|
|
|
|
|
var retVal = new string[lineCount];
|
|
|
|
|
buffer.CopyTo(lastLine + 1, retVal, 0, lineCount - lastLine - 1);
|
|
|
|
|
buffer.CopyTo(0, retVal, lineCount - lastLine - 1, lastLine + 1);
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
//END
|
|
|
|
|
|
|
|
|
|
private long sze;
|
|
|
|
|
private String Name;
|
|
|
|
|
private StreamReader Handle;
|
|
|
|
|
}
|
|
|
|
|
}
|