mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-12 02:30:23 +01:00
merge common implementations and server implementations
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public abstract class AbstractMap<T, TU> : IDictionary<T, TU>
|
||||
{
|
||||
public virtual void Clear ()
|
||||
{
|
||||
EntrySet ().Clear ();
|
||||
}
|
||||
|
||||
public virtual bool ContainsKey (object name)
|
||||
{
|
||||
return EntrySet ().Any (p => p.Key.Equals ((T)name));
|
||||
}
|
||||
|
||||
public abstract ICollection<KeyValuePair<T, TU>> EntrySet ();
|
||||
|
||||
public virtual TU Get (object key)
|
||||
{
|
||||
return EntrySet ().Where (p => p.Key.Equals (key)).Select (p => p.Value).FirstOrDefault ();
|
||||
}
|
||||
|
||||
protected virtual IEnumerator<KeyValuePair<T, TU>> InternalGetEnumerator ()
|
||||
{
|
||||
return EntrySet ().GetEnumerator ();
|
||||
}
|
||||
|
||||
public virtual bool IsEmpty ()
|
||||
{
|
||||
return !EntrySet ().Any ();
|
||||
}
|
||||
|
||||
public virtual TU Put (T key, TU value)
|
||||
{
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
|
||||
public virtual TU Remove (object key)
|
||||
{
|
||||
Iterator<TU> iterator = EntrySet () as Iterator<TU>;
|
||||
if (iterator == null) {
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
while (iterator.HasNext ()) {
|
||||
TU local = iterator.Next ();
|
||||
if (local.Equals ((T)key)) {
|
||||
iterator.Remove ();
|
||||
return local;
|
||||
}
|
||||
}
|
||||
return default(TU);
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<T, TU>>.Add (KeyValuePair<T, TU> item)
|
||||
{
|
||||
Put (item.Key, item.Value);
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<T, TU>>.Contains (KeyValuePair<T, TU> item)
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<T, TU>>.CopyTo (KeyValuePair<T, TU>[] array, int arrayIndex)
|
||||
{
|
||||
EntrySet ().CopyTo (array, arrayIndex);
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<T, TU>>.Remove (KeyValuePair<T, TU> item)
|
||||
{
|
||||
Remove (item.Key);
|
||||
return true;
|
||||
}
|
||||
|
||||
void IDictionary<T, TU>.Add (T key, TU value)
|
||||
{
|
||||
Put (key, value);
|
||||
}
|
||||
|
||||
bool IDictionary<T, TU>.ContainsKey (T key)
|
||||
{
|
||||
return ContainsKey (key);
|
||||
}
|
||||
|
||||
bool IDictionary<T, TU>.Remove (T key)
|
||||
{
|
||||
if (ContainsKey (key)) {
|
||||
Remove (key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IDictionary<T, TU>.TryGetValue (T key, out TU value)
|
||||
{
|
||||
if (ContainsKey (key)) {
|
||||
value = Get (key);
|
||||
return true;
|
||||
}
|
||||
value = default(TU);
|
||||
return false;
|
||||
}
|
||||
|
||||
IEnumerator<KeyValuePair<T, TU>> IEnumerable<KeyValuePair<T, TU>>.GetEnumerator ()
|
||||
{
|
||||
return InternalGetEnumerator ();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator ()
|
||||
{
|
||||
return InternalGetEnumerator ();
|
||||
}
|
||||
|
||||
public virtual int Count {
|
||||
get { return EntrySet ().Count; }
|
||||
}
|
||||
|
||||
public TU this[T key] {
|
||||
get { return Get (key); }
|
||||
set { Put (key, value); }
|
||||
}
|
||||
|
||||
public virtual IEnumerable<T> Keys {
|
||||
get { return EntrySet ().Select (p => p.Key); }
|
||||
}
|
||||
|
||||
int ICollection<KeyValuePair<T, TU>>.Count {
|
||||
get { return Count; }
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<T, TU>>.IsReadOnly {
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
ICollection<T> IDictionary<T, TU>.Keys {
|
||||
get { return Keys.ToList (); }
|
||||
}
|
||||
|
||||
ICollection<TU> IDictionary<T, TU>.Values {
|
||||
get { return Values.ToList (); }
|
||||
}
|
||||
|
||||
public virtual IEnumerable<TU> Values {
|
||||
get { return EntrySet ().Select (p => p.Value); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Arrays
|
||||
{
|
||||
public static List<T> AsList<T> (params T[] array)
|
||||
{
|
||||
return array.ToList ();
|
||||
}
|
||||
|
||||
public static bool Equals<T> (T[] a1, T[] a2)
|
||||
{
|
||||
if (a1.Length != a2.Length) {
|
||||
return false;
|
||||
}
|
||||
return !a1.Where((t, i) => !t.Equals(a2[i])).Any();
|
||||
}
|
||||
|
||||
public static void Fill<T> (T[] array, T val)
|
||||
{
|
||||
Fill (array, 0, array.Length, val);
|
||||
}
|
||||
|
||||
public static void Fill<T> (T[] array, int start, int end, T val)
|
||||
{
|
||||
for (int i = start; i < end; i++) {
|
||||
array[i] = val;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Sort (string[] array)
|
||||
{
|
||||
Array.Sort (array, (s1,s2) => string.CompareOrdinal (s1,s2));
|
||||
}
|
||||
|
||||
public static void Sort<T> (T[] array)
|
||||
{
|
||||
Array.Sort (array);
|
||||
}
|
||||
|
||||
public static void Sort<T> (T[] array, IComparer<T> c)
|
||||
{
|
||||
Array.Sort (array, c);
|
||||
}
|
||||
|
||||
public static void Sort<T> (T[] array, int start, int count)
|
||||
{
|
||||
Array.Sort (array, start, count);
|
||||
}
|
||||
|
||||
public static void Sort<T> (T[] array, int start, int count, IComparer<T> c)
|
||||
{
|
||||
Array.Sort (array, start, count, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class BufferedReader : StreamReader
|
||||
{
|
||||
public BufferedReader (InputStreamReader r) : base(r.BaseStream)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// BufferedWriter.cs
|
||||
//
|
||||
// Author:
|
||||
// Lluis Sanchez Gual <lluis@novell.com>
|
||||
//
|
||||
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// 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 THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS 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.
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class BufferedWriter
|
||||
{
|
||||
StreamWriter _writer;
|
||||
|
||||
public BufferedWriter (StreamWriter w)
|
||||
{
|
||||
_writer = w;
|
||||
}
|
||||
|
||||
public void Write (string s)
|
||||
{
|
||||
_writer.Write (s);
|
||||
}
|
||||
|
||||
public void NewLine ()
|
||||
{
|
||||
_writer.WriteLine ();
|
||||
}
|
||||
|
||||
public void Close ()
|
||||
{
|
||||
//Stream.`Close` method deleted
|
||||
//_writer.Close ();
|
||||
_writer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class CharBuffer : CharSequence
|
||||
{
|
||||
internal string Wrapped;
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return Wrapped;
|
||||
}
|
||||
|
||||
public static CharBuffer Wrap (string str)
|
||||
{
|
||||
CharBuffer buffer = new CharBuffer ();
|
||||
buffer.Wrapped = str;
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class CharSequence
|
||||
{
|
||||
public static implicit operator CharSequence (string str)
|
||||
{
|
||||
return new StringCharSequence (str);
|
||||
}
|
||||
|
||||
public static implicit operator CharSequence (StringBuilder str)
|
||||
{
|
||||
return new StringCharSequence (str.ToString ());
|
||||
}
|
||||
}
|
||||
|
||||
class StringCharSequence: CharSequence
|
||||
{
|
||||
string _str;
|
||||
|
||||
public StringCharSequence (string str)
|
||||
{
|
||||
this._str = str;
|
||||
}
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return _str;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal static class Collections<T>
|
||||
{
|
||||
static readonly IList<T> Empty = new T [0];
|
||||
public static IList<T> EmptySet {
|
||||
get { return Empty; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Collections
|
||||
{
|
||||
public static bool AddAll<T> (ICollection<T> list, IEnumerable toAdd)
|
||||
{
|
||||
foreach (T t in toAdd)
|
||||
list.Add (t);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static TV Remove<TK, TV> (IDictionary<TK, TV> map, TK toRemove) where TK : class
|
||||
{
|
||||
TV local;
|
||||
if (map.TryGetValue (toRemove, out local)) {
|
||||
map.Remove (toRemove);
|
||||
return local;
|
||||
}
|
||||
return default(TV);
|
||||
}
|
||||
|
||||
|
||||
public static T[] ToArray<T> (ICollection<T> list)
|
||||
{
|
||||
T[] array = new T[list.Count];
|
||||
list.CopyTo (array, 0);
|
||||
return array;
|
||||
}
|
||||
|
||||
public static T[] ToArray<T>(List<object> list)
|
||||
{
|
||||
T[] array = new T[list.Count];
|
||||
for(int c = 0; c < list.Count; c++)
|
||||
{
|
||||
array[c] = (T)list[c];
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
public static TU[] ToArray<T,TU> (ICollection<T> list, TU[] res) where T:TU
|
||||
{
|
||||
if (res.Length < list.Count)
|
||||
res = new TU [list.Count];
|
||||
|
||||
int n = 0;
|
||||
foreach (T t in list)
|
||||
res [n++] = t;
|
||||
|
||||
if (res.Length > list.Count)
|
||||
res [list.Count] = default (T);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static IDictionary<TK,TV> EmptyMap<TK,TV> ()
|
||||
{
|
||||
return new Dictionary<TK,TV> ();
|
||||
}
|
||||
|
||||
public static IList<T> EmptyList<T> ()
|
||||
{
|
||||
return Collections<T>.EmptySet;
|
||||
}
|
||||
|
||||
public static ICollection<T> EmptySet<T> ()
|
||||
{
|
||||
return Collections<T>.EmptySet;
|
||||
}
|
||||
|
||||
public static IList<T> NCopies<T> (int n, T elem)
|
||||
{
|
||||
List<T> list = new List<T> (n);
|
||||
while (n-- > 0) {
|
||||
list.Add (elem);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void Reverse<T> (IList<T> list)
|
||||
{
|
||||
int end = list.Count - 1;
|
||||
int index = 0;
|
||||
while (index < end) {
|
||||
T tmp = list [index];
|
||||
list [index] = list [end];
|
||||
list [end] = tmp;
|
||||
++index;
|
||||
--end;
|
||||
}
|
||||
}
|
||||
|
||||
public static ICollection<T> Singleton<T> (T item)
|
||||
{
|
||||
List<T> list = new List<T> (1);
|
||||
list.Add (item);
|
||||
return list;
|
||||
}
|
||||
|
||||
public static IList<T> SingletonList<T> (T item)
|
||||
{
|
||||
List<T> list = new List<T> (1);
|
||||
list.Add (item);
|
||||
return list;
|
||||
}
|
||||
|
||||
public static IList<T> SynchronizedList<T> (IList<T> list)
|
||||
{
|
||||
return new SynchronizedList<T> (list);
|
||||
}
|
||||
|
||||
public static ICollection<T> UnmodifiableCollection<T> (ICollection<T> list)
|
||||
{
|
||||
return list;
|
||||
}
|
||||
|
||||
public static IList<T> UnmodifiableList<T> (IList<T> list)
|
||||
{
|
||||
return new ReadOnlyCollection<T> (list);
|
||||
}
|
||||
|
||||
public static ICollection<T> UnmodifiableSet<T> (ICollection<T> list)
|
||||
{
|
||||
return list;
|
||||
}
|
||||
|
||||
public static IDictionary<TK,TV> UnmodifiableMap<TK,TV> (IDictionary<TK,TV> dict)
|
||||
{
|
||||
return dict;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ConcurrentHashMap<T, TU> : AbstractMap<T, TU>, IConcurrentMap<T, TU>
|
||||
{
|
||||
private Dictionary<T, TU> _table;
|
||||
|
||||
public ConcurrentHashMap ()
|
||||
{
|
||||
_table = new Dictionary<T, TU> ();
|
||||
}
|
||||
|
||||
public ConcurrentHashMap (int initialCapacity, float loadFactor, int concurrencyLevel)
|
||||
{
|
||||
_table = new Dictionary<T, TU> (initialCapacity);
|
||||
}
|
||||
|
||||
public override void Clear ()
|
||||
{
|
||||
lock (_table) {
|
||||
_table = new Dictionary<T, TU> ();
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ContainsKey (object name)
|
||||
{
|
||||
return _table.ContainsKey ((T)name);
|
||||
}
|
||||
|
||||
public override ICollection<KeyValuePair<T, TU>> EntrySet ()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public override TU Get (object key)
|
||||
{
|
||||
TU local;
|
||||
_table.TryGetValue ((T)key, out local);
|
||||
return local;
|
||||
}
|
||||
|
||||
protected override IEnumerator<KeyValuePair<T, TU>> InternalGetEnumerator ()
|
||||
{
|
||||
return _table.GetEnumerator ();
|
||||
}
|
||||
|
||||
public override bool IsEmpty ()
|
||||
{
|
||||
return _table.Count == 0;
|
||||
}
|
||||
|
||||
public override TU Put (T key, TU value)
|
||||
{
|
||||
lock (_table) {
|
||||
TU old = Get (key);
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable[key] = value;
|
||||
_table = newTable;
|
||||
return old;
|
||||
}
|
||||
}
|
||||
|
||||
public TU PutIfAbsent (T key, TU value)
|
||||
{
|
||||
lock (_table) {
|
||||
if (!ContainsKey (key)) {
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable[key] = value;
|
||||
_table = newTable;
|
||||
return value;
|
||||
}
|
||||
return Get (key);
|
||||
}
|
||||
}
|
||||
|
||||
public override TU Remove (object key)
|
||||
{
|
||||
lock (_table) {
|
||||
TU old = Get ((T)key);
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable.Remove ((T)key);
|
||||
_table = newTable;
|
||||
return old;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove (object key, object value)
|
||||
{
|
||||
lock (_table) {
|
||||
if (ContainsKey (key) && value.Equals (Get (key))) {
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable.Remove ((T)key);
|
||||
_table = newTable;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Replace (T key, TU oldValue, TU newValue)
|
||||
{
|
||||
lock (_table) {
|
||||
if (ContainsKey (key) && oldValue.Equals (Get (key))) {
|
||||
Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
|
||||
newTable[key] = newValue;
|
||||
_table = newTable;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<T> Keys {
|
||||
get { return _table.Keys; }
|
||||
}
|
||||
|
||||
public override IEnumerable<TU> Values {
|
||||
get { return _table.Values; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public abstract class DateFormat
|
||||
{
|
||||
public const int Default = 2;
|
||||
|
||||
public static DateFormat GetDateTimeInstance (int dateStyle, int timeStyle)
|
||||
{
|
||||
return GetDateTimeInstance (dateStyle, timeStyle, CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
public static DateFormat GetDateTimeInstance (int dateStyle, int timeStyle, CultureInfo aLocale)
|
||||
{
|
||||
return new SimpleDateFormat (aLocale.DateTimeFormat.FullDateTimePattern, aLocale);
|
||||
}
|
||||
|
||||
TimeZoneInfo _timeZone;
|
||||
|
||||
public abstract DateTime Parse (string value);
|
||||
|
||||
public TimeZoneInfo GetTimeZone ()
|
||||
{
|
||||
return _timeZone;
|
||||
}
|
||||
|
||||
public void SetTimeZone (TimeZoneInfo timeZone)
|
||||
{
|
||||
this._timeZone = timeZone;
|
||||
}
|
||||
|
||||
public abstract string Format (DateTime time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class EnumeratorWrapper<T> : Iterator<T>
|
||||
{
|
||||
object _collection;
|
||||
IEnumerator<T> _e;
|
||||
T _lastVal;
|
||||
bool _more;
|
||||
bool _copied;
|
||||
|
||||
public EnumeratorWrapper (object collection, IEnumerator<T> e)
|
||||
{
|
||||
this._e = e;
|
||||
this._collection = collection;
|
||||
_more = e.MoveNext ();
|
||||
}
|
||||
|
||||
public override bool HasNext ()
|
||||
{
|
||||
return _more;
|
||||
}
|
||||
|
||||
public override T Next ()
|
||||
{
|
||||
if (!_more)
|
||||
throw new NoSuchElementException ();
|
||||
_lastVal = _e.Current;
|
||||
_more = _e.MoveNext ();
|
||||
return _lastVal;
|
||||
}
|
||||
|
||||
public override void Remove ()
|
||||
{
|
||||
ICollection<T> col = _collection as ICollection<T>;
|
||||
if (col == null) {
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
if (_more && !_copied) {
|
||||
// Read the remaining elements, since the current enumerator
|
||||
// will be invalid after removing the element
|
||||
List<T> remaining = new List<T> ();
|
||||
do {
|
||||
remaining.Add (_e.Current);
|
||||
} while (_e.MoveNext ());
|
||||
_e = remaining.GetEnumerator ();
|
||||
_e.MoveNext ();
|
||||
_copied = true;
|
||||
}
|
||||
col.Remove (_lastVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
//
|
||||
// Exceptions.cs
|
||||
//
|
||||
// Author:
|
||||
// Lluis Sanchez Gual <lluis@novell.com>
|
||||
//
|
||||
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// 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 THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS 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.
|
||||
|
||||
using System;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class VirtualMachineError : Error
|
||||
{
|
||||
}
|
||||
|
||||
public class StackOverflowError : VirtualMachineError
|
||||
{
|
||||
}
|
||||
|
||||
public class BrokenBarrierException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
internal class BufferUnderflowException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class CharacterCodingException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class DataFormatException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class EofException : Exception
|
||||
{
|
||||
public EofException ()
|
||||
{
|
||||
}
|
||||
|
||||
public EofException (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class Error : Exception
|
||||
{
|
||||
public Error ()
|
||||
{
|
||||
}
|
||||
|
||||
public Error (Exception ex) : base("Runtime Exception", ex)
|
||||
{
|
||||
}
|
||||
|
||||
public Error (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
|
||||
public Error (string msg, Exception ex) : base(msg, ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class ExecutionException : Exception
|
||||
{
|
||||
public ExecutionException (Exception inner): base ("Execution failed", inner)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class InstantiationException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class InterruptedIoException : Exception
|
||||
{
|
||||
public InterruptedIoException (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class MissingResourceException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class NoSuchAlgorithmException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class NoSuchElementException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
internal class NoSuchMethodException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
internal class OverlappingFileLockException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class ParseException : Exception
|
||||
{
|
||||
public ParseException ()
|
||||
{
|
||||
}
|
||||
|
||||
public ParseException (string msg, int errorOffset) : base(string.Format ("Msg: {0}. Error Offset: {1}", msg, errorOffset))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class RuntimeException : Exception
|
||||
{
|
||||
public RuntimeException ()
|
||||
{
|
||||
}
|
||||
|
||||
public RuntimeException (Exception ex) : base("Runtime Exception", ex)
|
||||
{
|
||||
}
|
||||
|
||||
public RuntimeException (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
|
||||
public RuntimeException (string msg, Exception ex) : base(msg, ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal class StringIndexOutOfBoundsException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class UnknownHostException : Exception
|
||||
{
|
||||
public UnknownHostException ()
|
||||
{
|
||||
}
|
||||
|
||||
public UnknownHostException(string message) : base(message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UnknownHostException (Exception ex): base ("Host not found", ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class UnsupportedEncodingException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
internal class UriSyntaxException : Exception
|
||||
{
|
||||
public UriSyntaxException (string s, string msg) : base(s + " " + msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal class ZipException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class GitException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class ConnectException: Exception
|
||||
{
|
||||
public ConnectException (string msg): base (msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class KeyManagementException: Exception
|
||||
{
|
||||
}
|
||||
|
||||
class IllegalCharsetNameException: Exception
|
||||
{
|
||||
public IllegalCharsetNameException (string msg): base (msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class UnsupportedCharsetException: Exception
|
||||
{
|
||||
public UnsupportedCharsetException (string msg): base (msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,696 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
//using Windows.Networking;
|
||||
//using Windows.Networking.Sockets;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
private static readonly long EpochTicks;
|
||||
|
||||
static Extensions()
|
||||
{
|
||||
DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
EpochTicks = time.Ticks;
|
||||
}
|
||||
|
||||
public static void Add<T>(this IList<T> list, int index, T item)
|
||||
{
|
||||
list.Insert(index, item);
|
||||
}
|
||||
|
||||
public static void AddFirst<T>(this IList<T> list, T item)
|
||||
{
|
||||
list.Insert(0, item);
|
||||
}
|
||||
|
||||
public static void AddLast<T>(this IList<T> list, T item)
|
||||
{
|
||||
list.Add(item);
|
||||
}
|
||||
|
||||
public static void RemoveLast<T>(this IList<T> list)
|
||||
{
|
||||
if (list.Count > 0)
|
||||
list.Remove(list.Count - 1);
|
||||
}
|
||||
|
||||
public static StringBuilder AppendRange(this StringBuilder sb, string str, int start, int end)
|
||||
{
|
||||
return sb.Append(str, start, end - start);
|
||||
}
|
||||
|
||||
public static StringBuilder Delete(this StringBuilder sb, int start, int end)
|
||||
{
|
||||
return sb.Remove(start, end - start);
|
||||
}
|
||||
|
||||
public static void SetCharAt(this StringBuilder sb, int index, char c)
|
||||
{
|
||||
sb[index] = c;
|
||||
}
|
||||
|
||||
public static int IndexOf(this StringBuilder sb, string str)
|
||||
{
|
||||
return sb.ToString().IndexOf(str);
|
||||
}
|
||||
|
||||
public static int BitCount(int val)
|
||||
{
|
||||
uint num = (uint)val;
|
||||
int count = 0;
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
if ((num & 1) != 0)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
num >>= 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static IndexOutOfRangeException CreateIndexOutOfRangeException(int index)
|
||||
{
|
||||
return new IndexOutOfRangeException("Index: " + index);
|
||||
}
|
||||
|
||||
public static string Decode(this Encoding e, byte[] chars, int start, int len)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bom = e.GetPreamble();
|
||||
if (bom != null && bom.Length > 0)
|
||||
{
|
||||
if (len >= bom.Length)
|
||||
{
|
||||
int pos = start;
|
||||
bool hasBom = true;
|
||||
for (int n = 0; n < bom.Length && hasBom; n++)
|
||||
{
|
||||
if (bom[n] != chars[pos++])
|
||||
hasBom = false;
|
||||
}
|
||||
if (hasBom)
|
||||
{
|
||||
len -= pos - start;
|
||||
start = pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
return e.GetString(chars, start, len);
|
||||
}
|
||||
catch (DecoderFallbackException)
|
||||
{
|
||||
throw new CharacterCodingException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Encoding GetEncoding(string name)
|
||||
{
|
||||
// Encoding e = Encoding.GetEncoding (name, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
|
||||
try
|
||||
{
|
||||
|
||||
Encoding e = Encoding.GetEncoding(name.Replace('_', '-'));
|
||||
if (e is UTF8Encoding)
|
||||
return new UTF8Encoding(false, true);
|
||||
|
||||
return e;
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
throw new UnsupportedCharsetException(name);
|
||||
}
|
||||
}
|
||||
|
||||
public static ICollection<KeyValuePair<T, TU>> EntrySet<T, TU>(this IDictionary<T, TU> s)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
public static bool AddItem<T>(this IList<T> list, T item)
|
||||
{
|
||||
list.Add(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool AddItem<T>(this ICollection<T> list, T item)
|
||||
{
|
||||
list.Add(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static TU Get<T, TU>(this IDictionary<T, TU> d, T key)
|
||||
{
|
||||
TU val;
|
||||
d.TryGetValue(key, out val);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
public static TU Put<T, TU>(this IDictionary<T, TU> d, T key, TU value)
|
||||
{
|
||||
TU old;
|
||||
d.TryGetValue(key, out old);
|
||||
d[key] = value;
|
||||
return old;
|
||||
}
|
||||
|
||||
public static void PutAll<T, TU>(this IDictionary<T, TU> d, IDictionary<T, TU> values)
|
||||
{
|
||||
foreach (KeyValuePair<T, TU> val in values)
|
||||
d[val.Key] = val.Value;
|
||||
}
|
||||
|
||||
|
||||
public static CultureInfo GetEnglishCulture()
|
||||
{
|
||||
return new CultureInfo("en-US");
|
||||
}
|
||||
|
||||
public static T GetFirst<T>(this IList<T> list)
|
||||
{
|
||||
return ((list.Count == 0) ? default(T) : list[0]);
|
||||
}
|
||||
|
||||
public static CultureInfo GetGermanCulture()
|
||||
{
|
||||
CultureInfo r = new CultureInfo("de-DE");
|
||||
return r;
|
||||
}
|
||||
|
||||
public static T GetLast<T>(this IList<T> list)
|
||||
{
|
||||
return ((list.Count == 0) ? default(T) : list[list.Count - 1]);
|
||||
}
|
||||
|
||||
public static int GetOffset(this TimeZoneInfo tzone, long date)
|
||||
{
|
||||
return (int)tzone.GetUtcOffset(MillisToDateTimeOffset(date, 0).DateTime).TotalMilliseconds;
|
||||
}
|
||||
|
||||
public static long GetTime(this DateTime dateTime)
|
||||
{
|
||||
return new DateTimeOffset(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc), TimeSpan.Zero).ToMillisecondsSinceEpoch();
|
||||
}
|
||||
|
||||
public static void InitCause(this Exception ex, Exception cause)
|
||||
{
|
||||
Console.WriteLine(cause);
|
||||
}
|
||||
|
||||
public static bool IsEmpty<T>(this ICollection<T> col)
|
||||
{
|
||||
return (col.Count == 0);
|
||||
}
|
||||
|
||||
public static bool IsEmpty<T>(this Stack<T> col)
|
||||
{
|
||||
return (col.Count == 0);
|
||||
}
|
||||
|
||||
public static bool IsLower(this char c)
|
||||
{
|
||||
return char.IsLower(c);
|
||||
}
|
||||
|
||||
public static bool IsUpper(this char c)
|
||||
{
|
||||
return char.IsUpper(c);
|
||||
}
|
||||
|
||||
public static Iterator<T> Iterator<T>(this ICollection<T> col)
|
||||
{
|
||||
return new EnumeratorWrapper<T>(col, col.GetEnumerator());
|
||||
}
|
||||
|
||||
public static Iterator<T> Iterator<T>(this IEnumerable<T> col)
|
||||
{
|
||||
return new EnumeratorWrapper<T>(col, col.GetEnumerator());
|
||||
}
|
||||
|
||||
public static T Last<T>(this ICollection<T> col)
|
||||
{
|
||||
IList<T> list = col as IList<T>;
|
||||
if (list != null)
|
||||
{
|
||||
return list[list.Count - 1];
|
||||
}
|
||||
return col.Last();
|
||||
}
|
||||
|
||||
public static int LowestOneBit(int val)
|
||||
{
|
||||
return (1 << NumberOfTrailingZeros(val));
|
||||
}
|
||||
|
||||
public static bool Matches(this string str, string regex)
|
||||
{
|
||||
Regex regex2 = new Regex(regex);
|
||||
return regex2.IsMatch(str);
|
||||
}
|
||||
|
||||
public static DateTime CreateDate(long milliSecondsSinceEpoch)
|
||||
{
|
||||
long num = EpochTicks + (milliSecondsSinceEpoch * 10000);
|
||||
return new DateTime(num);
|
||||
}
|
||||
|
||||
public static DateTime CreateDateFromUTC(long milliSecondsSinceEpoch)
|
||||
{
|
||||
long num = EpochTicks + (milliSecondsSinceEpoch * 10000);
|
||||
return new DateTime(num, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
|
||||
public static DateTimeOffset MillisToDateTimeOffset(long milliSecondsSinceEpoch, long offsetMinutes)
|
||||
{
|
||||
TimeSpan offset = TimeSpan.FromMinutes(offsetMinutes);
|
||||
long num = EpochTicks + (milliSecondsSinceEpoch * 10000);
|
||||
return new DateTimeOffset(num + offset.Ticks, offset);
|
||||
}
|
||||
|
||||
public static int NumberOfLeadingZeros(int val)
|
||||
{
|
||||
uint num = (uint)val;
|
||||
int count = 0;
|
||||
while ((num & 0x80000000) == 0)
|
||||
{
|
||||
num = num << 1;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static int NumberOfTrailingZeros(int val)
|
||||
{
|
||||
uint num = (uint)val;
|
||||
int count = 0;
|
||||
while ((num & 1) == 0)
|
||||
{
|
||||
num = num >> 1;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static int Read(this StreamReader reader, char[] data)
|
||||
{
|
||||
return reader.Read(data, 0, data.Length);
|
||||
}
|
||||
|
||||
public static T Remove<T>(this IList<T> list, T item)
|
||||
{
|
||||
int index = list.IndexOf(item);
|
||||
if (index == -1)
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
T local = list[index];
|
||||
list.RemoveAt(index);
|
||||
return local;
|
||||
}
|
||||
|
||||
public static T Remove<T>(this IList<T> list, int i)
|
||||
{
|
||||
T old;
|
||||
try
|
||||
{
|
||||
old = list[i];
|
||||
list.RemoveAt(i);
|
||||
}
|
||||
catch (IndexOutOfRangeException)
|
||||
{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return old;
|
||||
}
|
||||
|
||||
public static T RemoveFirst<T>(this IList<T> list)
|
||||
{
|
||||
return list.Remove(0);
|
||||
}
|
||||
|
||||
public static string ReplaceAll(this string str, string regex, string replacement)
|
||||
{
|
||||
Regex rgx = new Regex(regex);
|
||||
|
||||
if (replacement.IndexOfAny(new[] { '\\', '$' }) != -1)
|
||||
{
|
||||
// Back references not yet supported
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int n = 0; n < replacement.Length; n++)
|
||||
{
|
||||
char c = replacement[n];
|
||||
if (c == '$')
|
||||
throw new NotSupportedException("Back references not supported");
|
||||
if (c == '\\')
|
||||
c = replacement[++n];
|
||||
sb.Append(c);
|
||||
}
|
||||
replacement = sb.ToString();
|
||||
}
|
||||
|
||||
return rgx.Replace(str, replacement);
|
||||
}
|
||||
|
||||
public static bool RegionMatches(this string str, bool ignoreCase, int toOffset, string other, int ooffset, int len)
|
||||
{
|
||||
if (toOffset < 0 || ooffset < 0 || toOffset + len > str.Length || ooffset + len > other.Length)
|
||||
return false;
|
||||
return string.Compare(str, toOffset, other, ooffset, len) == 0;
|
||||
}
|
||||
|
||||
public static T Set<T>(this IList<T> list, int index, T item)
|
||||
{
|
||||
T old = list[index];
|
||||
list[index] = item;
|
||||
return old;
|
||||
}
|
||||
|
||||
public static int Signum(long val)
|
||||
{
|
||||
if (val < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (val > 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void RemoveAll<T, TU>(this ICollection<T> col, ICollection<TU> items) where TU : T
|
||||
{
|
||||
foreach (var u in items)
|
||||
col.Remove(u);
|
||||
}
|
||||
|
||||
public static bool ContainsAll<T, TU>(this ICollection<T> col, ICollection<TU> items) where TU : T
|
||||
{
|
||||
foreach (var u in items)
|
||||
if (!col.Any(n => (ReferenceEquals(n, u)) || n.Equals(u)))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool Contains<T>(this ICollection<T> col, object item)
|
||||
{
|
||||
if (!(item is T))
|
||||
return false;
|
||||
return col.Any(n => (ReferenceEquals(n, item)) || n.Equals(item));
|
||||
}
|
||||
|
||||
public static void Sort<T>(this IList<T> list)
|
||||
{
|
||||
List<T> sorted = new List<T>(list);
|
||||
sorted.Sort();
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
list[i] = sorted[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static void Sort<T>(this IList<T> list, IComparer<T> comparer)
|
||||
{
|
||||
List<T> sorted = new List<T>(list);
|
||||
sorted.Sort(comparer);
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
list[i] = sorted[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static string[] Split(this string str, string regex)
|
||||
{
|
||||
return str.Split(regex, 0);
|
||||
}
|
||||
|
||||
public static string[] Split(this string str, string regex, int limit)
|
||||
{
|
||||
Regex rgx = new Regex(regex);
|
||||
List<string> list = new List<string>();
|
||||
int startIndex = 0;
|
||||
if (limit != 1)
|
||||
{
|
||||
int nm = 1;
|
||||
foreach (Match match in rgx.Matches(str))
|
||||
{
|
||||
list.Add(str.Substring(startIndex, match.Index - startIndex));
|
||||
startIndex = match.Index + match.Length;
|
||||
if (limit > 0 && ++nm == limit)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (startIndex < str.Length)
|
||||
{
|
||||
list.Add(str.Substring(startIndex));
|
||||
}
|
||||
if (limit >= 0)
|
||||
{
|
||||
int count = list.Count - 1;
|
||||
while ((count >= 0) && (list[count].Length == 0))
|
||||
{
|
||||
count--;
|
||||
}
|
||||
list.RemoveRange(count + 1, (list.Count - count) - 1);
|
||||
}
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
public static IList<T> SubList<T>(this IList<T> list, int start, int len)
|
||||
{
|
||||
List<T> sublist = new List<T>(len);
|
||||
for (int i = start; i < (start + len); i++)
|
||||
{
|
||||
sublist.Add(list[i]);
|
||||
}
|
||||
return sublist;
|
||||
}
|
||||
|
||||
public static char[] ToCharArray(this string str)
|
||||
{
|
||||
char[] destination = new char[str.Length];
|
||||
str.CopyTo(0, destination, 0, str.Length);
|
||||
return destination;
|
||||
}
|
||||
|
||||
public static long ToMillisecondsSinceEpoch(this DateTime dateTime)
|
||||
{
|
||||
if (dateTime.Kind != DateTimeKind.Utc)
|
||||
{
|
||||
throw new ArgumentException("dateTime is expected to be expressed as a UTC DateTime", "dateTime");
|
||||
}
|
||||
return new DateTimeOffset(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc), TimeSpan.Zero).ToMillisecondsSinceEpoch();
|
||||
}
|
||||
|
||||
public static long ToMillisecondsSinceEpoch(this DateTimeOffset dateTimeOffset)
|
||||
{
|
||||
return (((dateTimeOffset.Ticks - dateTimeOffset.Offset.Ticks) - EpochTicks) / TimeSpan.TicksPerMillisecond);
|
||||
}
|
||||
|
||||
public static string ToOctalString(int val)
|
||||
{
|
||||
return Convert.ToString(val, 8);
|
||||
}
|
||||
|
||||
public static string ToHexString(int val)
|
||||
{
|
||||
return Convert.ToString(val, 16);
|
||||
}
|
||||
|
||||
public static string ToString(object val)
|
||||
{
|
||||
return val.ToString();
|
||||
}
|
||||
|
||||
public static string ToString(int val, int bas)
|
||||
{
|
||||
return Convert.ToString(val, bas);
|
||||
}
|
||||
|
||||
public static IList<TU> UpcastTo<T, TU>(this IList<T> s) where T : TU
|
||||
{
|
||||
List<TU> list = new List<TU>(s.Count);
|
||||
for (int i = 0; i < s.Count; i++)
|
||||
{
|
||||
list.Add(s[i]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static ICollection<TU> UpcastTo<T, TU>(this ICollection<T> s) where T : TU
|
||||
{
|
||||
List<TU> list = new List<TU>(s.Count);
|
||||
foreach (var v in s)
|
||||
{
|
||||
list.Add(v);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static T ValueOf<T>(T val)
|
||||
{
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
//use? for NUnit-testing?
|
||||
//public static string GetTestName(object obj)
|
||||
//{
|
||||
// return GetTestName();
|
||||
//}
|
||||
|
||||
//public static string GetTestName()
|
||||
//{
|
||||
// MethodBase met;
|
||||
// int n = 0;
|
||||
// do
|
||||
// {
|
||||
// met = new StackFrame(n).GetMethod();
|
||||
// if (met != null)
|
||||
// {
|
||||
// foreach (Attribute at in met.GetCustomAttributes(true))
|
||||
// {
|
||||
// if (at.GetType().FullName == "NUnit.Framework.TestAttribute")
|
||||
// {
|
||||
// // Convert back to camel case
|
||||
// string name = met.Name;
|
||||
// if (char.IsUpper(name[0]))
|
||||
// name = char.ToLower(name[0]) + name.Substring(1);
|
||||
// return name;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// n++;
|
||||
// } while (met != null);
|
||||
// return "";
|
||||
//}
|
||||
|
||||
public static string GetHostAddress(this IPAddress addr)
|
||||
{
|
||||
return addr.ToString();
|
||||
}
|
||||
|
||||
|
||||
public static IPAddress GetAddressByName(string host)
|
||||
{
|
||||
if (host == "0.0.0.0")
|
||||
{
|
||||
return IPAddress.Any;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return IPAddress.Parse(host);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static IPAddress[] GetAddressesByName(string host)
|
||||
{
|
||||
//IReadOnlyList<EndpointPair> data = null;
|
||||
|
||||
//try
|
||||
//{
|
||||
// Task.Run(async () =>
|
||||
// {
|
||||
// data = await DatagramSocket.GetEndpointPairsAsync(new HostName(host), "0");
|
||||
// }).Wait();
|
||||
//}
|
||||
//catch (Exception ex)
|
||||
//{
|
||||
// return null;
|
||||
//}
|
||||
|
||||
//return data != null
|
||||
// ? data.Where(i => i.RemoteHostName.Type == HostNameType.Ipv4)
|
||||
// .GroupBy(i => i.RemoteHostName.DisplayName)
|
||||
// .Select(i => IPAddress.Parse(i.First().RemoteHostName.DisplayName))
|
||||
// .ToArray()
|
||||
// : null;
|
||||
|
||||
//get v4-address only
|
||||
var entry = Task.Run(() => System.Net.Dns.GetHostEntryAsync(host))
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
return entry.AddressList
|
||||
.Where(addr => addr.AddressFamily == AddressFamily.InterNetwork)
|
||||
.ToArray();
|
||||
|
||||
}
|
||||
|
||||
public static string GetImplementationVersion(this Assembly asm)
|
||||
{
|
||||
return asm.GetName().Version.ToString();
|
||||
}
|
||||
|
||||
public static string GetHost(this Uri uri)
|
||||
{
|
||||
return string.IsNullOrEmpty(uri.Host) ? "" : uri.Host;
|
||||
}
|
||||
|
||||
public static string GetUserInfo(this Uri uri)
|
||||
{
|
||||
return string.IsNullOrEmpty(uri.UserInfo) ? null : uri.UserInfo;
|
||||
}
|
||||
|
||||
public static string GetQuery(this Uri uri)
|
||||
{
|
||||
return string.IsNullOrEmpty(uri.Query) ? null : uri.Query;
|
||||
}
|
||||
|
||||
public static int GetLocalPort(this Socket socket)
|
||||
{
|
||||
return ((IPEndPoint)socket.LocalEndPoint).Port;
|
||||
}
|
||||
|
||||
public static int GetPort(this Socket socket)
|
||||
{
|
||||
return ((IPEndPoint)socket.RemoteEndPoint).Port;
|
||||
}
|
||||
|
||||
public static IPAddress GetInetAddress(this Socket socket)
|
||||
{
|
||||
return ((IPEndPoint)socket.RemoteEndPoint).Address;
|
||||
}
|
||||
|
||||
|
||||
/*public static bool RemoveElement(this ArrayList list, object elem)
|
||||
{
|
||||
int i = list.IndexOf(elem);
|
||||
if (i == -1)
|
||||
return false;
|
||||
list.RemoveAt(i);
|
||||
return true;
|
||||
}*/
|
||||
|
||||
public static Semaphore CreateSemaphore(int count)
|
||||
{
|
||||
return new Semaphore(count, int.MaxValue);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FileInputStream : InputStream
|
||||
{
|
||||
public FileInputStream (FilePath file) : this(file.GetPath ())
|
||||
{
|
||||
}
|
||||
|
||||
public FileInputStream (string file)
|
||||
{
|
||||
if (!File.Exists (file)) {
|
||||
throw new FileNotFoundException ("File not found", file);
|
||||
}
|
||||
Wrapped = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class FileOutputStream : OutputStream
|
||||
{
|
||||
public FileOutputStream (FilePath file): this (file.GetPath (), false)
|
||||
{
|
||||
}
|
||||
|
||||
public FileOutputStream (string file): this (file, false)
|
||||
{
|
||||
}
|
||||
|
||||
public FileOutputStream (FilePath file, bool append) : this(file.GetPath (), append)
|
||||
{
|
||||
}
|
||||
|
||||
public FileOutputStream (string file, bool append)
|
||||
{
|
||||
try {
|
||||
if (append) {
|
||||
Wrapped = File.Open (file, FileMode.Append, FileAccess.Write);
|
||||
} else {
|
||||
Wrapped = File.Open (file, FileMode.Create, FileAccess.Write);
|
||||
}
|
||||
} catch (DirectoryNotFoundException) {
|
||||
throw new FileNotFoundException ("File not found: " + file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FilePath
|
||||
{
|
||||
private string _path;
|
||||
private static long _tempCounter;
|
||||
|
||||
public FilePath ()
|
||||
{
|
||||
}
|
||||
|
||||
public FilePath (string path)
|
||||
: this ((string) null, path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public FilePath (FilePath other, string child)
|
||||
: this ((string) other, child)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public FilePath (string other, string child)
|
||||
{
|
||||
if (other == null) {
|
||||
_path = child;
|
||||
} else {
|
||||
while (!string.IsNullOrEmpty(child) && (child[0] == Path.DirectorySeparatorChar || child[0] == Path.AltDirectorySeparatorChar))
|
||||
child = child.Substring (1);
|
||||
|
||||
if (!string.IsNullOrEmpty(other) && other[other.Length - 1] == Path.VolumeSeparatorChar)
|
||||
other += Path.DirectorySeparatorChar;
|
||||
|
||||
_path = Path.Combine (other, child);
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator FilePath (string name)
|
||||
{
|
||||
return new FilePath (name);
|
||||
}
|
||||
|
||||
public static implicit operator string (FilePath filePath)
|
||||
{
|
||||
return filePath == null ? null : filePath._path;
|
||||
}
|
||||
|
||||
public override bool Equals (object obj)
|
||||
{
|
||||
FilePath other = obj as FilePath;
|
||||
if (other == null)
|
||||
return false;
|
||||
return GetCanonicalPath () == other.GetCanonicalPath ();
|
||||
}
|
||||
|
||||
public override int GetHashCode ()
|
||||
{
|
||||
return _path.GetHashCode ();
|
||||
}
|
||||
|
||||
public bool CreateNewFile ()
|
||||
{
|
||||
try {
|
||||
//Stream.`Close` method deleted
|
||||
//File.Open (_path, FileMode.CreateNew).Close ();
|
||||
File.Open(_path, FileMode.CreateNew).Dispose();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static FilePath CreateTempFile ()
|
||||
{
|
||||
return new FilePath (Path.GetTempFileName ());
|
||||
}
|
||||
|
||||
public static FilePath CreateTempFile (string prefix, string suffix)
|
||||
{
|
||||
return CreateTempFile (prefix, suffix, null);
|
||||
}
|
||||
|
||||
public static FilePath CreateTempFile (string prefix, string suffix, FilePath directory)
|
||||
{
|
||||
string file;
|
||||
if (prefix == null) {
|
||||
throw new ArgumentNullException ("prefix");
|
||||
}
|
||||
if (prefix.Length < 3) {
|
||||
throw new ArgumentException ("prefix must have at least 3 characters");
|
||||
}
|
||||
string str = (directory == null) ? Path.GetTempPath () : directory.GetPath ();
|
||||
do {
|
||||
file = Path.Combine (str, prefix + Interlocked.Increment (ref _tempCounter) + suffix);
|
||||
} while (File.Exists (file));
|
||||
|
||||
new FileOutputStream (file).Close ();
|
||||
return new FilePath (file);
|
||||
}
|
||||
|
||||
|
||||
public void DeleteOnExit ()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public FilePath GetAbsoluteFile ()
|
||||
{
|
||||
return new FilePath (Path.GetFullPath (_path));
|
||||
}
|
||||
|
||||
public string GetAbsolutePath ()
|
||||
{
|
||||
return Path.GetFullPath (_path);
|
||||
}
|
||||
|
||||
public FilePath GetCanonicalFile ()
|
||||
{
|
||||
return new FilePath (GetCanonicalPath ());
|
||||
}
|
||||
|
||||
public string GetCanonicalPath ()
|
||||
{
|
||||
string p = Path.GetFullPath (_path);
|
||||
p.TrimEnd (Path.DirectorySeparatorChar);
|
||||
return p;
|
||||
}
|
||||
|
||||
public string GetName ()
|
||||
{
|
||||
return Path.GetFileName (_path);
|
||||
}
|
||||
|
||||
public FilePath GetParentFile ()
|
||||
{
|
||||
return new FilePath (Path.GetDirectoryName (_path));
|
||||
}
|
||||
|
||||
public string GetPath ()
|
||||
{
|
||||
return _path;
|
||||
}
|
||||
|
||||
public bool IsAbsolute ()
|
||||
{
|
||||
return Path.IsPathRooted (_path);
|
||||
}
|
||||
|
||||
public bool IsDirectory ()
|
||||
{
|
||||
return false; // FileHelper.Instance.IsDirectory(this);
|
||||
}
|
||||
|
||||
public bool IsFile ()
|
||||
{
|
||||
return false; //FileHelper.Instance.IsFile (this);
|
||||
}
|
||||
|
||||
public long LastModified ()
|
||||
{
|
||||
return 0; // FileHelper.Instance.LastModified(this);
|
||||
}
|
||||
|
||||
public long Length ()
|
||||
{
|
||||
return 0; // FileHelper.Instance.Length(this);
|
||||
}
|
||||
|
||||
public string[] List ()
|
||||
{
|
||||
return List (null);
|
||||
}
|
||||
|
||||
public string[] List (IFilenameFilter filter)
|
||||
{
|
||||
try {
|
||||
if (IsFile ())
|
||||
return null;
|
||||
List<string> list = new List<string> ();
|
||||
foreach (string filePth in Directory.GetFileSystemEntries (_path)) {
|
||||
string fileName = Path.GetFileName (filePth);
|
||||
if ((filter == null) || filter.Accept (this, fileName)) {
|
||||
list.Add (fileName);
|
||||
}
|
||||
}
|
||||
return list.ToArray ();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public FilePath[] ListFiles ()
|
||||
{
|
||||
try {
|
||||
if (IsFile ())
|
||||
return null;
|
||||
List<FilePath> list = new List<FilePath> ();
|
||||
foreach (string filePath in Directory.GetFileSystemEntries (_path)) {
|
||||
list.Add (new FilePath (filePath));
|
||||
}
|
||||
return list.ToArray ();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static void MakeDirWritable (string dir)
|
||||
{
|
||||
//FileHelper.Instance.MakeDirWritable (dir);
|
||||
}
|
||||
|
||||
static void MakeFileWritable (string file)
|
||||
{
|
||||
//FileHelper.Instance.MakeFileWritable (file);
|
||||
}
|
||||
|
||||
public bool Mkdir ()
|
||||
{
|
||||
try {
|
||||
if (Directory.Exists (_path))
|
||||
return false;
|
||||
Directory.CreateDirectory (_path);
|
||||
return true;
|
||||
} catch (Exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Mkdirs ()
|
||||
{
|
||||
try {
|
||||
if (Directory.Exists (_path))
|
||||
return false;
|
||||
Directory.CreateDirectory (_path);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool RenameTo (FilePath file)
|
||||
{
|
||||
return RenameTo (file._path);
|
||||
}
|
||||
|
||||
public bool RenameTo (string name)
|
||||
{
|
||||
return false; // FileHelper.Instance.RenameTo(this, name);
|
||||
}
|
||||
|
||||
public bool SetLastModified (long milis)
|
||||
{
|
||||
return false; // FileHelper.Instance.SetLastModified(this, milis);
|
||||
}
|
||||
|
||||
public bool SetReadOnly ()
|
||||
{
|
||||
return false; // FileHelper.Instance.SetReadOnly(this);
|
||||
}
|
||||
|
||||
public Uri ToUri ()
|
||||
{
|
||||
return new Uri (_path);
|
||||
}
|
||||
|
||||
// Don't change the case of this method, since ngit does reflection on it
|
||||
public bool CanExecute ()
|
||||
{
|
||||
return false; // FileHelper.Instance.CanExecute(this);
|
||||
}
|
||||
|
||||
// Don't change the case of this method, since ngit does reflection on it
|
||||
public bool SetExecutable (bool exec)
|
||||
{
|
||||
return false; // FileHelper.Instance.SetExecutable(this, exec);
|
||||
}
|
||||
|
||||
public string GetParent ()
|
||||
{
|
||||
string p = Path.GetDirectoryName (_path);
|
||||
if (string.IsNullOrEmpty(p) || p == _path)
|
||||
return null;
|
||||
return p;
|
||||
}
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return _path;
|
||||
}
|
||||
|
||||
static internal string PathSeparator {
|
||||
get { return Path.PathSeparator.ToString (); }
|
||||
}
|
||||
|
||||
static internal char PathSeparatorChar {
|
||||
get { return Path.PathSeparator; }
|
||||
}
|
||||
|
||||
static internal char SeparatorChar {
|
||||
get { return Path.DirectorySeparatorChar; }
|
||||
}
|
||||
|
||||
static internal string Separator {
|
||||
get { return Path.DirectorySeparatorChar.ToString (); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FileReader : InputStreamReader
|
||||
{
|
||||
//public FileReader (FilePath f) : base(f.GetPath ())
|
||||
//{
|
||||
//}
|
||||
//path -> fileStream
|
||||
public FileReader(InputStream s) : base(s)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
//resharper said, nobody using this. i believe it.
|
||||
//internal class FileWriter : StreamWriter
|
||||
//{
|
||||
// public FileWriter (FilePath path) : base(path.GetPath ())
|
||||
// {
|
||||
// }
|
||||
|
||||
// public FileWriter Append (string sequence)
|
||||
// {
|
||||
// Write (sequence);
|
||||
// return this;
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FilterInputStream : InputStream
|
||||
{
|
||||
protected InputStream In;
|
||||
|
||||
public FilterInputStream (InputStream s)
|
||||
{
|
||||
In = s;
|
||||
}
|
||||
|
||||
public override int Available ()
|
||||
{
|
||||
return In.Available ();
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
In.Close ();
|
||||
}
|
||||
|
||||
public override void Mark (int readlimit)
|
||||
{
|
||||
In.Mark (readlimit);
|
||||
}
|
||||
|
||||
public override bool MarkSupported ()
|
||||
{
|
||||
return In.MarkSupported ();
|
||||
}
|
||||
|
||||
public override int Read ()
|
||||
{
|
||||
return In.Read ();
|
||||
}
|
||||
|
||||
public override int Read (byte[] buf)
|
||||
{
|
||||
return In.Read (buf);
|
||||
}
|
||||
|
||||
public override int Read (byte[] b, int off, int len)
|
||||
{
|
||||
return In.Read (b, off, len);
|
||||
}
|
||||
|
||||
public override void Reset ()
|
||||
{
|
||||
In.Reset ();
|
||||
}
|
||||
|
||||
public override long Skip (long cnt)
|
||||
{
|
||||
return In.Skip (cnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class FilterOutputStream : OutputStream
|
||||
{
|
||||
protected OutputStream Out;
|
||||
|
||||
public FilterOutputStream (OutputStream os)
|
||||
{
|
||||
Out = os;
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
Out.Close ();
|
||||
}
|
||||
|
||||
public override void Flush ()
|
||||
{
|
||||
Out.Flush ();
|
||||
}
|
||||
|
||||
public override void Write (byte[] b)
|
||||
{
|
||||
Out.Write (b);
|
||||
}
|
||||
|
||||
public override void Write (int b)
|
||||
{
|
||||
Out.Write (b);
|
||||
}
|
||||
|
||||
public override void Write (byte[] b, int offset, int len)
|
||||
{
|
||||
Out.Write (b, offset, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Hashtable : Dictionary<object, object>
|
||||
{
|
||||
public void Put(object key, object value)
|
||||
{
|
||||
Add(key, value);
|
||||
}
|
||||
|
||||
public object Get(object key)
|
||||
{
|
||||
var m_key = Keys.SingleOrDefault(k => k.Equals(key));
|
||||
|
||||
return m_key != null ? this[m_key] : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// HttpURLConnection.cs
|
||||
//
|
||||
// Author:
|
||||
// Lluis Sanchez Gual <lluis@novell.com>
|
||||
//
|
||||
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// 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 THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS 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.
|
||||
|
||||
using System;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class UrlConnection
|
||||
{
|
||||
protected Uri Url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal interface ICallable<T>
|
||||
{
|
||||
T Call ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal interface IConcurrentMap<T, TU> : IDictionary<T, TU>
|
||||
{
|
||||
TU PutIfAbsent (T key, TU value);
|
||||
bool Remove (object key, object value);
|
||||
bool Replace (T key, TU oldValue, TU newValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public interface IExecutor
|
||||
{
|
||||
void Execute (IRunnable runnable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public interface IFilenameFilter
|
||||
{
|
||||
bool Accept (FilePath dir, string name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal interface IFuture<T>
|
||||
{
|
||||
bool Cancel (bool mayInterruptIfRunning);
|
||||
T Get ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal interface IPrivilegedAction<T>
|
||||
{
|
||||
T Run ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public interface IRunnable
|
||||
{
|
||||
void Run ();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class InputStream : IDisposable
|
||||
{
|
||||
private long _mark;
|
||||
protected Stream Wrapped;
|
||||
protected Stream BaseStream;
|
||||
|
||||
public static implicit operator InputStream (Stream s)
|
||||
{
|
||||
return Wrap (s);
|
||||
}
|
||||
|
||||
public static implicit operator Stream (InputStream s)
|
||||
{
|
||||
return s.GetWrappedStream ();
|
||||
}
|
||||
|
||||
public virtual int Available ()
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
return ((WrappedSystemStream)Wrapped).InputStream.Available ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
public virtual void Close ()
|
||||
{
|
||||
if (Wrapped != null) {
|
||||
//Stream.`Close` method deleted
|
||||
//Wrapped.Close ();
|
||||
Wrapped.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
Close ();
|
||||
}
|
||||
|
||||
internal Stream GetWrappedStream ()
|
||||
{
|
||||
// Always create a wrapper stream (not directly Wrapped) since the subclass
|
||||
// may be overriding methods that need to be called when used through the Stream class
|
||||
return new WrappedSystemStream (this);
|
||||
}
|
||||
|
||||
public virtual void Mark (int readlimit)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
((WrappedSystemStream)Wrapped).InputStream.Mark (readlimit);
|
||||
else {
|
||||
if (BaseStream is WrappedSystemStream)
|
||||
((WrappedSystemStream)BaseStream).OnMark (readlimit);
|
||||
if (Wrapped != null)
|
||||
_mark = Wrapped.Position;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool MarkSupported ()
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
return ((WrappedSystemStream)Wrapped).InputStream.MarkSupported ();
|
||||
return ((Wrapped != null) && Wrapped.CanSeek);
|
||||
}
|
||||
|
||||
public virtual int Read ()
|
||||
{
|
||||
if (Wrapped == null) {
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
return Wrapped.ReadByte ();
|
||||
}
|
||||
|
||||
public virtual int Read (byte[] buf)
|
||||
{
|
||||
return Read (buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public virtual int Read (byte[] b, int off, int len)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
return ((WrappedSystemStream)Wrapped).InputStream.Read (b, off, len);
|
||||
|
||||
if (Wrapped != null) {
|
||||
int num = Wrapped.Read (b, off, len);
|
||||
return ((num <= 0) ? -1 : num);
|
||||
}
|
||||
int totalRead = 0;
|
||||
while (totalRead < len) {
|
||||
int nr = Read ();
|
||||
if (nr == -1)
|
||||
return -1;
|
||||
b[off + totalRead] = (byte)nr;
|
||||
totalRead++;
|
||||
}
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
public virtual void Reset ()
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
((WrappedSystemStream)Wrapped).InputStream.Reset ();
|
||||
else {
|
||||
if (Wrapped == null)
|
||||
throw new IOException ();
|
||||
Wrapped.Position = _mark;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual long Skip (long cnt)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
return ((WrappedSystemStream)Wrapped).InputStream.Skip (cnt);
|
||||
|
||||
long n = cnt;
|
||||
while (n > 0) {
|
||||
if (Read () == -1)
|
||||
return cnt - n;
|
||||
n--;
|
||||
}
|
||||
return cnt - n;
|
||||
}
|
||||
|
||||
internal virtual bool CanSeek ()
|
||||
{
|
||||
if (Wrapped != null)
|
||||
return Wrapped.CanSeek;
|
||||
return false;
|
||||
}
|
||||
|
||||
internal virtual long Position {
|
||||
get
|
||||
{
|
||||
if (Wrapped != null)
|
||||
return Wrapped.Position;
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
set {
|
||||
if (Wrapped != null)
|
||||
Wrapped.Position = value;
|
||||
else
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Wrapped != null)
|
||||
{
|
||||
return Wrapped.Length;
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
static internal InputStream Wrap (Stream s)
|
||||
{
|
||||
InputStream stream = new InputStream ();
|
||||
stream.Wrapped = s;
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class InputStreamReader : StreamReader
|
||||
{
|
||||
//Stream(string path) constructor deleted
|
||||
//protected InputStreamReader (string file) : base(file)
|
||||
//{
|
||||
//}
|
||||
|
||||
public InputStreamReader (InputStream s) : base(s.GetWrappedStream ())
|
||||
{
|
||||
}
|
||||
|
||||
public InputStreamReader (InputStream s, string encoding) : base(s.GetWrappedStream (), Encoding.GetEncoding (encoding))
|
||||
{
|
||||
}
|
||||
|
||||
public InputStreamReader (InputStream s, Encoding e) : base(s.GetWrappedStream (), e)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public interface ITerator
|
||||
{
|
||||
bool HasNext ();
|
||||
object Next ();
|
||||
void Remove ();
|
||||
}
|
||||
|
||||
public abstract class Iterator<T> : IEnumerator<T>, ITerator
|
||||
{
|
||||
private T _lastValue;
|
||||
|
||||
object ITerator.Next ()
|
||||
{
|
||||
return Next ();
|
||||
}
|
||||
|
||||
public abstract bool HasNext ();
|
||||
public abstract T Next ();
|
||||
public abstract void Remove ();
|
||||
|
||||
bool IEnumerator.MoveNext ()
|
||||
{
|
||||
if (HasNext ()) {
|
||||
_lastValue = Next ();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void IEnumerator.Reset ()
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
void IDisposable.Dispose ()
|
||||
{
|
||||
}
|
||||
|
||||
T IEnumerator<T>.Current {
|
||||
get { return _lastValue; }
|
||||
}
|
||||
|
||||
object IEnumerator.Current {
|
||||
get { return _lastValue; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class LinkageError : Exception
|
||||
{
|
||||
public LinkageError (string msg) : base(msg)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
275
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/MD5.cs
Normal file
275
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/MD5.cs
Normal file
@@ -0,0 +1,275 @@
|
||||
//Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{ // **************************************************************
|
||||
// * Raw implementation of the MD5 hash algorithm
|
||||
// * from RFC 1321.
|
||||
// *
|
||||
// * Written By: Reid Borsuk and Jenny Zheng
|
||||
// * Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// **************************************************************
|
||||
|
||||
// Simple struct for the (a,b,c,d) which is used to compute the mesage digest.
|
||||
struct AbcdStruct
|
||||
{
|
||||
public uint A;
|
||||
public uint B;
|
||||
public uint C;
|
||||
public uint D;
|
||||
}
|
||||
|
||||
public sealed class Md5Core
|
||||
{
|
||||
//Prevent CSC from adding a default public constructor
|
||||
private Md5Core() { }
|
||||
|
||||
public static byte[] GetHash(string input, Encoding encoding)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
|
||||
if (null == encoding)
|
||||
throw new ArgumentNullException("encoding", "Unable to calculate hash over a string without a default encoding. Consider using the GetHash(string) overload to use UTF8 Encoding");
|
||||
|
||||
byte[] target = encoding.GetBytes(input);
|
||||
|
||||
return GetHash(target);
|
||||
}
|
||||
|
||||
public static byte[] GetHash(string input)
|
||||
{
|
||||
return GetHash(input, new UTF8Encoding());
|
||||
}
|
||||
|
||||
public static string GetHashString(byte[] input)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
|
||||
|
||||
string retval = BitConverter.ToString(GetHash(input));
|
||||
retval = retval.Replace("-", "");
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
public static string GetHashString(string input, Encoding encoding)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
|
||||
if (null == encoding)
|
||||
throw new ArgumentNullException("encoding", "Unable to calculate hash over a string without a default encoding. Consider using the GetHashString(string) overload to use UTF8 Encoding");
|
||||
|
||||
byte[] target = encoding.GetBytes(input);
|
||||
|
||||
return GetHashString(target);
|
||||
}
|
||||
|
||||
public static string GetHashString(string input)
|
||||
{
|
||||
return GetHashString(input, new UTF8Encoding());
|
||||
}
|
||||
|
||||
public static byte[] GetHash(byte[] input)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable to calculate hash over null input data");
|
||||
|
||||
//Intitial values defined in RFC 1321
|
||||
AbcdStruct abcd = new AbcdStruct();
|
||||
abcd.A = 0x67452301;
|
||||
abcd.B = 0xefcdab89;
|
||||
abcd.C = 0x98badcfe;
|
||||
abcd.D = 0x10325476;
|
||||
|
||||
//We pass in the input array by block, the final block of data must be handled specialy for padding & length embeding
|
||||
int startIndex = 0;
|
||||
while (startIndex <= input.Length - 64)
|
||||
{
|
||||
GetHashBlock(input, ref abcd, startIndex);
|
||||
startIndex += 64;
|
||||
}
|
||||
// The final data block.
|
||||
return GetHashFinalBlock(input, startIndex, input.Length - startIndex, abcd, (Int64)input.Length * 8);
|
||||
}
|
||||
|
||||
internal static byte[] GetHashFinalBlock(byte[] input, int ibStart, int cbSize, AbcdStruct abcd, Int64 len)
|
||||
{
|
||||
byte[] working = new byte[64];
|
||||
byte[] length = BitConverter.GetBytes(len);
|
||||
|
||||
//Padding is a single bit 1, followed by the number of 0s required to make size congruent to 448 modulo 512. Step 1 of RFC 1321
|
||||
//The CLR ensures that our buffer is 0-assigned, we don't need to explicitly set it. This is why it ends up being quicker to just
|
||||
//use a temporary array rather then doing in-place assignment (5% for small inputs)
|
||||
Array.Copy(input, ibStart, working, 0, cbSize);
|
||||
working[cbSize] = 0x80;
|
||||
|
||||
//We have enough room to store the length in this chunk
|
||||
if (cbSize < 56)
|
||||
{
|
||||
Array.Copy(length, 0, working, 56, 8);
|
||||
GetHashBlock(working, ref abcd, 0);
|
||||
}
|
||||
else //We need an aditional chunk to store the length
|
||||
{
|
||||
GetHashBlock(working, ref abcd, 0);
|
||||
//Create an entirely new chunk due to the 0-assigned trick mentioned above, to avoid an extra function call clearing the array
|
||||
working = new byte[64];
|
||||
Array.Copy(length, 0, working, 56, 8);
|
||||
GetHashBlock(working, ref abcd, 0);
|
||||
}
|
||||
byte[] output = new byte[16];
|
||||
Array.Copy(BitConverter.GetBytes(abcd.A), 0, output, 0, 4);
|
||||
Array.Copy(BitConverter.GetBytes(abcd.B), 0, output, 4, 4);
|
||||
Array.Copy(BitConverter.GetBytes(abcd.C), 0, output, 8, 4);
|
||||
Array.Copy(BitConverter.GetBytes(abcd.D), 0, output, 12, 4);
|
||||
return output;
|
||||
}
|
||||
|
||||
// Performs a single block transform of MD5 for a given set of ABCD inputs
|
||||
/* If implementing your own hashing framework, be sure to set the initial ABCD correctly according to RFC 1321:
|
||||
// A = 0x67452301;
|
||||
// B = 0xefcdab89;
|
||||
// C = 0x98badcfe;
|
||||
// D = 0x10325476;
|
||||
*/
|
||||
internal static void GetHashBlock(byte[] input, ref AbcdStruct abcdValue, int ibStart)
|
||||
{
|
||||
uint[] temp = Converter(input, ibStart);
|
||||
uint a = abcdValue.A;
|
||||
uint b = abcdValue.B;
|
||||
uint c = abcdValue.C;
|
||||
uint d = abcdValue.D;
|
||||
|
||||
a = R1(a, b, c, d, temp[0], 7, 0xd76aa478);
|
||||
d = R1(d, a, b, c, temp[1], 12, 0xe8c7b756);
|
||||
c = R1(c, d, a, b, temp[2], 17, 0x242070db);
|
||||
b = R1(b, c, d, a, temp[3], 22, 0xc1bdceee);
|
||||
a = R1(a, b, c, d, temp[4], 7, 0xf57c0faf);
|
||||
d = R1(d, a, b, c, temp[5], 12, 0x4787c62a);
|
||||
c = R1(c, d, a, b, temp[6], 17, 0xa8304613);
|
||||
b = R1(b, c, d, a, temp[7], 22, 0xfd469501);
|
||||
a = R1(a, b, c, d, temp[8], 7, 0x698098d8);
|
||||
d = R1(d, a, b, c, temp[9], 12, 0x8b44f7af);
|
||||
c = R1(c, d, a, b, temp[10], 17, 0xffff5bb1);
|
||||
b = R1(b, c, d, a, temp[11], 22, 0x895cd7be);
|
||||
a = R1(a, b, c, d, temp[12], 7, 0x6b901122);
|
||||
d = R1(d, a, b, c, temp[13], 12, 0xfd987193);
|
||||
c = R1(c, d, a, b, temp[14], 17, 0xa679438e);
|
||||
b = R1(b, c, d, a, temp[15], 22, 0x49b40821);
|
||||
|
||||
a = R2(a, b, c, d, temp[1], 5, 0xf61e2562);
|
||||
d = R2(d, a, b, c, temp[6], 9, 0xc040b340);
|
||||
c = R2(c, d, a, b, temp[11], 14, 0x265e5a51);
|
||||
b = R2(b, c, d, a, temp[0], 20, 0xe9b6c7aa);
|
||||
a = R2(a, b, c, d, temp[5], 5, 0xd62f105d);
|
||||
d = R2(d, a, b, c, temp[10], 9, 0x02441453);
|
||||
c = R2(c, d, a, b, temp[15], 14, 0xd8a1e681);
|
||||
b = R2(b, c, d, a, temp[4], 20, 0xe7d3fbc8);
|
||||
a = R2(a, b, c, d, temp[9], 5, 0x21e1cde6);
|
||||
d = R2(d, a, b, c, temp[14], 9, 0xc33707d6);
|
||||
c = R2(c, d, a, b, temp[3], 14, 0xf4d50d87);
|
||||
b = R2(b, c, d, a, temp[8], 20, 0x455a14ed);
|
||||
a = R2(a, b, c, d, temp[13], 5, 0xa9e3e905);
|
||||
d = R2(d, a, b, c, temp[2], 9, 0xfcefa3f8);
|
||||
c = R2(c, d, a, b, temp[7], 14, 0x676f02d9);
|
||||
b = R2(b, c, d, a, temp[12], 20, 0x8d2a4c8a);
|
||||
|
||||
a = R3(a, b, c, d, temp[5], 4, 0xfffa3942);
|
||||
d = R3(d, a, b, c, temp[8], 11, 0x8771f681);
|
||||
c = R3(c, d, a, b, temp[11], 16, 0x6d9d6122);
|
||||
b = R3(b, c, d, a, temp[14], 23, 0xfde5380c);
|
||||
a = R3(a, b, c, d, temp[1], 4, 0xa4beea44);
|
||||
d = R3(d, a, b, c, temp[4], 11, 0x4bdecfa9);
|
||||
c = R3(c, d, a, b, temp[7], 16, 0xf6bb4b60);
|
||||
b = R3(b, c, d, a, temp[10], 23, 0xbebfbc70);
|
||||
a = R3(a, b, c, d, temp[13], 4, 0x289b7ec6);
|
||||
d = R3(d, a, b, c, temp[0], 11, 0xeaa127fa);
|
||||
c = R3(c, d, a, b, temp[3], 16, 0xd4ef3085);
|
||||
b = R3(b, c, d, a, temp[6], 23, 0x04881d05);
|
||||
a = R3(a, b, c, d, temp[9], 4, 0xd9d4d039);
|
||||
d = R3(d, a, b, c, temp[12], 11, 0xe6db99e5);
|
||||
c = R3(c, d, a, b, temp[15], 16, 0x1fa27cf8);
|
||||
b = R3(b, c, d, a, temp[2], 23, 0xc4ac5665);
|
||||
|
||||
a = R4(a, b, c, d, temp[0], 6, 0xf4292244);
|
||||
d = R4(d, a, b, c, temp[7], 10, 0x432aff97);
|
||||
c = R4(c, d, a, b, temp[14], 15, 0xab9423a7);
|
||||
b = R4(b, c, d, a, temp[5], 21, 0xfc93a039);
|
||||
a = R4(a, b, c, d, temp[12], 6, 0x655b59c3);
|
||||
d = R4(d, a, b, c, temp[3], 10, 0x8f0ccc92);
|
||||
c = R4(c, d, a, b, temp[10], 15, 0xffeff47d);
|
||||
b = R4(b, c, d, a, temp[1], 21, 0x85845dd1);
|
||||
a = R4(a, b, c, d, temp[8], 6, 0x6fa87e4f);
|
||||
d = R4(d, a, b, c, temp[15], 10, 0xfe2ce6e0);
|
||||
c = R4(c, d, a, b, temp[6], 15, 0xa3014314);
|
||||
b = R4(b, c, d, a, temp[13], 21, 0x4e0811a1);
|
||||
a = R4(a, b, c, d, temp[4], 6, 0xf7537e82);
|
||||
d = R4(d, a, b, c, temp[11], 10, 0xbd3af235);
|
||||
c = R4(c, d, a, b, temp[2], 15, 0x2ad7d2bb);
|
||||
b = R4(b, c, d, a, temp[9], 21, 0xeb86d391);
|
||||
|
||||
abcdValue.A = unchecked(a + abcdValue.A);
|
||||
abcdValue.B = unchecked(b + abcdValue.B);
|
||||
abcdValue.C = unchecked(c + abcdValue.C);
|
||||
abcdValue.D = unchecked(d + abcdValue.D);
|
||||
}
|
||||
|
||||
//Manually unrolling these equations nets us a 20% performance improvement
|
||||
private static uint R1(uint a, uint b, uint c, uint d, uint x, int s, uint t)
|
||||
{
|
||||
// (b + LSR((a + F(b, c, d) + x + t), s))
|
||||
//F(x, y, z) ((x & y) | ((x ^ 0xFFFFFFFF) & z))
|
||||
return unchecked(b + Lsr((a + ((b & c) | ((b ^ 0xFFFFFFFF) & d)) + x + t), s));
|
||||
}
|
||||
|
||||
private static uint R2(uint a, uint b, uint c, uint d, uint x, int s, uint t)
|
||||
{
|
||||
// (b + LSR((a + G(b, c, d) + x + t), s))
|
||||
//G(x, y, z) ((x & z) | (y & (z ^ 0xFFFFFFFF)))
|
||||
return unchecked(b + Lsr((a + ((b & d) | (c & (d ^ 0xFFFFFFFF))) + x + t), s));
|
||||
}
|
||||
|
||||
private static uint R3(uint a, uint b, uint c, uint d, uint x, int s, uint t)
|
||||
{
|
||||
// (b + LSR((a + H(b, c, d) + k + i), s))
|
||||
//H(x, y, z) (x ^ y ^ z)
|
||||
return unchecked(b + Lsr((a + (b ^ c ^ d) + x + t), s));
|
||||
}
|
||||
|
||||
private static uint R4(uint a, uint b, uint c, uint d, uint x, int s, uint t)
|
||||
{
|
||||
// (b + LSR((a + I(b, c, d) + k + i), s))
|
||||
//I(x, y, z) (y ^ (x | (z ^ 0xFFFFFFFF)))
|
||||
return unchecked(b + Lsr((a + (c ^ (b | (d ^ 0xFFFFFFFF))) + x + t), s));
|
||||
}
|
||||
|
||||
// Implementation of left rotate
|
||||
// s is an int instead of a uint becuase the CLR requires the argument passed to >>/<< is of
|
||||
// type int. Doing the demoting inside this function would add overhead.
|
||||
private static uint Lsr(uint i, int s)
|
||||
{
|
||||
return ((i << s) | (i >> (32 - s)));
|
||||
}
|
||||
|
||||
//Convert input array into array of UInts
|
||||
private static uint[] Converter(byte[] input, int ibStart)
|
||||
{
|
||||
if (null == input)
|
||||
throw new ArgumentNullException("input", "Unable convert null array to array of uInts");
|
||||
|
||||
uint[] result = new uint[16];
|
||||
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
result[i] = input[ibStart + i * 4];
|
||||
result[i] += (uint)input[ibStart + i * 4 + 1] << 8;
|
||||
result[i] += (uint)input[ibStart + i * 4 + 2] << 16;
|
||||
result[i] += (uint)input[ibStart + i * 4 + 3] << 24;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
//Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
// **************************************************************
|
||||
// * Raw implementation of the MD5 hash algorithm
|
||||
// * from RFC 1321.
|
||||
// *
|
||||
// * Written By: Reid Borsuk and Jenny Zheng
|
||||
// * Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// **************************************************************
|
||||
|
||||
|
||||
#if SILVERLIGHT
|
||||
#else
|
||||
//public class MD5Managed : MD5
|
||||
#endif
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Md5Managed : HashAlgorithm
|
||||
|
||||
{
|
||||
public static Md5Managed Create()
|
||||
{
|
||||
return new Md5Managed();
|
||||
}
|
||||
|
||||
private byte[] _data;
|
||||
private AbcdStruct _abcd;
|
||||
private Int64 _totalLength;
|
||||
private int _dataSize;
|
||||
|
||||
public Md5Managed()
|
||||
{
|
||||
//field cannot access
|
||||
//HashSizeValue = 0x80;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
_data = new byte[64];
|
||||
_dataSize = 0;
|
||||
_totalLength = 0;
|
||||
_abcd = new AbcdStruct();
|
||||
//Intitial values as defined in RFC 1321
|
||||
_abcd.A = 0x67452301;
|
||||
_abcd.B = 0xefcdab89;
|
||||
_abcd.C = 0x98badcfe;
|
||||
_abcd.D = 0x10325476;
|
||||
}
|
||||
|
||||
protected override void HashCore(byte[] array, int ibStart, int cbSize)
|
||||
{
|
||||
int startIndex = ibStart;
|
||||
int totalArrayLength = _dataSize + cbSize;
|
||||
if (totalArrayLength >= 64)
|
||||
{
|
||||
Array.Copy(array, startIndex, _data, _dataSize, 64 - _dataSize);
|
||||
// Process message of 64 bytes (512 bits)
|
||||
Md5Core.GetHashBlock(_data, ref _abcd, 0);
|
||||
startIndex += 64 - _dataSize;
|
||||
totalArrayLength -= 64;
|
||||
while (totalArrayLength >= 64)
|
||||
{
|
||||
Array.Copy(array, startIndex, _data, 0, 64);
|
||||
Md5Core.GetHashBlock(array, ref _abcd, startIndex);
|
||||
totalArrayLength -= 64;
|
||||
startIndex += 64;
|
||||
}
|
||||
_dataSize = totalArrayLength;
|
||||
Array.Copy(array, startIndex, _data, 0, totalArrayLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(array, startIndex, _data, _dataSize, cbSize);
|
||||
_dataSize = totalArrayLength;
|
||||
}
|
||||
_totalLength += cbSize;
|
||||
}
|
||||
|
||||
protected override byte[] HashFinal()
|
||||
{
|
||||
//field cannot access
|
||||
//HashValue = Md5Core.GetHashFinalBlock(_data, 0, _dataSize, _abcd, _totalLength * 8);
|
||||
//return HashValue;
|
||||
return Md5Core.GetHashFinalBlock(_data, 0, _dataSize, _abcd, _totalLength * 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class Matcher
|
||||
{
|
||||
private int _current;
|
||||
private MatchCollection _matches;
|
||||
private Regex _regex;
|
||||
private string _str;
|
||||
|
||||
internal Matcher (Regex regex, string str)
|
||||
{
|
||||
this._regex = regex;
|
||||
this._str = str;
|
||||
}
|
||||
|
||||
public int End ()
|
||||
{
|
||||
if ((_matches == null) || (_current >= _matches.Count)) {
|
||||
throw new InvalidOperationException ();
|
||||
}
|
||||
return (_matches[_current].Index + _matches[_current].Length);
|
||||
}
|
||||
|
||||
public bool Find ()
|
||||
{
|
||||
if (_matches == null) {
|
||||
_matches = _regex.Matches (_str);
|
||||
_current = 0;
|
||||
}
|
||||
return (_current < _matches.Count);
|
||||
}
|
||||
|
||||
public bool Find (int index)
|
||||
{
|
||||
_matches = _regex.Matches (_str, index);
|
||||
_current = 0;
|
||||
return (_matches.Count > 0);
|
||||
}
|
||||
|
||||
public string Group (int n)
|
||||
{
|
||||
if ((_matches == null) || (_current >= _matches.Count)) {
|
||||
throw new InvalidOperationException ();
|
||||
}
|
||||
Group grp = _matches[_current].Groups[n];
|
||||
return grp.Success ? grp.Value : null;
|
||||
}
|
||||
|
||||
public bool Matches ()
|
||||
{
|
||||
_matches = null;
|
||||
return Find ();
|
||||
}
|
||||
|
||||
public string ReplaceFirst (string txt)
|
||||
{
|
||||
return _regex.Replace (_str, txt, 1);
|
||||
}
|
||||
|
||||
public Matcher Reset (CharSequence str)
|
||||
{
|
||||
return Reset (str.ToString ());
|
||||
}
|
||||
|
||||
public Matcher Reset (string str)
|
||||
{
|
||||
_matches = null;
|
||||
this._str = str;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int Start ()
|
||||
{
|
||||
if ((_matches == null) || (_current >= _matches.Count)) {
|
||||
throw new InvalidOperationException ();
|
||||
}
|
||||
return _matches[_current].Index;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public abstract class MessageDigest
|
||||
{
|
||||
public void Digest (byte[] buffer, int o, int len)
|
||||
{
|
||||
byte[] d = Digest ();
|
||||
d.CopyTo (buffer, o);
|
||||
}
|
||||
|
||||
public byte[] Digest (byte[] buffer)
|
||||
{
|
||||
Update (buffer);
|
||||
return Digest ();
|
||||
}
|
||||
|
||||
public abstract byte[] Digest ();
|
||||
public abstract int GetDigestLength ();
|
||||
public static MessageDigest GetInstance (string algorithm)
|
||||
{
|
||||
switch (algorithm.ToLower ()) {
|
||||
case "sha-1":
|
||||
//System.Security.CryptographySHA1Managed not found
|
||||
//return new MessageDigest<SHA1Managed> ();
|
||||
return new MessageDigest<System.Security.Cryptography.SHA1>();
|
||||
case "md5":
|
||||
return new MessageDigest<Md5Managed> ();
|
||||
}
|
||||
throw new NotSupportedException (string.Format ("The requested algorithm \"{0}\" is not supported.", algorithm));
|
||||
}
|
||||
|
||||
public abstract void Reset ();
|
||||
public abstract void Update (byte[] b);
|
||||
public abstract void Update (byte b);
|
||||
public abstract void Update (byte[] b, int offset, int len);
|
||||
}
|
||||
|
||||
|
||||
public class MessageDigest<TAlgorithm> : MessageDigest where TAlgorithm : HashAlgorithm //, new() //use static `Create` method
|
||||
{
|
||||
private TAlgorithm _hash;
|
||||
//private CryptoStream _stream; //don't work .NET Core
|
||||
private MemoryStream _stream;
|
||||
|
||||
|
||||
public MessageDigest ()
|
||||
{
|
||||
Init ();
|
||||
}
|
||||
|
||||
public override byte[] Digest ()
|
||||
{
|
||||
//CryptoStream -> MemoryStream, needless method
|
||||
//_stream.FlushFinalBlock ();
|
||||
|
||||
//HashAlgorithm.`Hash` property deleted
|
||||
//byte[] hash = _hash.Hash;
|
||||
byte[] hash = _hash.ComputeHash(_stream.ToArray());
|
||||
|
||||
Reset ();
|
||||
return hash;
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
if (_stream != null) {
|
||||
_stream.Dispose ();
|
||||
}
|
||||
_stream = null;
|
||||
}
|
||||
|
||||
public override int GetDigestLength ()
|
||||
{
|
||||
return (_hash.HashSize / 8);
|
||||
}
|
||||
|
||||
private void Init ()
|
||||
{
|
||||
//use static `Create` method
|
||||
//_hash = Activator.CreateInstance<TAlgorithm> ();
|
||||
var createMethod = typeof(TAlgorithm).GetRuntimeMethod("Create", new Type[0]);
|
||||
_hash = (TAlgorithm)createMethod.Invoke(null, new object[] {});
|
||||
|
||||
//HashAlgorithm cannot cast `ICryptoTransform` on .NET Core, gave up using CryptoStream.
|
||||
//_stream = new CryptoStream(Stream.Null, _hash, CryptoStreamMode.Write);
|
||||
//_stream = new CryptoStream(_tmpStream, (ICryptoTransform)_hash, CryptoStreamMode.Write);
|
||||
_stream = new MemoryStream();
|
||||
}
|
||||
|
||||
public override void Reset ()
|
||||
{
|
||||
Dispose ();
|
||||
Init ();
|
||||
}
|
||||
|
||||
public override void Update (byte[] input)
|
||||
{
|
||||
_stream.Write (input, 0, input.Length);
|
||||
}
|
||||
|
||||
public override void Update (byte input)
|
||||
{
|
||||
_stream.WriteByte (input);
|
||||
}
|
||||
|
||||
public override void Update (byte[] input, int index, int count)
|
||||
{
|
||||
if (count < 0)
|
||||
Console.WriteLine ("Argh!");
|
||||
_stream.Write (input, index, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class NetworkStream : Stream
|
||||
{
|
||||
SocketEx _socket;
|
||||
|
||||
public NetworkStream(SocketEx socket)
|
||||
{
|
||||
_socket = socket;
|
||||
}
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override bool CanSeek
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
// throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override long Length
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
set
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
return _socket.Receive(buffer, offset, count);
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
_socket.Send(buffer, offset, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ObjectInputStream : InputStream
|
||||
{
|
||||
private BinaryReader _reader;
|
||||
|
||||
public ObjectInputStream (InputStream s)
|
||||
{
|
||||
_reader = new BinaryReader (s.GetWrappedStream ());
|
||||
}
|
||||
|
||||
public int ReadInt ()
|
||||
{
|
||||
return _reader.ReadInt32 ();
|
||||
}
|
||||
|
||||
public object ReadObject ()
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ObjectOutputStream : OutputStream
|
||||
{
|
||||
private BinaryWriter _bw;
|
||||
|
||||
public ObjectOutputStream (OutputStream os)
|
||||
{
|
||||
_bw = new BinaryWriter (os.GetWrappedStream ());
|
||||
}
|
||||
|
||||
public virtual void WriteInt (int i)
|
||||
{
|
||||
_bw.Write (i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class OutputStream : IDisposable
|
||||
{
|
||||
protected Stream Wrapped;
|
||||
|
||||
public static implicit operator OutputStream (Stream s)
|
||||
{
|
||||
return Wrap (s);
|
||||
}
|
||||
|
||||
public static implicit operator Stream (OutputStream s)
|
||||
{
|
||||
return s.GetWrappedStream ();
|
||||
}
|
||||
|
||||
public virtual void Close ()
|
||||
{
|
||||
if (Wrapped != null) {
|
||||
//Stream.`Close` method deleted
|
||||
//Wrapped.Close ();
|
||||
Wrapped.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
Close ();
|
||||
}
|
||||
|
||||
public virtual void Flush ()
|
||||
{
|
||||
if (Wrapped != null) {
|
||||
Wrapped.Flush ();
|
||||
}
|
||||
}
|
||||
|
||||
internal Stream GetWrappedStream ()
|
||||
{
|
||||
// Always create a wrapper stream (not directly Wrapped) since the subclass
|
||||
// may be overriding methods that need to be called when used through the Stream class
|
||||
return new WrappedSystemStream (this);
|
||||
}
|
||||
|
||||
static internal OutputStream Wrap (Stream s)
|
||||
{
|
||||
OutputStream stream = new OutputStream ();
|
||||
stream.Wrapped = s;
|
||||
return stream;
|
||||
}
|
||||
|
||||
public virtual void Write (int b)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
((WrappedSystemStream)Wrapped).OutputStream.Write (b);
|
||||
else {
|
||||
if (Wrapped == null)
|
||||
throw new NotImplementedException ();
|
||||
Wrapped.WriteByte ((byte)b);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Write (byte[] b)
|
||||
{
|
||||
Write (b, 0, b.Length);
|
||||
}
|
||||
|
||||
public virtual void Write (byte[] b, int offset, int len)
|
||||
{
|
||||
if (Wrapped is WrappedSystemStream)
|
||||
((WrappedSystemStream)Wrapped).OutputStream.Write (b, offset, len);
|
||||
else {
|
||||
if (Wrapped != null) {
|
||||
Wrapped.Write (b, offset, len);
|
||||
} else {
|
||||
for (int i = 0; i < len; i++) {
|
||||
Write (b[i + offset]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class OutputStreamWriter : StreamWriter
|
||||
{
|
||||
public OutputStreamWriter (OutputStream stream) : base(stream.GetWrappedStream ())
|
||||
{
|
||||
}
|
||||
|
||||
public OutputStreamWriter (OutputStream stream, string encoding) : base(stream.GetWrappedStream (), Extensions.GetEncoding (encoding))
|
||||
{
|
||||
}
|
||||
|
||||
public OutputStreamWriter (OutputStream stream, Encoding encoding) : base(stream.GetWrappedStream (), encoding)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class PipedInputStream : InputStream
|
||||
{
|
||||
private byte[] _oneBuffer;
|
||||
public const int PipeSize = 1024;
|
||||
|
||||
protected byte[] Buffer;
|
||||
private bool _closed;
|
||||
private ManualResetEvent _dataEvent;
|
||||
private int _end;
|
||||
private int _start;
|
||||
private object _thisLock;
|
||||
private bool _allowGrow = false;
|
||||
|
||||
public int In {
|
||||
get { return _start; }
|
||||
set { _start = value; }
|
||||
}
|
||||
|
||||
public int Out {
|
||||
get { return _end; }
|
||||
set { _end = value; }
|
||||
}
|
||||
|
||||
public PipedInputStream ()
|
||||
{
|
||||
_thisLock = new object ();
|
||||
_dataEvent = new ManualResetEvent (false);
|
||||
Buffer = new byte[PipeSize + 1];
|
||||
}
|
||||
|
||||
public PipedInputStream (PipedOutputStream os): this ()
|
||||
{
|
||||
os.Attach (this);
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
lock (_thisLock) {
|
||||
_closed = true;
|
||||
_dataEvent.Set ();
|
||||
}
|
||||
}
|
||||
|
||||
public override int Available ()
|
||||
{
|
||||
lock (_thisLock) {
|
||||
if (_start <= _end) {
|
||||
return (_end - _start);
|
||||
}
|
||||
return ((Buffer.Length - _start) + _end);
|
||||
}
|
||||
}
|
||||
|
||||
public override int Read ()
|
||||
{
|
||||
if (_oneBuffer == null)
|
||||
_oneBuffer = new byte[1];
|
||||
if (Read (_oneBuffer, 0, 1) == -1)
|
||||
return -1;
|
||||
return _oneBuffer[0];
|
||||
}
|
||||
|
||||
public override int Read (byte[] b, int offset, int len)
|
||||
{
|
||||
int length = 0;
|
||||
do {
|
||||
_dataEvent.WaitOne ();
|
||||
lock (_thisLock) {
|
||||
if (_closed && Available () == 0) {
|
||||
return -1;
|
||||
}
|
||||
if (_start < _end) {
|
||||
length = Math.Min (len, _end - _start);
|
||||
Array.Copy (Buffer, _start, b, offset, length);
|
||||
_start += length;
|
||||
} else if (_start > _end) {
|
||||
length = Math.Min (len, Buffer.Length - _start);
|
||||
Array.Copy (Buffer, _start, b, offset, length);
|
||||
len -= length;
|
||||
_start = (_start + length) % Buffer.Length;
|
||||
if (len > 0) {
|
||||
int i = Math.Min (len, _end);
|
||||
Array.Copy (Buffer, 0, b, offset + length, i);
|
||||
_start += i;
|
||||
length += i;
|
||||
}
|
||||
}
|
||||
if (_start == _end && !_closed) {
|
||||
_dataEvent.Reset ();
|
||||
}
|
||||
Monitor.PulseAll (_thisLock);
|
||||
}
|
||||
} while (length == 0);
|
||||
return length;
|
||||
}
|
||||
|
||||
private int Allocate (int len)
|
||||
{
|
||||
int alen;
|
||||
while ((alen = TryAllocate (len)) == 0) {
|
||||
// Wait until somebody reads data
|
||||
try {
|
||||
Monitor.Wait (_thisLock);
|
||||
} catch {
|
||||
_closed = true;
|
||||
_dataEvent.Set ();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return alen;
|
||||
}
|
||||
|
||||
int TryAllocate (int len)
|
||||
{
|
||||
int free;
|
||||
if (_start <= _end) {
|
||||
free = (Buffer.Length - _end) + _start;
|
||||
} else {
|
||||
free = _start - _end;
|
||||
}
|
||||
if (free <= len) {
|
||||
if (!_allowGrow)
|
||||
return free > 0 ? free - 1 : 0;
|
||||
int sizeInc = (len - free) + 1;
|
||||
byte[] destinationArray = new byte[Buffer.Length + sizeInc];
|
||||
if (_start <= _end) {
|
||||
Array.Copy (Buffer, _start, destinationArray, _start, _end - _start);
|
||||
} else {
|
||||
Array.Copy (Buffer, 0, destinationArray, 0, _end);
|
||||
Array.Copy (Buffer, _start, destinationArray, _start + sizeInc, Buffer.Length - _start);
|
||||
_start += sizeInc;
|
||||
}
|
||||
Buffer = destinationArray;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
internal void Write (int b)
|
||||
{
|
||||
lock (_thisLock) {
|
||||
Allocate (1);
|
||||
Buffer[_end] = (byte)b;
|
||||
_end = (_end + 1) % Buffer.Length;
|
||||
_dataEvent.Set ();
|
||||
}
|
||||
}
|
||||
|
||||
internal void Write (byte[] b, int offset, int len)
|
||||
{
|
||||
do {
|
||||
lock (_thisLock) {
|
||||
int alen = Allocate (len);
|
||||
int length = Math.Min (Buffer.Length - _end, alen);
|
||||
Array.Copy (b, offset, Buffer, _end, length);
|
||||
_end = (_end + length) % Buffer.Length;
|
||||
if (length < alen) {
|
||||
Array.Copy (b, offset + length, Buffer, 0, alen - length);
|
||||
_end += alen - length;
|
||||
}
|
||||
_dataEvent.Set ();
|
||||
len -= alen;
|
||||
offset += alen;
|
||||
}
|
||||
} while (len > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class PipedOutputStream : OutputStream
|
||||
{
|
||||
PipedInputStream _ips;
|
||||
|
||||
public PipedOutputStream ()
|
||||
{
|
||||
}
|
||||
|
||||
public PipedOutputStream (PipedInputStream iss) : this()
|
||||
{
|
||||
Attach (iss);
|
||||
}
|
||||
|
||||
public override void Close ()
|
||||
{
|
||||
_ips.Close ();
|
||||
base.Close ();
|
||||
}
|
||||
|
||||
internal void Attach (PipedInputStream iss)
|
||||
{
|
||||
_ips = iss;
|
||||
}
|
||||
|
||||
public override void Write (int b)
|
||||
{
|
||||
_ips.Write (b);
|
||||
}
|
||||
|
||||
public override void Write (byte[] b, int offset, int len)
|
||||
{
|
||||
_ips.Write (b, offset, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class PrintWriter : TextWriter
|
||||
{
|
||||
TextWriter _writer;
|
||||
private FileStream _stream;
|
||||
|
||||
public PrintWriter (FilePath path)
|
||||
{
|
||||
//Stream(string path) constructor deleted
|
||||
_stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
|
||||
_writer = new StreamWriter (_stream);
|
||||
}
|
||||
|
||||
public PrintWriter (TextWriter other)
|
||||
{
|
||||
_writer = other;
|
||||
}
|
||||
|
||||
public override Encoding Encoding {
|
||||
get { return _writer.Encoding; }
|
||||
}
|
||||
|
||||
public void Close() // remove `override`
|
||||
{
|
||||
//Stream.`Close` method deleted
|
||||
//_writer.Close ();
|
||||
_writer.Dispose();
|
||||
_stream.Dispose();
|
||||
}
|
||||
|
||||
public override void Flush ()
|
||||
{
|
||||
_writer.Flush ();
|
||||
}
|
||||
|
||||
public override IFormatProvider FormatProvider {
|
||||
get {
|
||||
return _writer.FormatProvider;
|
||||
}
|
||||
}
|
||||
|
||||
public override string NewLine {
|
||||
get {
|
||||
return _writer.NewLine;
|
||||
}
|
||||
set {
|
||||
_writer.NewLine = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write (char[] buffer, int index, int count)
|
||||
{
|
||||
_writer.Write (buffer, index, count);
|
||||
}
|
||||
|
||||
public override void Write (char[] buffer)
|
||||
{
|
||||
_writer.Write (buffer);
|
||||
}
|
||||
|
||||
public void Write (string format, object arg0, object arg1, object arg2)
|
||||
{
|
||||
_writer.Write (format, arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
public override void Write (string format, object arg0, object arg1)
|
||||
{
|
||||
_writer.Write (format, arg0, arg1);
|
||||
}
|
||||
|
||||
public override void Write (string format, object arg0)
|
||||
{
|
||||
_writer.Write (format, arg0);
|
||||
}
|
||||
|
||||
public override void Write (string format, params object[] arg)
|
||||
{
|
||||
_writer.Write (format, arg);
|
||||
}
|
||||
|
||||
public override void WriteLine (char[] buffer, int index, int count)
|
||||
{
|
||||
_writer.WriteLine (buffer, index, count);
|
||||
}
|
||||
|
||||
public override void WriteLine (char[] buffer)
|
||||
{
|
||||
_writer.WriteLine (buffer);
|
||||
}
|
||||
|
||||
public void WriteLine (string format, object arg0, object arg1, object arg2)
|
||||
{
|
||||
_writer.WriteLine (format, arg0, arg1, arg2);
|
||||
}
|
||||
|
||||
public override void WriteLine (string format, object arg0, object arg1)
|
||||
{
|
||||
_writer.WriteLine (format, arg0, arg1);
|
||||
}
|
||||
|
||||
public override void WriteLine (string format, object arg0)
|
||||
{
|
||||
_writer.WriteLine (format, arg0);
|
||||
}
|
||||
|
||||
public override void WriteLine (string format, params object[] arg)
|
||||
{
|
||||
_writer.WriteLine (format, arg);
|
||||
}
|
||||
|
||||
public override void WriteLine (ulong value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (uint value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (string value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (float value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (object value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (long value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (int value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (double value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (decimal value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (char value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine (bool value)
|
||||
{
|
||||
_writer.WriteLine (value);
|
||||
}
|
||||
|
||||
public override void WriteLine ()
|
||||
{
|
||||
_writer.WriteLine ();
|
||||
}
|
||||
|
||||
public override void Write (bool value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (char value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (decimal value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (double value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (int value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (long value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (object value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (float value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (string value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (uint value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
|
||||
public override void Write (ulong value)
|
||||
{
|
||||
_writer.Write (value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Properties
|
||||
{
|
||||
protected Hashtable _properties;
|
||||
|
||||
public Properties()
|
||||
{
|
||||
_properties = new Hashtable();
|
||||
}
|
||||
|
||||
public Properties(Properties defaultProp): this()
|
||||
{
|
||||
PutAll(defaultProp._properties);
|
||||
}
|
||||
|
||||
public void PutAll(Hashtable properties)
|
||||
{
|
||||
foreach (var key in properties.Keys)
|
||||
{
|
||||
//_properties.Add(key, properties[key]);
|
||||
_properties.Put(key, properties[key]);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetProperty(object key, object value)
|
||||
{
|
||||
//_properties.Add(key, value);
|
||||
_properties.Put(key, value);
|
||||
}
|
||||
|
||||
public object GetProperty(object key)
|
||||
{
|
||||
return _properties.Keys.Contains(key) ? _properties[key] : null;
|
||||
}
|
||||
|
||||
public object GetProperty(object key, object def)
|
||||
{
|
||||
/*if (_properties.ContainsKey(key))
|
||||
{
|
||||
return _properties[key];
|
||||
}
|
||||
return def;*/
|
||||
object value = _properties.Get(key);
|
||||
|
||||
return value ?? def;
|
||||
}
|
||||
|
||||
public void Load(InputStream input)
|
||||
{
|
||||
StreamReader sr = new StreamReader(input);
|
||||
while (!sr.EndOfStream)
|
||||
{
|
||||
string line = sr.ReadLine();
|
||||
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
{
|
||||
string[] tokens = line.Split('=');
|
||||
//_properties.Add(tokens[0], tokens[1]);
|
||||
_properties.Put(tokens[0], tokens[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Store(OutputStream output)
|
||||
{
|
||||
StreamWriter sw = new StreamWriter(output);
|
||||
foreach (var key in _properties.Keys)
|
||||
{
|
||||
string line = string.Format("{0}={1}", key, _properties[key]);
|
||||
sw.WriteLine(line);
|
||||
}
|
||||
}
|
||||
|
||||
public void Store(TextWriter output)
|
||||
{
|
||||
foreach (var key in _properties.Keys)
|
||||
{
|
||||
string line = string.Format("{0}={1}", key, _properties[key]);
|
||||
output.WriteLine(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class RandomAccessFile
|
||||
{
|
||||
private FileStream _stream;
|
||||
|
||||
public RandomAccessFile (FilePath file, string mode) : this(file.GetPath (), mode)
|
||||
{
|
||||
}
|
||||
|
||||
public RandomAccessFile (string file, string mode)
|
||||
{
|
||||
if (mode.IndexOf ('w') != -1)
|
||||
_stream = new FileStream (file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
||||
else
|
||||
_stream = new FileStream (file, FileMode.Open, FileAccess.Read);
|
||||
}
|
||||
|
||||
public void Close ()
|
||||
{
|
||||
//Stream.`Close` method deleted
|
||||
//_stream.Close ();
|
||||
_stream.Dispose();
|
||||
}
|
||||
|
||||
public long GetFilePointer ()
|
||||
{
|
||||
return _stream.Position;
|
||||
}
|
||||
|
||||
public long Length ()
|
||||
{
|
||||
return _stream.Length;
|
||||
}
|
||||
|
||||
public int Read (byte[] buffer)
|
||||
{
|
||||
int r = _stream.Read (buffer, 0, buffer.Length);
|
||||
return r > 0 ? r : -1;
|
||||
}
|
||||
|
||||
public int Read (byte[] buffer, int start, int size)
|
||||
{
|
||||
return _stream.Read (buffer, start, size);
|
||||
}
|
||||
|
||||
public void ReadFully (byte[] buffer, int start, int size)
|
||||
{
|
||||
while (size > 0) {
|
||||
int num = _stream.Read (buffer, start, size);
|
||||
if (num == 0) {
|
||||
throw new EofException ();
|
||||
}
|
||||
size -= num;
|
||||
start += num;
|
||||
}
|
||||
}
|
||||
|
||||
public void Seek (long pos)
|
||||
{
|
||||
_stream.Position = pos;
|
||||
}
|
||||
|
||||
public void SetLength (long len)
|
||||
{
|
||||
_stream.SetLength (len);
|
||||
}
|
||||
|
||||
public void Write (int value)
|
||||
{
|
||||
_stream.Write (BitConverter.GetBytes (value), 0, 4);
|
||||
}
|
||||
|
||||
public void Write (byte[] buffer)
|
||||
{
|
||||
_stream.Write (buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public void Write (byte[] buffer, int start, int size)
|
||||
{
|
||||
_stream.Write (buffer, start, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ReentrantLock
|
||||
{
|
||||
public void Lock ()
|
||||
{
|
||||
Monitor.Enter (this);
|
||||
}
|
||||
|
||||
public bool TryLock ()
|
||||
{
|
||||
return Monitor.TryEnter (this);
|
||||
}
|
||||
|
||||
public void Unlock ()
|
||||
{
|
||||
Monitor.Exit (this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal abstract class Reference<T>
|
||||
{
|
||||
public abstract T Get ();
|
||||
}
|
||||
}
|
||||
212
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/Runtime.cs
Normal file
212
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/Runtime.cs
Normal file
@@ -0,0 +1,212 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Runtime
|
||||
{
|
||||
private static Runtime _instance;
|
||||
private List<ShutdownHook> _shutdownHooks = new List<ShutdownHook> ();
|
||||
|
||||
internal void AddShutdownHook (IRunnable r)
|
||||
{
|
||||
ShutdownHook item = new ShutdownHook ();
|
||||
item.Runnable = r;
|
||||
_shutdownHooks.Add (item);
|
||||
}
|
||||
|
||||
internal int AvailableProcessors ()
|
||||
{
|
||||
return Environment.ProcessorCount;
|
||||
}
|
||||
|
||||
public static long CurrentTimeMillis ()
|
||||
{
|
||||
return DateTime.UtcNow.ToMillisecondsSinceEpoch ();
|
||||
}
|
||||
|
||||
static Hashtable _properties;
|
||||
|
||||
public static Hashtable GetProperties ()
|
||||
{
|
||||
if (_properties == null) {
|
||||
_properties = new Hashtable ();
|
||||
_properties ["jgit.fs.debug"] = "false";
|
||||
_properties["file.encoding"] = "UTF-8";
|
||||
if (Path.DirectorySeparatorChar != '\\')
|
||||
_properties ["os.name"] = "Unix";
|
||||
else
|
||||
_properties ["os.name"] = "Windows";
|
||||
}
|
||||
return _properties;
|
||||
}
|
||||
|
||||
public static string GetProperty (string key)
|
||||
{
|
||||
if (GetProperties().Keys.Contains(key))
|
||||
{
|
||||
return ((string)GetProperties()[key]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void SetProperty (string key, string value)
|
||||
{
|
||||
GetProperties () [key] = value;
|
||||
}
|
||||
|
||||
public static Runtime GetRuntime ()
|
||||
{
|
||||
if (_instance == null) {
|
||||
_instance = new Runtime ();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
public static int IdentityHashCode (object ob)
|
||||
{
|
||||
return RuntimeHelpers.GetHashCode (ob);
|
||||
}
|
||||
|
||||
internal long MaxMemory ()
|
||||
{
|
||||
return int.MaxValue;
|
||||
}
|
||||
|
||||
private class ShutdownHook
|
||||
{
|
||||
public IRunnable Runnable;
|
||||
|
||||
~ShutdownHook ()
|
||||
{
|
||||
Runnable.Run ();
|
||||
}
|
||||
}
|
||||
|
||||
public static void DeleteCharAt (StringBuilder sb, int index)
|
||||
{
|
||||
sb.Remove (index, 1);
|
||||
}
|
||||
|
||||
public static byte[] GetBytesForString (string str)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes (str);
|
||||
}
|
||||
|
||||
public static byte[] GetBytesForString (string str, string encoding)
|
||||
{
|
||||
return Encoding.GetEncoding (encoding).GetBytes (str);
|
||||
}
|
||||
|
||||
public static FieldInfo[] GetDeclaredFields (Type t)
|
||||
{
|
||||
throw new NotImplementedException("Type.GetFields not found on .NetStandard");
|
||||
//return t.GetFields (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
||||
}
|
||||
|
||||
public static void NotifyAll (object ob)
|
||||
{
|
||||
Monitor.PulseAll (ob);
|
||||
}
|
||||
|
||||
public static void Notify(object obj)
|
||||
{
|
||||
Monitor.Pulse(obj);
|
||||
}
|
||||
|
||||
public static void PrintStackTrace (Exception ex)
|
||||
{
|
||||
Console.WriteLine (ex);
|
||||
}
|
||||
|
||||
public static void PrintStackTrace (Exception ex, TextWriter tw)
|
||||
{
|
||||
tw.WriteLine (ex);
|
||||
}
|
||||
|
||||
public static string Substring (string str, int index)
|
||||
{
|
||||
return str.Substring (index);
|
||||
}
|
||||
|
||||
public static string Substring (string str, int index, int endIndex)
|
||||
{
|
||||
return str.Substring (index, endIndex - index);
|
||||
}
|
||||
|
||||
public static void Wait (object ob)
|
||||
{
|
||||
Monitor.Wait (ob);
|
||||
}
|
||||
|
||||
public static bool Wait (object ob, long milis)
|
||||
{
|
||||
return Monitor.Wait (ob, (int)milis);
|
||||
}
|
||||
|
||||
public static Type GetType (string name)
|
||||
{
|
||||
throw new NotImplementedException("AppDomain.CurrentDomain.GetAssemblies not found on .NetStandard");
|
||||
//foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
|
||||
// Type t = a.GetType (name);
|
||||
// if (t != null)
|
||||
// return t;
|
||||
//}
|
||||
//never used
|
||||
//throw new InvalidOperationException ("Type not found: " + name);
|
||||
}
|
||||
|
||||
public static void SetCharAt (StringBuilder sb, int index, char c)
|
||||
{
|
||||
sb [index] = c;
|
||||
}
|
||||
|
||||
public static bool EqualsIgnoreCase (string s1, string s2)
|
||||
{
|
||||
return s1.Equals (s2, StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
|
||||
internal static long NanoTime ()
|
||||
{
|
||||
return Environment.TickCount * 1000 * 1000;
|
||||
}
|
||||
|
||||
internal static int CompareOrdinal (string s1, string s2)
|
||||
{
|
||||
return string.CompareOrdinal (s1, s2);
|
||||
}
|
||||
|
||||
public static string GetStringForBytes (byte[] chars)
|
||||
{
|
||||
return Encoding.UTF8.GetString (chars, 0, chars.Length);
|
||||
}
|
||||
|
||||
public static string GetStringForBytes (byte[] chars, string encoding)
|
||||
{
|
||||
return GetEncoding (encoding).GetString (chars, 0, chars.Length);
|
||||
}
|
||||
|
||||
public static string GetStringForBytes (byte[] chars, int start, int len)
|
||||
{
|
||||
return Encoding.UTF8.GetString (chars, start, len);
|
||||
}
|
||||
|
||||
public static string GetStringForBytes (byte[] chars, int start, int len, string encoding)
|
||||
{
|
||||
return GetEncoding (encoding).Decode (chars, start, len);
|
||||
}
|
||||
|
||||
public static Encoding GetEncoding (string name)
|
||||
{
|
||||
Encoding e = Encoding.GetEncoding (name.Replace ('_','-'));
|
||||
if (e is UTF8Encoding)
|
||||
return new UTF8Encoding (false, true);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class SimpleDateFormat : DateFormat
|
||||
{
|
||||
string _format;
|
||||
|
||||
CultureInfo Culture {
|
||||
get; set;
|
||||
}
|
||||
|
||||
bool Lenient {
|
||||
get; set;
|
||||
}
|
||||
|
||||
public SimpleDateFormat (): this ("g")
|
||||
{
|
||||
}
|
||||
|
||||
public SimpleDateFormat (string format): this (format, CultureInfo.CurrentCulture)
|
||||
{
|
||||
}
|
||||
|
||||
public SimpleDateFormat (string format, CultureInfo c)
|
||||
{
|
||||
Culture = c;
|
||||
this._format = format.Replace ("EEE", "ddd");
|
||||
this._format = this._format.Replace ("Z", "zzz");
|
||||
SetTimeZone (TimeZoneInfo.Local);
|
||||
}
|
||||
|
||||
public bool IsLenient ()
|
||||
{
|
||||
return Lenient;
|
||||
}
|
||||
|
||||
public void SetLenient (bool lenient)
|
||||
{
|
||||
Lenient = lenient;
|
||||
}
|
||||
|
||||
public override DateTime Parse (string value)
|
||||
{
|
||||
if (IsLenient ())
|
||||
return DateTime.Parse (value);
|
||||
return DateTime.ParseExact (value, _format, Culture);
|
||||
}
|
||||
|
||||
public override string Format (DateTime date)
|
||||
{
|
||||
date += GetTimeZone().BaseUtcOffset;
|
||||
return date.ToString (_format);
|
||||
}
|
||||
|
||||
public string Format (long date)
|
||||
{
|
||||
return Extensions.MillisToDateTimeOffset (date, (int)GetTimeZone ().BaseUtcOffset.TotalMinutes).DateTime.ToString (_format);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// SocketEx.cs implementation by J. Arturo <webmaster at komodosoft dot net>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class SocketEx : Socket
|
||||
{
|
||||
private int _soTimeOut = -1;
|
||||
|
||||
public int SoTimeOut
|
||||
{
|
||||
get
|
||||
{
|
||||
return _soTimeOut;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
_soTimeOut = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
_soTimeOut = -1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public SocketEx(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
|
||||
: base(addressFamily, socketType, protocolType)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Connect(IPEndPoint endPoint, int timeOut)
|
||||
{
|
||||
using (var evt = new ManualResetEventSlim(false))
|
||||
{
|
||||
using (var args = new SocketAsyncEventArgs
|
||||
{
|
||||
RemoteEndPoint = endPoint
|
||||
})
|
||||
{
|
||||
args.Completed += delegate
|
||||
{
|
||||
evt.Set();
|
||||
};
|
||||
|
||||
ConnectAsync(args);
|
||||
|
||||
if (!evt.Wait(timeOut))
|
||||
{
|
||||
CancelConnectAsync(args);
|
||||
throw new ConnectException("Can't connect to end point.");
|
||||
}
|
||||
if (args.SocketError != SocketError.Success)
|
||||
{
|
||||
throw new ConnectException("Can't connect to end point.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Bind2(EndPoint ep)
|
||||
{
|
||||
if (ep == null)
|
||||
Bind(new IPEndPoint(IPAddress.Any, 0));
|
||||
else
|
||||
Bind(ep);
|
||||
}
|
||||
|
||||
|
||||
public int Receive(byte[] buffer, int offset, int count)
|
||||
{
|
||||
using (var evt = new ManualResetEventSlim(false))
|
||||
{
|
||||
using (var args = new SocketAsyncEventArgs
|
||||
{
|
||||
UserToken = this
|
||||
})
|
||||
{
|
||||
args.SetBuffer(buffer, offset, count);
|
||||
|
||||
args.Completed += delegate
|
||||
{
|
||||
evt.Set();
|
||||
};
|
||||
|
||||
if (ReceiveAsync(args))
|
||||
{
|
||||
if (!evt.Wait(_soTimeOut))
|
||||
{
|
||||
throw new TimeoutException("No data received.");
|
||||
}
|
||||
}
|
||||
|
||||
return args.BytesTransferred;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Send(byte[] buffer, int offset, int length, EndPoint destination = null)
|
||||
{
|
||||
using (var evt = new ManualResetEventSlim(false))
|
||||
{
|
||||
using (SocketAsyncEventArgs args = new SocketAsyncEventArgs
|
||||
{
|
||||
UserToken = this
|
||||
})
|
||||
{
|
||||
args.SetBuffer(buffer, offset, length);
|
||||
|
||||
args.Completed += delegate
|
||||
{
|
||||
evt.Set();
|
||||
};
|
||||
|
||||
args.RemoteEndPoint = destination ?? RemoteEndPoint;
|
||||
|
||||
|
||||
SendToAsync(args);
|
||||
if (!evt.Wait(_soTimeOut))
|
||||
{
|
||||
throw new TimeoutException("No data sent.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream GetInputStream()
|
||||
{
|
||||
return new NetworkStream(this);
|
||||
}
|
||||
|
||||
public OutputStream GetOutputStream()
|
||||
{
|
||||
return new NetworkStream(this);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class StringTokenizer
|
||||
{
|
||||
private string[] _tokens;
|
||||
private int _pos;
|
||||
|
||||
public StringTokenizer(string text, string delim)
|
||||
{
|
||||
_tokens = text.Split(delim);
|
||||
}
|
||||
|
||||
public int CountTokens()
|
||||
{
|
||||
return _tokens.Length;
|
||||
}
|
||||
|
||||
public string NextToken()
|
||||
{
|
||||
string value = _tokens[_pos];
|
||||
|
||||
_pos++;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public bool HasMoreTokens()
|
||||
{
|
||||
return _pos < _tokens.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class SynchronizedList<T> : IList<T>
|
||||
{
|
||||
private IList<T> _list;
|
||||
|
||||
public SynchronizedList (IList<T> list)
|
||||
{
|
||||
this._list = list;
|
||||
}
|
||||
|
||||
public int IndexOf (T item)
|
||||
{
|
||||
lock (_list) {
|
||||
return _list.IndexOf (item);
|
||||
}
|
||||
}
|
||||
|
||||
public void Insert (int index, T item)
|
||||
{
|
||||
lock (_list) {
|
||||
_list.Insert (index, item);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveAt (int index)
|
||||
{
|
||||
lock (_list) {
|
||||
_list.RemoveAt (index);
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection<T>.Add (T item)
|
||||
{
|
||||
lock (_list) {
|
||||
_list.Add (item);
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection<T>.Clear ()
|
||||
{
|
||||
lock (_list) {
|
||||
_list.Clear ();
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.Contains (T item)
|
||||
{
|
||||
lock (_list) {
|
||||
return _list.Contains (item);
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection<T>.CopyTo (T[] array, int arrayIndex)
|
||||
{
|
||||
lock (_list) {
|
||||
_list.CopyTo (array, arrayIndex);
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.Remove (T item)
|
||||
{
|
||||
lock (_list) {
|
||||
return _list.Remove (item);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator<T> IEnumerable<T>.GetEnumerator ()
|
||||
{
|
||||
return _list.GetEnumerator ();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator ()
|
||||
{
|
||||
return _list.GetEnumerator ();
|
||||
}
|
||||
|
||||
public T this[int index] {
|
||||
get {
|
||||
lock (_list) {
|
||||
return _list[index];
|
||||
}
|
||||
}
|
||||
set {
|
||||
lock (_list) {
|
||||
_list[index] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ICollection<T>.Count {
|
||||
get {
|
||||
lock (_list) {
|
||||
return _list.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ICollection<T>.IsReadOnly {
|
||||
get { return _list.IsReadOnly; }
|
||||
}
|
||||
}
|
||||
}
|
||||
193
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/Thread.cs
Normal file
193
Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/Thread.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
public class Thread : IRunnable
|
||||
{
|
||||
private static ThreadGroup _defaultGroup = new ThreadGroup ();
|
||||
private bool _interrupted;
|
||||
private IRunnable _runnable;
|
||||
private ThreadGroup _tgroup;
|
||||
private System.Threading.Thread _thread;
|
||||
|
||||
[ThreadStatic]
|
||||
private static Thread _wrapperThread;
|
||||
|
||||
public Thread () : this(null, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
public Thread (string name) : this (null, null, name)
|
||||
{
|
||||
}
|
||||
|
||||
public Thread (ThreadGroup grp, string name) : this (null, grp, name)
|
||||
{
|
||||
}
|
||||
|
||||
public Thread (IRunnable runnable): this (runnable, null, null)
|
||||
{
|
||||
}
|
||||
|
||||
Thread (IRunnable runnable, ThreadGroup grp, string name)
|
||||
{
|
||||
_thread = new System.Threading.Thread (InternalRun);
|
||||
|
||||
this._runnable = runnable ?? this;
|
||||
_tgroup = grp ?? _defaultGroup;
|
||||
_tgroup.Add (this);
|
||||
if (name != null)
|
||||
_thread.Name = name;
|
||||
}
|
||||
|
||||
private Thread (System.Threading.Thread t)
|
||||
{
|
||||
_thread = t;
|
||||
_tgroup = _defaultGroup;
|
||||
_tgroup.Add (this);
|
||||
}
|
||||
|
||||
public static Thread CurrentThread ()
|
||||
{
|
||||
if (_wrapperThread == null) {
|
||||
_wrapperThread = new Thread (System.Threading.Thread.CurrentThread);
|
||||
}
|
||||
return _wrapperThread;
|
||||
}
|
||||
|
||||
public string GetName ()
|
||||
{
|
||||
return _thread.Name;
|
||||
}
|
||||
|
||||
public ThreadGroup GetThreadGroup ()
|
||||
{
|
||||
return _tgroup;
|
||||
}
|
||||
|
||||
private void InternalRun ()
|
||||
{
|
||||
_wrapperThread = this;
|
||||
try {
|
||||
_runnable.Run ();
|
||||
} catch (Exception exception) {
|
||||
Console.WriteLine (exception);
|
||||
} finally {
|
||||
_tgroup.Remove (this);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Yield ()
|
||||
{
|
||||
}
|
||||
|
||||
public void Interrupt ()
|
||||
{
|
||||
lock (_thread) {
|
||||
_interrupted = true;
|
||||
//thread.Interrupt ();
|
||||
|
||||
//TODO: implement CancellationToken
|
||||
//_thread.Abort();
|
||||
throw new NotImplementedException("implement CancellationToken for thread");
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Interrupted ()
|
||||
{
|
||||
if (Thread._wrapperThread == null) {
|
||||
return false;
|
||||
}
|
||||
Thread wrapperThread = Thread._wrapperThread;
|
||||
lock (wrapperThread) {
|
||||
bool interrupted = Thread._wrapperThread._interrupted;
|
||||
Thread._wrapperThread._interrupted = false;
|
||||
return interrupted;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsAlive ()
|
||||
{
|
||||
return _thread.IsAlive;
|
||||
}
|
||||
|
||||
public void Join ()
|
||||
{
|
||||
_thread.Join ();
|
||||
}
|
||||
|
||||
public void Join (long timeout)
|
||||
{
|
||||
_thread.Join ((int)timeout);
|
||||
}
|
||||
|
||||
public virtual void Run ()
|
||||
{
|
||||
}
|
||||
|
||||
public void SetDaemon (bool daemon)
|
||||
{
|
||||
_thread.IsBackground = daemon;
|
||||
}
|
||||
|
||||
public void SetName (string name)
|
||||
{
|
||||
_thread.Name = name;
|
||||
}
|
||||
|
||||
public static void Sleep (long milis)
|
||||
{
|
||||
System.Threading.Thread.Sleep ((int)milis);
|
||||
}
|
||||
|
||||
public void Start ()
|
||||
{
|
||||
_thread.Start ();
|
||||
}
|
||||
|
||||
public void Abort ()
|
||||
{
|
||||
//TODO: implement CancellationToken
|
||||
//_thread.Abort ();
|
||||
throw new NotImplementedException("implement CancellationToken for thread");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class ThreadGroup
|
||||
{
|
||||
private List<Thread> _threads = new List<Thread> ();
|
||||
|
||||
public ThreadGroup()
|
||||
{
|
||||
}
|
||||
|
||||
public ThreadGroup (string name)
|
||||
{
|
||||
}
|
||||
|
||||
internal void Add (Thread t)
|
||||
{
|
||||
lock (_threads) {
|
||||
_threads.Add (t);
|
||||
}
|
||||
}
|
||||
|
||||
internal void Remove (Thread t)
|
||||
{
|
||||
lock (_threads) {
|
||||
_threads.Remove (t);
|
||||
}
|
||||
}
|
||||
|
||||
public int Enumerate (Thread[] array)
|
||||
{
|
||||
lock (_threads) {
|
||||
int count = Math.Min (array.Length, _threads.Count);
|
||||
_threads.CopyTo (0, array, 0, count);
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class ThreadFactory
|
||||
{
|
||||
public Thread NewThread (IRunnable r)
|
||||
{
|
||||
Thread t = new Thread (r);
|
||||
t.SetDaemon (true);
|
||||
t.Start ();
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ST = System.Threading;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
class ThreadPoolExecutor
|
||||
{
|
||||
ThreadFactory _tf;
|
||||
int _corePoolSize;
|
||||
int _maxPoolSize;
|
||||
List<Thread> _pool = new List<Thread> ();
|
||||
int _runningThreads;
|
||||
int _freeThreads;
|
||||
bool _shutdown;
|
||||
Queue<IRunnable> _pendingTasks = new Queue<IRunnable> ();
|
||||
|
||||
public ThreadPoolExecutor (int corePoolSize, ThreadFactory factory)
|
||||
{
|
||||
this._corePoolSize = corePoolSize;
|
||||
_maxPoolSize = corePoolSize;
|
||||
_tf = factory;
|
||||
}
|
||||
|
||||
public void SetMaximumPoolSize (int size)
|
||||
{
|
||||
_maxPoolSize = size;
|
||||
}
|
||||
|
||||
public bool IsShutdown ()
|
||||
{
|
||||
return _shutdown;
|
||||
}
|
||||
|
||||
public virtual bool IsTerminated ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
return _shutdown && _pendingTasks.Count == 0;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsTerminating ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
return _shutdown && !IsTerminated ();
|
||||
}
|
||||
}
|
||||
|
||||
public int GetCorePoolSize ()
|
||||
{
|
||||
return _corePoolSize;
|
||||
}
|
||||
|
||||
public void PrestartAllCoreThreads ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
while (_runningThreads < _corePoolSize)
|
||||
StartPoolThread ();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetThreadFactory (ThreadFactory f)
|
||||
{
|
||||
_tf = f;
|
||||
}
|
||||
|
||||
public void Execute (IRunnable r)
|
||||
{
|
||||
InternalExecute (r, true);
|
||||
}
|
||||
|
||||
internal void InternalExecute (IRunnable r, bool checkShutdown)
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
if (_shutdown && checkShutdown)
|
||||
throw new InvalidOperationException ();
|
||||
if (_runningThreads < _corePoolSize) {
|
||||
StartPoolThread ();
|
||||
}
|
||||
else if (_freeThreads > 0) {
|
||||
_freeThreads--;
|
||||
}
|
||||
else if (_runningThreads < _maxPoolSize) {
|
||||
StartPoolThread ();
|
||||
}
|
||||
_pendingTasks.Enqueue (r);
|
||||
ST.Monitor.PulseAll (_pendingTasks);
|
||||
}
|
||||
}
|
||||
|
||||
void StartPoolThread ()
|
||||
{
|
||||
_runningThreads++;
|
||||
_pool.Add (_tf.NewThread (new RunnableAction (RunPoolThread)));
|
||||
}
|
||||
|
||||
public void RunPoolThread ()
|
||||
{
|
||||
while (!IsTerminated ()) {
|
||||
try {
|
||||
IRunnable r = null;
|
||||
lock (_pendingTasks) {
|
||||
_freeThreads++;
|
||||
while (!IsTerminated () && _pendingTasks.Count == 0)
|
||||
ST.Monitor.Wait (_pendingTasks);
|
||||
if (IsTerminated ())
|
||||
break;
|
||||
r = _pendingTasks.Dequeue ();
|
||||
}
|
||||
if (r != null)
|
||||
r.Run ();
|
||||
}
|
||||
//supress all errors, anyway
|
||||
//catch (ST.ThreadAbortException) {
|
||||
// // Do not catch a thread abort. If we've been aborted just let the thread die.
|
||||
// // Currently reseting an abort which was issued because the appdomain is being
|
||||
// // torn down results in the process living forever and consuming 100% cpu time.
|
||||
// return;
|
||||
//}
|
||||
catch {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Shutdown ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
_shutdown = true;
|
||||
ST.Monitor.PulseAll (_pendingTasks);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual List<IRunnable> ShutdownNow ()
|
||||
{
|
||||
lock (_pendingTasks) {
|
||||
_shutdown = true;
|
||||
foreach (var t in _pool) {
|
||||
try {
|
||||
t.Abort ();
|
||||
} catch {}
|
||||
}
|
||||
_pool.Clear ();
|
||||
_freeThreads = 0;
|
||||
_runningThreads = 0;
|
||||
var res = new List<IRunnable> (_pendingTasks);
|
||||
_pendingTasks.Clear ();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RunnableAction: IRunnable
|
||||
{
|
||||
Action _action;
|
||||
|
||||
public RunnableAction (Action a)
|
||||
{
|
||||
_action = a;
|
||||
}
|
||||
|
||||
public void Run ()
|
||||
{
|
||||
_action ();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace SharpCifs.Util.Sharpen
|
||||
{
|
||||
internal class WrappedSystemStream : Stream
|
||||
{
|
||||
private InputStream _ist;
|
||||
private OutputStream _ost;
|
||||
int _position;
|
||||
int _markedPosition;
|
||||
|
||||
public WrappedSystemStream (InputStream ist)
|
||||
{
|
||||
this._ist = ist;
|
||||
}
|
||||
|
||||
public WrappedSystemStream (OutputStream ost)
|
||||
{
|
||||
this._ost = ost;
|
||||
}
|
||||
|
||||
public InputStream InputStream {
|
||||
get { return _ist; }
|
||||
}
|
||||
|
||||
public OutputStream OutputStream {
|
||||
get { return _ost; }
|
||||
}
|
||||
|
||||
public void Close() //remove `override`
|
||||
{
|
||||
if (_ist != null) {
|
||||
//Stream.`Close` method deleted
|
||||
//_ist.Close ();
|
||||
_ist.Dispose();
|
||||
}
|
||||
if (_ost != null) {
|
||||
//Stream.`Close` method deleted
|
||||
//_ost.Close ();
|
||||
_ost.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush ()
|
||||
{
|
||||
_ost.Flush ();
|
||||
}
|
||||
|
||||
public override int Read (byte[] buffer, int offset, int count)
|
||||
{
|
||||
int res = _ist.Read (buffer, offset, count);
|
||||
if (res != -1) {
|
||||
_position += res;
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public override int ReadByte ()
|
||||
{
|
||||
int res = _ist.Read ();
|
||||
if (res != -1)
|
||||
_position++;
|
||||
return res;
|
||||
}
|
||||
|
||||
public override long Seek (long offset, SeekOrigin origin)
|
||||
{
|
||||
if (origin == SeekOrigin.Begin)
|
||||
Position = offset;
|
||||
else if (origin == SeekOrigin.Current)
|
||||
Position = Position + offset;
|
||||
else if (origin == SeekOrigin.End)
|
||||
Position = Length + offset;
|
||||
return Position;
|
||||
}
|
||||
|
||||
public override void SetLength (long value)
|
||||
{
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
|
||||
public override void Write (byte[] buffer, int offset, int count)
|
||||
{
|
||||
_ost.Write (buffer, offset, count);
|
||||
_position += count;
|
||||
}
|
||||
|
||||
public override void WriteByte (byte value)
|
||||
{
|
||||
_ost.Write (value);
|
||||
_position++;
|
||||
}
|
||||
|
||||
public override bool CanRead {
|
||||
get { return (_ist != null); }
|
||||
}
|
||||
|
||||
public override bool CanSeek {
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanWrite {
|
||||
get { return (_ost != null); }
|
||||
}
|
||||
|
||||
public override long Length {
|
||||
get { return _ist.Length; }
|
||||
}
|
||||
|
||||
internal void OnMark (int nb)
|
||||
{
|
||||
_markedPosition = _position;
|
||||
_ist.Mark (nb);
|
||||
}
|
||||
|
||||
public override long Position {
|
||||
get
|
||||
{
|
||||
if (_ist != null && _ist.CanSeek ())
|
||||
return _ist.Position;
|
||||
return _position;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == _position)
|
||||
return;
|
||||
if (value == _markedPosition)
|
||||
_ist.Reset ();
|
||||
else if (_ist != null && _ist.CanSeek ()) {
|
||||
_ist.Position = value;
|
||||
}
|
||||
else
|
||||
throw new NotSupportedException ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user