Browse Source

Old Java code I wrote long, long, long ago.

master
Solomon Peachy 9 years ago
parent
commit
14758322e8
  1. 401
      java/JavaDecoder.java
  2. BIN
      java/dnsjava-1.2.3.tar.gz
  3. 168
      java/dnsjava-ixfr.diff

401
java/JavaDecoder.java

@ -0,0 +1,401 @@
/* JOrbisCodec -- JMF codec for the JOrbis project.
*
* Copyright (C) 2001 Solomon Peachy <pizza@shaftnet.org>
*
* JOrbisCodec depends on JOrbis.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.jcraft.media.codec.audio.vorbis;
import java.io.*;
import java.util.Vector;
import javax.media.*;
import javax.media.format.*;
import javax.media.bean.playerbean.MediaPlayer;
import com.jcraft.jorbis.*;
import com.jcraft.jogg.*;
public class JavaDecoder implements Codec {
public static final String ENCODING_VORBIS = "oggvorbis";
public static final int START = 0;
public static final int OK = 1;
public static final int FAILED = -1;
public static final int NEEDMORE = 2;
public static final int FINISHED = 3;
private int state = 0;
SyncState oy;
StreamState os;
Page og;
Packet op;
Info vi;
Comment vc;
DspState vd;
Block vb;
double[][][] _pcm;
float[][][] _pcmf;
int[] _index;
byte[] buffer=null;
int bytes=0;
InputStream bitStream = null;
// plugIn stuff
public JavaDecoder() {
//
}
public void open()
throws ResourceUnavailableException
{
oy=new SyncState();
os=new StreamState();
og=new Page();
op=new Packet();
vi=new Info();
vc=new Comment();
vd=new DspState();
vb=new Block(vd);
buffer=null;
bytes=0;
oy.init();
state = START;
}
public void close()
{
os.clear();
vb.clear();
vd.clear();
vi.clear();
oy.clear();
state = FINISHED;
}
public void reset()
{
try {
close();
open();
} catch (ResourceUnavailableException e) {
// ignore
}
}
public String getName()
{
return "JOrbis JMF codec";
}
// Control stuff
public Object[] getControls()
{
return new Object[0];
}
public Object getControl(java.lang.String controlType)
{
return null;
}
// Codec stuff.
protected Format inFormat;
protected Format outFormat;
public Format setInputFormat(Format format) {
inFormat = format;
return format;
}
public Format setOutputFormat(Format format) {
outFormat = format;
return format;
}
public Format[] getSupportedInputFormats() {
Format[] fs = new Format[1];
fs[0] = new AudioFormat(ENCODING_VORBIS, 44100.0, 16, 2);
return fs;
}
public Format[] getSupportedOutputFormats(Format f) {
Format[] fs = new Format[1];
fs[0] = new AudioFormat(AudioFormat.LINEAR, 44100.0, 16, 2);
return fs;
}
public int process(javax.media.Buffer input, javax.media.Buffer output)
{
Object inObject = input.getData();
Object outObject = output.getData();
if (outObject == null) {
outObject = new byte[0];
output.setData(outObject);
}
if (!(inObject instanceof byte[]) ||
!(outObject instanceof byte[]))
return PlugIn.BUFFER_PROCESSED_FAILED;
byte[] inData = (byte[]) inObject;
bitStream = new SequenceInputStream(bitStream, new ByteArrayInputStream(inData));
byte[] outData = (byte[]) outObject;
if (state == FINISHED) {
return PlugIn.PLUGIN_TERMINATED; // XXX need to check exit conditions.
}
if (state == START) {
int retval = FAILED;
try {
retval = init_feed();
} catch (IOException e) {
return PlugIn.BUFFER_PROCESSED_FAILED;
}
if (retval != OK)
return PlugIn.BUFFER_PROCESSED_FAILED;
state = 1;
output.setFormat(outFormat); // assert?
}
if (state == OK) { // normal running.
try {
outData = decode_main();
} catch (IOException e) {
throw new IllegalStateException("gak..");
}
if (outData == null)
return PlugIn.PLUGIN_TERMINATED;
output.setData(outData);
return BUFFER_PROCESSED_OK;
}
/*
BUFFER_PROCESSED_FAILED
BUFFER_PROCESSED_OK
INPUT_BUFFER_NOT_CONSUMED -- never.
OUTPUT_BUFFER_NOT_FILLED -- possible.
PLUGIN_TERMINATED -- on failure, we die.
*/
return PlugIn.BUFFER_PROCESSED_FAILED;
}
static final int BUFSIZE=4096*2;
public int init_feed()
throws IOException
{
int index=oy.buffer(BUFSIZE);
buffer=oy.data;
bytes=bitStream.read(buffer, index, BUFSIZE);
oy.wrote(bytes);
if (oy.pageout(og) != 1) {
if (bytes < BUFSIZE)
throw new EOFException("not enough data"); // XXXX
else
throw new IllegalArgumentException("Not Ogg bitstream");
}
os.init(og.serialno());
os.reset();
vi.init();
vc.init();
if(os.pagein(og) < 0)
return FAILED;
if(os.packetout(op) != 1)
return FAILED;
if(vi.synthesis_headerin(vc, op) < 0)
return FAILED;
// XXXX need to work this out.
/*
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, (float) vi.rate, 16, vi.channels, outFormat.getFrameSize(), AudioSystem.NOT_SPECIFIED, false);
if (!outFormat.matches(format)) {
System.err.println(format);
System.err.println(outFormat);
throw new IllegalArgumentException("Incompatible AudioFormat");
}
*/
int i = 0 ;
while(i<2){
while(i<2){
int result=oy.pageout(og);
if(result==0)
break; // Need more data
if(result==1){
os.pagein(og);
while(i<2){
result=os.packetout(op);
if(result==0)
break;
if(result==-1)
return FAILED;
}
vi.synthesis_headerin(vc, op);
i++;
}
}
index=oy.buffer(BUFSIZE);
buffer=oy.data;
bytes=bitStream.read(buffer, index, BUFSIZE);
if(bytes==0 && i<2)
return FAILED;
oy.wrote(bytes);
}
{
byte[][] ptr=vc.user_comments;
for(int j=0; j<ptr.length;j++){
if(ptr[j]==null) break;
System.err.println("Comment: "+new String(ptr[j], 0, ptr[j].length-1));
}
System.err.println("Bitstream is "+vi.channels+" channel, "+vi.rate+"Hz" );
System.err.println("Encoded by: "+new String(vc.vendor, 0, vc.vendor.length-1)+"\n");
}
vd.synthesis_init(vi);
vb.init(vd);
_pcm=new double[1][][];
_pcmf=new float[1][][];
_index=new int[vi.channels];
return OK;
}
public byte[] decode_main()
throws IOException
{
int i;
ByteArrayOutputStream bas = new ByteArrayOutputStream();
while (og.eos() == 0) {
int result=oy.pageout(og);
if (result == 0)
break;
if (result == -1) {
System.err.println("Corrupt or missing data in bitstream; continuing ...");
continue;
}
os.pagein(og);
while (true) {
result=os.packetout(op);
if(result == 0)
break;
if(result == -1)
continue;
int samples;
if(vb.synthesis(op) == 0)
vd.synthesis_blockin(vb);
while((samples=vd.synthesis_pcmout(_pcmf, _index))>0){
double[][] pcm=_pcm[0];
float[][] pcmf=_pcmf[0];
boolean clipflag=false;
for(i=0;i<vi.channels;i++){
int mono=_index[i];
for(int j=0;j<samples;j++){
int val=(int)(pcmf[i][mono+j]*32767.);
if(val>32767){
val=32767;
clipflag=true;
}
if(val<-32768){
val=-32768;
clipflag=true;
}
if(val<0) val=val|0x8000;
bas.write((byte)val);
bas.write((byte)val>>8);
}
}
vd.synthesis_read(samples);
}
}
int index = oy.buffer(BUFSIZE);
buffer = oy.data;
bytes = bitStream.read(buffer,index,BUFSIZE);
if (bytes == -1)
break;
if (bytes == 0)
break;
oy.wrote(bytes);
}
return bas.toByteArray();
}
public static void main(String argv[])
{
JavaDecoder x = new JavaDecoder();
if (PlugInManager.addPlugIn(x.getClass().getName(), x.getSupportedInputFormats(), x.getSupportedOutputFormats(null), PlugInManager.CODEC))
System.out.println("ok on codec");
if (com.sun.media.MimeManager.addMimeType("ogg", "audio/vorbis"))
System.out.println("ok on mime");
Vector v = javax.media.PackageManager.getProtocolPrefixList();
v.add("com.jcraft");
javax.media.PackageManager.setProtocolPrefixList(v);
v = javax.media.PackageManager.getContentPrefixList();
v.add("com.jcraft");
javax.media.PackageManager.setContentPrefixList(v);
// com.sun.media.JMFSecurityManager.DEBUG = true;
MediaPlayer xxx = new MediaPlayer();
xxx.setMediaLocation("/home/pizza/down/track01-128k.ogg");
xxx.prefetch();
String strMediaLocation = xxx.getMediaLocation();
if (strMediaLocation == null)
System.out.println("ok!");
else
System.out.println("ack.");
}
}

