/// <summary> /// Returns the number of bytes that can be read from this input stream without /// blocking. /// </summary> /// <remarks> /// Returns the number of bytes that can be read from this input stream without /// blocking. The <code>available</code> method of <code>InputStream</code> /// returns <code>0</code>. This method <B>should</B> be overridden by /// subclasses. /// </remarks> /// <returns> /// the number of bytes that can be read from this input stream without /// blocking. /// </returns> /// <exception> /// IOException /// if an I/O error occurs. /// </exception> /// <exception cref="System.IO.IOException"/> public override int Available() { if (!useWrap) { return(inStream.Available()); } return(ofinish - ostart); }
private List <MidiTimestampPair> ReadMidiMessages(MidiCommandHeader midiCommandHeader, DataInputStream midiInputStream) { List <MidiTimestampPair> result = new List <MidiTimestampPair>(); int status = -1; ByteArrayOutputStream sysexDataStream = null; int deltaTimeSum = midiCommandHeader.RtpHeader.Timestamp; while (midiInputStream.Available() > 0) { int deltaTime; if (midiCommandHeader.Z) { deltaTime = ReadDeltaTime(midiInputStream); } else { deltaTime = 0; } deltaTimeSum += deltaTime; byte midiOctet1 = (byte)midiInputStream.ReadByte(); bool systemCommonMessage = (midiOctet1 & 0xF0) == 0xF0; int possibleStatus = midiOctet1 & 0xFF; if (systemCommonMessage) { if (midiOctet1 == (byte)SysexMessage.SYSTEM_EXCLUSIVE) { sysexDataStream = new ByteArrayOutputStream(); sysexDataStream.Write(midiOctet1); bool partial = ReadSysexData(midiInputStream, sysexDataStream); if (!partial) { result.Add(new MidiTimestampPair(deltaTimeSum, new SysexMessage(sysexDataStream.ToByteArray(), sysexDataStream.Size()))); } } else if (midiOctet1 == (byte)SysexMessage.SPECIAL_SYSTEM_EXCLUSIVE) { bool partial = ReadSysexData(midiInputStream, sysexDataStream); if (!partial) { result.Add(new MidiTimestampPair(deltaTimeSum, new SysexMessage(sysexDataStream.ToByteArray(), sysexDataStream.Size()))); } } else if (midiOctet1 == (byte)0xF4) { sysexDataStream.Reset(); } else { ShortMessage shortMessage = null; switch (possibleStatus) { case ShortMessage.TUNE_REQUEST: case ShortMessage.TIMING_CLOCK: case ShortMessage.START: case ShortMessage.STOP: case ShortMessage.ACTIVE_SENSING: case ShortMessage.SYSTEM_RESET: status = possibleStatus; shortMessage = new ShortMessage((byte)(status & 0xFF)); break; case ShortMessage.MIDI_TIME_CODE: case ShortMessage.SONG_SELECT: status = possibleStatus; shortMessage = new ShortMessage((byte)(status & 0xFF), (byte)midiInputStream.ReadByte()); break; case ShortMessage.SONG_POSITION_POINTER: status = possibleStatus; shortMessage = new ShortMessage((byte)(status & 0xFF), (byte)midiInputStream.ReadByte(), (byte)midiInputStream.ReadByte()); break; default: Log.Error("RtpMidi", "Invalid Message-status: {}", possibleStatus); break; } if (shortMessage != null) { result.Add(new MidiTimestampPair(deltaTimeSum, shortMessage)); } } } else { ShortMessage shortMessage = null; switch (midiOctet1 & 0xF0) { case ShortMessage.NOTE_OFF: case ShortMessage.NOTE_ON: case ShortMessage.POLY_PRESSURE: case ShortMessage.CONTROL_CHANGE: case ShortMessage.PITCH_BEND: status = possibleStatus; shortMessage = new ShortMessage((byte)(status & 0xFF), (byte)midiInputStream.ReadByte(), (byte)midiInputStream.ReadByte()); break; case ShortMessage.PROGRAM_CHANGE: case ShortMessage.CHANNEL_PRESSURE: status = possibleStatus; shortMessage = new ShortMessage((byte)(status & 0xFF), (byte)midiInputStream.ReadByte()); break; default: Log.Error("rtpMidi", "Invalid ShortMessage-status: {}", midiOctet1 & 0xF0); break; } if (shortMessage != null) { result.Add(new MidiTimestampPair(deltaTimeSum, shortMessage)); } } } return(result); }
static void LoadResToCache() { string loon_default_font = "lfcache"; IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication(); if (!isoStorage.FileExists(loon_default_font)) { IsolatedStorageFileStream outStream = isoStorage.CreateFile(loon_default_font); Stream gzip = null; try { gzip = new GZipInputStream(XNAConfig.LoadStream(LSystem.FRAMEWORK_IMG_NAME + "font.zip")); byte[] buffer = new byte[4096]; int charsRead; while ((charsRead = gzip.Read(buffer, 0, buffer.Length)) != 0) { outStream.Write(buffer, 0, charsRead); } outStream.Flush(); if (gzip != null) { gzip.Close(); gzip.Dispose(); gzip = null; } buffer = null; } catch (Exception) { if (outStream != null) { outStream.Close(); outStream.Dispose(); outStream = null; } if (gzip != null) { gzip.Close(); gzip.Dispose(); gzip = null; } IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(loon_default_font); } finally { if (outStream != null) { outStream.Close(); outStream = null; } } } Stream ins = null; try { ins = isoStorage.OpenFile(loon_default_font, FileMode.Open); DataInputStream resStream = new DataInputStream(ins); realsize = resStream.ReadUnsignedByte(); offy = resStream.ReadByte(); fontSpace = (realsize + offy) * 2; if (realsize > 0x10) { fontSpace *= 2; } int num = resStream.ReadInt(); int num2 = resStream.ReadByte(); byte[] bufferData = new byte[resStream.Available()]; resStream.Read(bufferData); fontData = bufferData; if (resStream != null) { resStream.Close(); resStream = null; } } catch (Exception) { fontData = null; if (ins != null) { ins.Close(); ins.Dispose(); ins = null; } LoadResToMemory(); } finally { if (ins != null) { ins.Close(); ins.Dispose(); ins = null; } } }