DynamicByteProvider.cs :  » Development » Be.HexEditor » Be » Windows » Forms » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » Be.HexEditor 
Be.HexEditor » Be » Windows » Forms » DynamicByteProvider.cs
using System;

namespace Be.Windows.Forms{
  /// <summary>
  /// Byte provider for a small amount of data.
  /// </summary>
  public class DynamicByteProvider : IByteProvider
  {
    /// <summary>
    /// Contains information about changes.
    /// </summary>
    bool _hasChanges;
    /// <summary>
    /// Contains a byte collection.
    /// </summary>
    ByteCollection _bytes;

    /// <summary>
    /// Initializes a new instance of the DynamicByteProvider class.
    /// </summary>
    /// <param name="data"></param>
    public DynamicByteProvider(byte[] data) : this(new ByteCollection(data)) 
    {
    }

    /// <summary>
    /// Initializes a new instance of the DynamicByteProvider class.
    /// </summary>
    /// <param name="bytes"></param>
    public DynamicByteProvider(ByteCollection bytes)
    {
      _bytes = bytes;
    }

    /// <summary>
    /// Raises the Changed event.
    /// </summary>
    void OnChanged(EventArgs e)
    {
      _hasChanges = true;

      if(Changed != null)
        Changed(this, e);
    }

    /// <summary>
    /// Raises the LengthChanged event.
    /// </summary>
    void OnLengthChanged(EventArgs e)
    {
      if(LengthChanged != null)
        LengthChanged(this, e);
    }

    /// <summary>
    /// Gets the byte collection.
    /// </summary>
    public ByteCollection Bytes
    {
      get { return _bytes; }
    }

    #region IByteProvider Members
    /// <summary>
    /// True, when changes are done.
    /// </summary>
    public bool HasChanges()
    {
      return _hasChanges;
    }

    /// <summary>
    /// Applies changes.
    /// </summary>
    public void ApplyChanges()
    {
      _hasChanges = false;
    }

    /// <summary>
    /// Occurs, when the write buffer contains new changes.
    /// </summary>
    public event EventHandler Changed;

    /// <summary>
    /// Occurs, when InsertBytes or DeleteBytes method is called.
    /// </summary>
    public event EventHandler LengthChanged;


    /// <summary>
    /// Reads a byte from the byte collection.
    /// </summary>
    /// <param name="index">the index of the byte to read</param>
    /// <returns>the byte</returns>
    public byte ReadByte(long index)
    { return _bytes[(int)index]; }

    /// <summary>
    /// Write a byte into the byte collection.
    /// </summary>
    /// <param name="index">the index of the byte to write.</param>
    /// <param name="value">the byte</param>
    public void WriteByte(long index, byte value)
    {
      _bytes[(int)index] = value;
      OnChanged(EventArgs.Empty);
    }

    /// <summary>
    /// Deletes bytes from the byte collection.
    /// </summary>
    /// <param name="index">the start index of the bytes to delete.</param>
    /// <param name="length">the length of bytes to delete.</param>
    public void DeleteBytes(long index, long length)
    { 
      int internal_index = (int)Math.Max(0, index);
      int internal_length = (int)Math.Min((int)Length, length);
      _bytes.RemoveRange(internal_index, internal_length); 

      OnLengthChanged(EventArgs.Empty);
      OnChanged(EventArgs.Empty);
    }

    /// <summary>
    /// Inserts byte into the byte collection.
    /// </summary>
    /// <param name="index">the start index of the bytes in the byte collection</param>
    /// <param name="bs">the byte array to insert</param>
    public void InsertBytes(long index, byte[] bs)
    { 
      _bytes.InsertRange((int)index, bs); 

      OnLengthChanged(EventArgs.Empty);
      OnChanged(EventArgs.Empty);
    }

    /// <summary>
    /// Gets the length of the bytes in the byte collection.
    /// </summary>
    public long Length
    {
      get
      {
        return _bytes.Count;
      }
    }

    /// <summary>
    /// Returns true
    /// </summary>
    public bool SupportsWriteByte()
    {
      return true;
    }

    /// <summary>
    /// Returns true
    /// </summary>
    public bool SupportsInsertBytes()
    {
      return true;
    }

    /// <summary>
    /// Returns true
    /// </summary>
    public bool SupportsDeleteBytes()
    {
      return true;
    }
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.