BIN
java/dnsjava-1.2.3.tar.gz

Binary file not shown.

168
java/dnsjava-ixfr.diff

@ -0,0 +1,168 @@
diff -aur dnsjava-1.2.3/org/xbill/DNS/Zone.java dnsjava-devel/org/xbill/DNS/Zone.java
--- dnsjava-1.2.3/org/xbill/DNS/Zone.java Wed Mar 14 20:49:55 2001
+++ dnsjava-devel/org/xbill/DNS/Zone.java Mon Feb 11 23:31:13 2002
@@ -15,6 +15,14 @@
public class Zone extends NameSet {
+ private static final int FIRSTSOA = 0;
+ private static final int SECONDSOA = 1;
+ private static final int ADD = 2;
+ private static final int DEL = 3;
+ private static final int LASTSOA = 4;
+ private static final int DONE = 5;
+ private static final int AXFR = 6;
+
class AXFREnumeration implements Enumeration {
private Enumeration znames;
private Name currentName;
@@ -173,6 +181,11 @@
Record rec = Record.newRecord(zone, Type.AXFR, dclass);
Message query = Message.newQuery(rec);
Message response = res.send(query);
+
+ if (response.getHeader().getRcode() != Rcode.NOERROR) {
+ // XXX uh, oh. we fux0red up. return an error!
+ }
+
Record [] recs = response.getSectionArray(Section.ANSWER);
for (int i = 0; i < recs.length; i++) {
if (!recs[i].getName().subdomain(origin)) {
@@ -191,6 +204,114 @@
validate();
}
+ /** Perform an IXFR (if possible) to update the zone.
+ @return false if unsuccessful, true if updated.
+ non-success may be because unsupported! */
+ public boolean updateZone(String remote, Cache cache)
+ throws IOException
+ {
+ Resolver res = new SimpleResolver(remote);
+ Record rec = Record.newRecord(origin, Type.IXFR, dclass);
+ Message query = Message.newQuery(rec);
+ query.addRecord(getSOA(), Section.AUTHORITY);
+ Message response = res.send(query);
+
+ if (response.getHeader().getRcode() == Rcode.NOTIMPL) {
+ // IXFR not supported. try again as an AXFR.
+ rec = Record.newRecord(origin, Type.AXFR, dclass);
+ query = Message.newQuery(rec);
+ response = res.send(query);
+ }
+
+ if (response.getHeader().getRcode() != Rcode.NOERROR)
+ return false; // uh, oh. we fux0red up. return an error!
+
+ // XXX what about other errors? permission, etc?
+
+ Record [] recs = response.getSectionArray(Section.ANSWER);
+
+ // check for UDP overflow.
+ if ((response.getHeader().getFlag(Flags.TC) == true) ||
+ ((recs.length == 1) && (recs[0].getType() == Type.SOA))) {
+ SOARecord r = (SOARecord) recs[0];
+ return (r.getSerial() <= getSOA().getSerial());
+ // if >, then it needs updating. otherwise we're okay.
+ }
+
+ SOARecord oldSOA = getSOA();
+ SOARecord newSOA = null;
+
+ int state = FIRSTSOA;
+
+ // we're getting a successful IXFR!
+ for (int i = 0; i < recs.length; i++) {
+ if (!recs[i].getName().subdomain(origin)) {
+ if (Options.check("verbose"))
+ System.err.println(recs[i].getName() +
+ "is not in zone " + origin);
+ continue;
+ }
+
+ // now for the state machine.
+ switch(state) {
+ case FIRSTSOA: {
+ newSOA = (SOARecord) recs[i];
+ state = SECONDSOA;
+ break;
+ }
+ case SECONDSOA: {
+ Record r = recs[i];
+ if (! (r instanceof SOARecord)) {
+ clear();
+ addRecord(r);
+ state = AXFR;
+ } else {
+ state = DEL;
+ deleteRecord(r);
+ }
+ }
+ case DEL: {
+ Record r = recs[i];
+ if (r instanceof SOARecord) {
+ state = ADD;
+ addRecord(r);
+ } else {
+ deleteRecord(r);
+ }
+ }
+ case ADD: {
+ Record r = recs[i];
+ if (r instanceof SOARecord) {
+ SOARecord x = (SOARecord) r;
+ if (x.getSerial() == newSOA.getSerial()) {
+ addRecord(r);
+ state = DONE;
+ } else {
+ state = DEL;
+ deleteRecord(r);
+ }
+ } else {
+ addRecord(r);
+ }
+ }
+ case AXFR: {
+ Record r = recs[i];
+ addRecord(r);
+ }
+ case DONE: {
+ // XXX wtF? nothing should get here.
+ }
+ } // end switch
+ }
+ if (cache != null) {
+ recs = response.getSectionArray(Section.ADDITIONAL);
+ for (int i = 0; i < recs.length; i++)
+ cache.addRecord(recs[i], Credibility.ZONE_GLUE, recs);
+ }
+ validate();
+ return true;
+}
+
/** Returns the Zone's origin */
public Name
getOrigin() {
@@ -326,6 +447,22 @@
rrset.addRR(r);
}
+/**
+ * Deletes a record from the Zone
+ * @param r The record to be deleted
+ * @see Record
+ */
+public void
+deleteRecord(Record r) {
+ Name name = r.getName();
+ short type = r.getRRsetType();
+ RRset rrset = (RRset) findExactSet (name, type);
+ if (rrset == null)
+ return;
+ else
+ rrset.deleteRR(r);
+}
+
public Enumeration
AXFR() {
return new AXFREnumeration();
Loading…
Cancel
Save