@Override
   public void save(final Graph inGraph) {
      final long startTime = System.currentTimeMillis();
      
      DataOutputStream dos = null;
      
      try {
         {
            final OutputStream outputStreamToUse;
            
            assert(this.filename != null || this.outputStream != null);
            if(this.outputStream != null) {
               outputStreamToUse = this.outputStream;
            } else {
               outputStreamToUse = new FileOutputStream(new File(this.filename));
            }
            
            dos = new DataOutputStream(outputStreamToUse);
         }
         
         final Dictionary dict = inGraph.getDictionary();

         final int numWords = dict.getNumUniqueWords();
         assert(numWords <= Character.MAX_VALUE);
         dos.writeChar(dict.getNumUniqueWords());
         
         for (char wordId = 0; wordId < numWords; wordId++) {
            dos.writeUTF(dict.getWord(wordId));
         }

         final String allWordHistories[] = inGraph.getWordHistories();
         dos.writeInt(allWordHistories.length);
         
         for (int historiesIndex = 0; historiesIndex < allWordHistories.length; historiesIndex++) {
            final String wordHistory = allWordHistories[historiesIndex];
            
            dos.writeUTF(wordHistory);
            
            final Transitions t = inGraph.getTransitions(wordHistory);
            
            final int numTransitions = t.getNumberTransitions();
            assert(numTransitions <= Character.MAX_VALUE);
            dos.writeChar(numTransitions);
             
            for(final TransitionProbability tp : t) {
               dos.writeChar(tp.targetWordId);
               dos.writeChar(tp.getNumerator());
               dos.writeChar(tp.getDenominator());
            }
         }

      } catch (final Exception anything) {
         anything.printStackTrace();
      } finally {
         if(dos != null) {
            try {
               dos.close();
            } catch(Throwable anything){}
         }
         System.out.println("Saving to "+filename+" took " + (System.currentTimeMillis() - startTime) + "ms");
      }
   }