Source for javax.imageio.stream.ImageOutputStreamImpl

   1: /* ImageOutputStream.java --
   2:    Copyright (C) 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.imageio.stream;
  40: 
  41: import gnu.classpath.NotImplementedException;
  42: 
  43: import java.io.IOException;
  44: import java.io.UTFDataFormatException;
  45: import java.nio.ByteOrder;
  46: 
  47: /**
  48:  * @author Michael Koch (konqueror@gmx.de)
  49:  */
  50: public abstract class ImageOutputStreamImpl extends ImageInputStreamImpl
  51:   implements ImageOutputStream
  52: {
  53:   public ImageOutputStreamImpl()
  54:   {
  55:     // Do nothing here.
  56:   }
  57: 
  58:   protected final void flushBits()
  59:     throws IOException, NotImplementedException
  60:   {
  61:     // FIXME: Implement me.
  62:     throw new Error("not implemented");
  63:   }
  64: 
  65:   public void write(byte[] data)
  66:     throws IOException
  67:   {
  68:     write(data, 0, data.length);
  69:   }
  70: 
  71:   public abstract void write(byte[] data, int offset, int len)
  72:     throws IOException;
  73: 
  74:   public abstract void write(int value)
  75:     throws IOException;
  76: 
  77:   public void writeBit(int bit)
  78:     throws IOException, NotImplementedException
  79:   {
  80:     // FIXME: Implement me.
  81:     throw new Error("not implemented");
  82:   }
  83: 
  84:   public void writeBits(long bits, int numBits)
  85:     throws IOException, NotImplementedException
  86:   {
  87:     // FIXME: Implement me.
  88:     throw new Error("not implemented");
  89:   }
  90: 
  91:   public void writeBoolean(boolean value)
  92:     throws IOException
  93:   {
  94:     writeByte(value ? 1 : 0);
  95:   }
  96: 
  97:   public void writeByte(int value)
  98:     throws IOException
  99:   {
 100:     write(value & 0xff);
 101:   }
 102: 
 103:   public void writeBytes(String data)
 104:     throws IOException
 105:   {
 106:     // This is bogus, but it is how the method is specified.
 107:     // Sun ought to deprecate this method.
 108:     int len = data.length();
 109:     for (int i = 0; i < len; ++i)
 110:       writeByte(data.charAt(i));
 111:   }
 112: 
 113:   public void writeChar(int value)
 114:     throws IOException
 115:   {
 116:     writeShort(value);
 117:   }
 118: 
 119:   public void writeChars(char[] data, int offset, int len)
 120:     throws IOException
 121:   {
 122:     for(int i = 0; i < len; ++len)
 123:       writeChar(data[offset + i]);
 124:   }
 125: 
 126:   public void writeChars(String data)
 127:     throws IOException
 128:   {
 129:     int len = data.length();
 130:     for (int i = 0; i < len; ++i)
 131:       writeChar(data.charAt(i));
 132:   }
 133: 
 134:   public void writeDouble(double value)
 135:     throws IOException
 136:   {
 137:     writeLong(Double.doubleToLongBits(value));
 138:   }
 139: 
 140:   public void writeDoubles(double[] data, int offset, int len)
 141:     throws IOException
 142:   {
 143:     for(int i = 0; i < len; ++len)
 144:       writeDouble(data[offset + i]);
 145:   }
 146:   
 147:   public void writeFloat(float value)
 148:     throws IOException
 149:   {
 150:     writeInt(Float.floatToIntBits(value));
 151:   }
 152:   
 153:   public void writeFloats(float[] data, int offset, int len)
 154:     throws IOException
 155:   {
 156:     for(int i = 0; i < len; ++len)
 157:       writeFloat(data[offset + i]);
 158:   }
 159:   
 160:   public void writeInt(int value)
 161:     throws IOException
 162:   {
 163:     if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
 164:       {
 165:         buffer[0] = ((byte) value);
 166:         buffer[1] = ((byte) (value >> 8));
 167:         buffer[2] = ((byte) (value >> 16));
 168:         buffer[3] = ((byte) (value >> 24));
 169:       }
 170:     else
 171:       {
 172:         buffer[0] = ((byte) (value >> 24));
 173:         buffer[1] = ((byte) (value >> 16));
 174:         buffer[2] = ((byte) (value >> 8));
 175:         buffer[3] = ((byte) value);
 176:       }
 177:     
 178:     write(buffer, 0, 4);
 179:   }
 180:   
 181:   public void writeInts(int[] data, int offset, int len)
 182:     throws IOException
 183:   {
 184:     for(int i = 0; i < len; ++len)
 185:       writeInt(data[offset + i]);
 186:   }
 187:   
 188:   public void writeLong(long value)
 189:     throws IOException
 190:   {
 191:     if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
 192:       {
 193:         buffer[0] = ((byte) value);
 194:         buffer[1] = ((byte) (value >> 8));
 195:         buffer[2] = ((byte) (value >> 16));
 196:         buffer[3] = ((byte) (value >> 24));
 197:         buffer[4] = ((byte) (value >> 32));
 198:         buffer[5] = ((byte) (value >> 40));
 199:         buffer[6] = ((byte) (value >> 48));
 200:         buffer[7] = ((byte) (value >> 56));
 201:       }
 202:     else
 203:       {
 204:         buffer[0] = ((byte) (value >> 56));
 205:         buffer[1] = ((byte) (value >> 48));
 206:         buffer[2] = ((byte) (value >> 40));
 207:         buffer[3] = ((byte) (value >> 32));
 208:         buffer[4] = ((byte) (value >> 24));
 209:         buffer[5] = ((byte) (value >> 16));
 210:         buffer[6] = ((byte) (value >> 8));
 211:         buffer[7] = ((byte) value);
 212:       }
 213:     
 214:     write(buffer, 0, 8);
 215:   }
 216:   
 217:   public void writeLongs(long[] data, int offset, int len)
 218:     throws IOException
 219:   {
 220:     for(int i = 0; i < len; ++len)
 221:       writeLong(data[offset + i]);
 222:   }
 223:   
 224:   public void writeShort(int value)
 225:     throws IOException
 226:   {
 227:     if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
 228:       {
 229:         buffer[0] = ((byte) value);
 230:         buffer[1] = ((byte) (value >> 8));
 231:       }
 232:     else
 233:       {
 234:         buffer[0] = ((byte) (value >> 8));
 235:         buffer[1] = ((byte) value);
 236:       }
 237:     
 238:     write(buffer, 0, 2);
 239:   }
 240:   
 241:   public void writeShorts(short[] data, int offset, int len)
 242:     throws IOException
 243:   {
 244:     for(int i = 0; i < len; ++len)
 245:       writeShort(data[offset + i]);
 246:   }
 247:   
 248:   public void writeUTF(String value)
 249:     throws IOException
 250:   {
 251:     // NOTE: this code comes directly from DataOutputStream.
 252:     int len = value.length();
 253:     int sum = 0;
 254: 
 255:     for (int i = 0; i < len && sum <= 65535; ++i)
 256:       {
 257:         char c = value.charAt(i);
 258:         if (c >= '\u0001' && c <= '\u007f')
 259:           sum += 1;
 260:         else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff'))
 261:           sum += 2;
 262:         else
 263:           sum += 3;
 264:       }
 265: 
 266:     if (sum > 65535)
 267:       throw new UTFDataFormatException ();
 268: 
 269:     int pos = 0;
 270:     byte[] buf = new byte[sum];
 271: 
 272:     for (int i = 0; i < len; ++i)
 273:       {
 274:         char c = value.charAt(i);
 275:         if (c >= '\u0001' && c <= '\u007f')
 276:           buf[pos++] = (byte) c;
 277:         else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff'))
 278:           {
 279:             buf[pos++] = (byte) (0xc0 | (0x1f & (c >> 6)));
 280:             buf[pos++] = (byte) (0x80 | (0x3f & c));
 281:           }
 282:         else
 283:           {
 284:             // JSL says the first byte should be or'd with 0xc0, but
 285:             // that is a typo.  Unicode says 0xe0, and that is what is
 286:             // consistent with DataInputStream.
 287:             buf[pos++] = (byte) (0xe0 | (0x0f & (c >> 12)));
 288:             buf[pos++] = (byte) (0x80 | (0x3f & (c >> 6)));
 289:             buf[pos++] = (byte) (0x80 | (0x3f & c));
 290:           }
 291:       }
 292:     
 293:     writeShort (sum);
 294:     write(buf, 0, sum);
 295:   }
 296: }