A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.IO;
6using System.Runtime.InteropServices;
7
8namespace osu.Framework.Audio.Callbacks
9{
10 /// <summary>
11 /// Implementation of <see cref="IFileProcedures"/> that supports reading from a <see cref="Stream"/>.
12 /// </summary>
13 public class DataStreamFileProcedures : IFileProcedures
14 {
15 private byte[] readBuffer = new byte[32768];
16
17 private readonly Stream dataStream;
18
19 public DataStreamFileProcedures(Stream data)
20 {
21 dataStream = data;
22 }
23
24 public void Close(IntPtr user)
25 {
26 }
27
28 public long Length(IntPtr user)
29 {
30 if (dataStream == null) return 0;
31
32 try
33 {
34 return dataStream.Length;
35 }
36 catch
37 {
38 }
39
40 return 0;
41 }
42
43 public int Read(IntPtr buffer, int length, IntPtr user)
44 {
45 if (dataStream == null) return 0;
46
47 try
48 {
49 if (length > readBuffer.Length)
50 readBuffer = new byte[length];
51
52 if (!dataStream.CanRead)
53 return 0;
54
55 int readBytes = dataStream.Read(readBuffer, 0, length);
56 Marshal.Copy(readBuffer, 0, buffer, readBytes);
57 return readBytes;
58 }
59 catch
60 {
61 }
62
63 return 0;
64 }
65
66 public bool Seek(long offset, IntPtr user)
67 {
68 if (dataStream == null) return false;
69
70 try
71 {
72 return dataStream.Seek(offset, SeekOrigin.Begin) == offset;
73 }
74 catch
75 {
76 }
77
78 return false;
79 }
80 }
81}